【问题标题】:Why does Node.js work as a proxy to backend Node.js app, but not Nginx?为什么 Node.js 可以作为后端 Node.js 应用程序的代理,而不是 Nginx?
【发布时间】:2016-01-21 01:12:28
【问题描述】:

我有一个简单的 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);

这很好用。

关于我做错了什么有什么想法吗?

【问题讨论】:

    标签: linux node.js nginx


    【解决方案1】:

    感谢有用的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   
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-10
      • 1970-01-01
      • 2017-12-24
      • 2015-03-09
      • 2015-02-12
      • 1970-01-01
      • 2013-06-03
      • 1970-01-01
      相关资源
      最近更新 更多