Question

How to connect node to nginx reverse proxy

Hi all,

I am receiving the following error message by my local node (express) application:

> node ./src/index.js

events.js:136
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE :::8080
    at Object._errnoException (util.js:1031:13)
    at _exceptionWithHostPort (util.js:1052:20)
    at Server.setupListenHandle [as _listen2] (net.js:1367:14)
    at listenInCluster (net.js:1408:12)
    at Server.listen (net.js:1496:7)
    at Function.listen (/var/www/html/menu.cocodnuts.com/server/node_modules/express/lib/application.js:618:24)
    at Object.<anonymous> (/var/www/html/menu.cocodnuts.com/server/src/index.js:22:8)
    at Module._compile (module.js:641:30)
    at Object.Module._extensions..js (module.js:652:10)
    at Module.load (module.js:560:32)

I’ve followed tutorials like How To Set Up a Node.js Application for Production on Ubuntu 14.04 under the impression that by setting my express application to listen to the same port (8080) as the proxy_pass, my client application could request data from “my.site.com” (without adding any ports to the address) and said request would eventually be transferred to the node application through the nginx reverse proxy.

I’ve attempted to resolve the conflict by changing the express app’s port to 8081 but still receive no requests from the client.

Here some relevant files:

/etc/nginx/sites-enabled/my.site.com

server {
        listen 80;

        root /var/www/html/my.site.com;

        index index.html;

        server_name my.site.com;

        location / {
                proxy_pass http://127.0.0.1:8080;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Fowarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Fowarded-Proto $scheme;
                proxy_cache_bypass $http_upgrade;

               try_files $uri $uri/ =404;
        }
}

/etc/apache2/sites-enabled/my.site.com.conf

<VirtualHost 127.0.0.1:8080>
        ServerAdmin somebody@some.where
        ServerName my.site.com
        DocumentRoot /var/www/html/my.site.com
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

./src/index.js

const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const morgan = require('morgan')
const handle = require('./handle.js')
const server = express()

server.use(morgan('combined'))
server.use(bodyParser.json())
server.use(cors())

server.get('/getData', (req, res) => {
        res.send(handle.get())
})

server.post('/publish', (req, res) => {
        res.send(handle.publish({
                "menu": req.body.menu
        }))
})

server.listen(8080)

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

The error Error: listen EADDRINUSE :::8080 is due to the fact that Apache is already listening to port 8080:

<VirtualHost 127.0.0.1:8080>
        ServerAdmin somebody@some.where
        ServerName my.site.com
        DocumentRoot /var/www/html/my.site.com
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

If you change the port that the Node application is running on to 8081, this also needs to be reflected in the Nginx configuration:

proxy_pass http://127.0.0.1:8081;

Additionally, if Nginx is only being used as a reverse proxy, these lines could be removed:

        root /var/www/html/my.site.com;
        index index.html;

They are telling Nginx to service content directly from /var/www/html/my.site.com which might cause some issues.

As it seems like you are already using Apache, you might want to look into mod_proxy. With it, you would be able to simplify your set up by removing Nginx from the equation. See: