簡體   English   中英

為什么Node.js充當后端Node.js應用程序的代理,而不是Nginx?

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

我有一個簡單的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);

這很好。

關於我在做什么錯的任何想法嗎?

感謝有用的netstat輸出。 看來問題在於您的Node.js應用僅監聽IPv6,如輸出中的:::*

Nginx嘗試通過未監聽的IPv4進行連接。

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

您沒有共享所使用的Node.js版本。 某些版本存在一個問題,即嘗試建立IPv4連接會導致IPv6連接 您可能遇到了這樣的錯誤,或者您的Node.js應用實際上配置錯誤,無法偵聽IPv6。

如果端口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