gonzalez
By:
gonzalez

How to connect node to nginx reverse proxy

January 5, 2018 185 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)
Be the first one to answer this question.