Ankit Ladhania Ankit Ladhania - 1 month ago 18
Node.js Question

Nginx Reverse Proxy not working with Nodejs at localhost

I have a dedicated server with 5 usable IP address,say

X.X.X.1 - X.X.X.5
below is my default schema for IP addresses

X.X.X.1
- ns1.ex.com - name server using
BIND


X.X.X.2
- ns2.ex.com

X.X.X.3
- www.ex.com - using nginx as the web server

X.X.X.4
- nothing

X.X.X.5
- nothing

Now I am trying to do reverse proxying on
nodejs(127.0.0.1:4501)
.... I have started the app and when I am trying to access the node app through reverse proxy it's not working. I have even tried to call curl like
http://localhost:4501/
and also
http://localhost:4501/test/
as the app.js is present at
/var/www/test/app.js
. I also tried changing the app ip addresses to X.X.X.3 but with no result

When I don't set the IP address of the node app then it is working on any port that I put in. I want to set the IP address to localhost so that it is only accessible through Nginx and I have my database that I also want to hide behind Nginx.

Below are my conf files:

nginx.conf:
present at
/usr/local/nginx/conf/nginx.conf


#===============================================================================
# Main Configuration Settings
#===============================================================================
user root admins;
worker_processes auto;
master_process on;
worker_rlimit_nofile 16384;
worker_priority 0;

#================================================================================
# Error Log Setting Goes HEre
#================================================================================

events {
multi_accept off;
worker_connections 5120;
}

http {
include mime.types;
default_type application/octet-stream;

open_file_cache max=10000 inactive=30s;
open_file_cache_errors on;
client_body_buffer_size 200M; #200 MB

log_not_found on; #LOG All 404 error code
log_format main '{'
' IP:"$remote_addr:$remote_port", Time:"$time_local", Request_Type:"$request", '
' Status:"$status", Referer:"$http_referer", '
' Agent:"$http_user_agent", Forwarded_By:"$http_x_forwarded_for" '
'}';

#===========================================
# Caching DNS records For 1 HR
#==========================================
resolver 8.8.8.8 8.8.4.4 valid=1h;
resolver_timeout 10s;

#sendfile on;

keepalive_timeout 10;

#=================================================================================
# Gzip Module
#=================================================================================
gzip on;
gzip_comp_level 4;
gzip_min_length 20;
gzip_vary on;
gzip_proxied any;
upstream localhost_servers {
server 127.0.0.1:4501;
keepalive 64;
}
server {
listen 80;
server_name www.ex.com ex.com;
charset UTF-8;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost_servers;
proxy_redirect off;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

}
}


app.js:
at /var/www/test/app.js

var express = require('express');
var morgan = require('morgan');
var bodyParser = require('body-parser');
var methodOverride = require('method-override');
var app = express();
app.use(express.static(__dirname + '/public'));
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
app.use(methodOverride());

app.get('/',function(req,res){
console.log(req.ip,req.host,req.path,req.originalUrl);
res.send(req.body);
});
app.listen('127.0.0.1',4501);
console.log('Magic happens on port 80');

Answer Source

It was a Very silly mistake from my side as pointed by @Ben Fortune express listen method takes port first and then ip address