How to connect node to nginx reverse proxy

Posted January 5, 2018 26.5k views
NginxApacheNode.jsUbuntu 16.04

Hi all,

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

> node ./src/index.js

      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/
    at Object.<anonymous> (/var/www/html/
    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 “” (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:


server {
        listen 80;

        root /var/www/html/;

        index index.html;


        location / {
                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;


        ServerAdmin somebody@some.where
        DocumentRoot /var/www/html/
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined


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.get('/getData', (req, res) => {
})'/publish', (req, res) => {


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.

Submit an Answer
1 answer

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

        ServerAdmin somebody@some.where
        DocumentRoot /var/www/html/
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

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


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

        root /var/www/html/;
        index index.html;

They are telling Nginx to service content directly from /var/www/html/ 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.