简体   繁体   English

为什么Node.js充当后端Node.js应用程序的代理,而不是Nginx?

[英]Why does Node.js work as a proxy to backend Node.js app, but not Nginx?

I have a simple nginx config file 我有一个简单的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;
    }
}

But when I send the request, it says it cannot access the server. 但是,当我发送请求时,它说它无法访问服务器。

the server works fine from port 4000 though, and sudo netstat -tulpn gives me this 服务器从端口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

Also, when I use node as a proxy server 另外,当我使用node作为代理服务器时

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

this works just fine. 这很好。

any ideas as to what I'm doing wrong? 关于我在做什么错的任何想法吗?

Thanks for the useful netstat output. 感谢有用的netstat输出。 It appears the issue is that your Node.js app is only listening on IPv6, as represented by :::* in the output. 看来问题在于您的Node.js应用仅监听IPv6,如输出中的:::*

Nginx is trying to connect it via IPv4, where it is not listening. Nginx尝试通过未监听的IPv4进行连接。

Your Node.js proxy probably works because it shares the same issue on both ends. 您的Node.js代理可能有效,因为它在两端共享相同的问题。 :) :)

You didn't share which Node.js version you are using. 您没有共享所使用的Node.js版本。 Some versions had an issue where attempting to set up an IPv4 connection would result in an IPv6 connection . 某些版本存在一个问题,即尝试建立IPv4连接会导致IPv6连接 Either you've run into a bug like that, or your Node.js app is actually misconfigured to listen on IPv6. 您可能遇到了这样的错误,或者您的Node.js应用实际上配置错误,无法侦听IPv6。

If the Node.js app on port 400 was correctly configured to listen on IPv4, you would see this kind of entry in the netstat output: 如果端口400上的Node.js应用程序已正确配置为侦听IPv4,则您会在netstat输出中看到以下条目:

tcp        0      0 127.0.0.1:4000      0.0.0.0:*       LISTEN      12345/node   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM