簡體   English   中英

Nginx反向代理不適用於本地主機上的Nodejs

[英]Nginx Reverse Proxy not working with Nodejs at localhost

我有一個具有5個可用IP地址的專用服務器,例如下面的XXX1 - XXX5是我的IP地址默認架構

XXX1 - ns1.ex.com -域名服務器使用BIND

XXX2 - ns2.ex.com

XXX3 -www.ex.com-使用Nginx作為Web服務器

XXX4沒有

XXX5沒有

現在,我正在嘗試在nodejs(127.0.0.1:4501)上進行反向代理....我已經啟動了應用程序,當我嘗試通過反向代理訪問節點應用程序時,它無法正常工作。 我什至嘗試調用curl,例如http://localhost:4501/以及http://localhost:4501/test/因為app.js位於/var/www/test/app.js 我也嘗試將應用程序的IP地址更改為XXX3,但沒有結果

當我不設置節點應用程序的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'); 

@Ben Fortune表示,這對我來說是一個非常愚蠢的錯誤express listen方法首先獲取端口,然后獲取IP地址

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM