简体   繁体   中英

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

I have a simple nginx config file

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

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

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. It appears the issue is that your Node.js app is only listening on IPv6, as represented by :::* in the output.

Nginx is trying to connect it via IPv4, where it is not listening.

Your Node.js proxy probably works because it shares the same issue on both ends. :)

You didn't share which Node.js version you are using. Some versions had an issue where attempting to set up an IPv4 connection would result in an IPv6 connection . Either you've run into a bug like that, or your Node.js app is actually misconfigured to listen on 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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