2016-01-20 59 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 
相关问题