2016-01-20 57 views
0

我有一個簡單的nginx配置文件爲什麼Node.js作爲後臺Node.js應用程序的代理而不是Nginx?

server { 
    listen  80 default_server; 
    listen  [::]:80 default_server; 
    server_name ec2-x-x-x-x.compute-1.amazonaws.com; 
    #root  /home/ec2-user/dashboard; 

    # Load configuration files for the default server block. 
    # include /etc/nginx/default.d/*.conf; 
    location/{ 
    proxy_pass http://127.0.0.1:4000; 
    } 
    error_page 500 502 503 504 /50x.html; 
    location = /50x.html { 
     root html; 
    } 
} 

但是,當我發送請求時,它說,它無法訪問服務器。

服務器工作從4000端口罰款雖然和sudo netstat -tulpn給了我這個

Active Internet connections (only servers) 
Proto Recv-Q Send-Q Local Address   Foreign Address   State  PID/Program name 
tcp  0  0 0.0.0.0:80    0.0.0.0:*    LISTEN  6512/nginx: master 
tcp  0  0 0.0.0.0:22    0.0.0.0:*    LISTEN  1640/sshd 
tcp  0  0 127.0.0.1:25   0.0.0.0:*    LISTEN  1247/master 
tcp6  0  0 :::80     :::*     LISTEN  6512/nginx: master 
tcp6  0  0 :::22     :::*     LISTEN  1640/sshd 
tcp6  0  0 :::3000     :::*     LISTEN  15985/node 
tcp6  0  0 ::1:25     :::*     LISTEN  1247/master 
tcp6  0  0 :::4000     :::*     LISTEN  3488/node 
udp  0  0 0.0.0.0:68    0.0.0.0:*       484/dhclient 
udp  0  0 127.0.0.1:323   0.0.0.0:*       451/chronyd 
udp  0  0 0.0.0.0:1510   0.0.0.0:*       484/dhclient 
udp6  0  0 ::1:323     :::*        451/chronyd 
udp6  0  0 :::1458     :::*        484/dhclient 

此外,當我使用node作爲代理服務器

var http = require('http'), 
        httpProxy = require('http-proxy'); 
httpProxy.createProxyServer({target:'http://localhost:4000'}).listen(80); 

這工作得很好。

關於我在做什麼錯誤的任何想法?

回答

2

感謝有用的netstat輸出。看來問題在於你的Node.js應用程序只能監聽IPv6,如輸出中的:::*所示。

Nginx正試圖通過IPv4連接它,它沒有在監聽。

您的Node.js代理可能工作,因爲它在兩端共享相同的問題。 :)

您沒有共享您正在使用的Node.js版本。 Some versions had an issue where attempting to set up an IPv4 connection would result in an IPv6 connection。要麼你遇到過這樣的錯誤,或者你的Node.js應用程序實際上被錯誤地配置爲偵聽IPv6。

如果端口400 Node.js的應用程序是正確配置爲偵聽IPv4中,你會看到這樣的條目在netstat輸出:

tcp  0  0 127.0.0.1:4000  0.0.0.0:*  LISTEN  12345/node 
相關問題