Nginx反向代理不在localhost上使用Nodejs

时间:2014-07-15 11:10:55

标签: node.js ubuntu nginx proxy reverse-proxy

我有一个有5个可用IP地址的专用服务器,下面说X.X.X.1 - X.X.X.5是我的IP地址默认架构

X.X.X.1 - ns1.ex.com - 使用BIND

命名服务器

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

X.X.X.3 - www.ex.com - 使用nginx作为网络服务器

X.X.X.4 - 没什么

X.X.X.5 - 没什么

现在我正在尝试在nodejs(127.0.0.1:4501)上进行反向代理....我已经启动了应用程序,当我尝试通过反向代理访问节点应用程序时,它无法正常工作。我甚至试图调用http://localhost:4501/http://localhost:4501/test/之类的curl,因为app.js出现在/var/www/test/app.js。我也尝试将app ip地址更改为X.X.X.3,但没有结果

当我没有设置节点应用程序的IP地址时,它正在处理我放入的任何端口。我想将IP地址设置为localhost,以便只能通过Nginx访问它并且我有我的数据库我也想躲在Nginx身后。

以下是我的conf文件:

nginx.conf:出现在/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: /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'); 

1 个答案:

答案 0 :(得分:0)

这是一个非常愚蠢的错误,我的身边指出@Ben Fortune express listen方法首先接收端口然后ip地址