How to connect node to nginx reverse proxy

January 5, 2018 3.6k views
Nginx Node.js Apache Ubuntu 16.04

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)
1 Answer

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:

by Mateusz Papiernik
In this tutorial, you will set up Apache as a reverse proxy using the `mod_proxy` extension to redirect incoming connections to underlying application server(s) running on the same network. There are instructions on setting up a simple web app using the Flask framework to show how Apache interacts with the real application hidden behind it, but you can also follow this tutorial using your existing application server, if you have one.
Have another answer? Share your knowledge.