3

我在 EC2 上运行 node.js 和 socket.io,使用 nginx v1.4.1

这是我的 /etc/nginx/sites-available/default

map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}


server {
        listen 80;

        server_name mysite.com;

        location / {
                proxy_pass http://localhost:4321;
                proxy_set_header Host $http_host;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection $connection_upgrade;
        }
}

但是我得到 WebSocket connection to 'ws://mysite.com/socket.io/1/websocket/nS7f2eI5jAZ-pIN_8fai' failed: Unexpected response code: 502

并从节点:

debug - setting request GET /socket.io/1/websocket/nS7f2eI5jAZ-pIN_8fai
debug - set heartbeat interval for client nS7f2eI5jAZ-pIN_8fai
warn  - websocket connection invalid

然后它切换到 xhr-polling。

我完全遵循了这么多在线指南关于 Nginx + websocket 的说法,但我不知道我在哪里犯了错误。

4

1 回答 1

0

根据Nginx 文档,您可以在 nginx 配置文件中尝试以下操作。

   location /wsapp/ {
        proxy_pass                           http://your-host:your-port/;
        proxy_http_version                   1.1;
        proxy_set_header                     Upgrade $http_upgrade;
        proxy_set_header                     Connection "Upgrade";
        proxy_set_header                     Host $host;
   }
于 2021-04-15T21:18:42.740 回答