websocket无法在ec2上连接nginx



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

这是我的/etc/nginx/站点可用/默认

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轮询。

我已经准确地了解了很多在线指南对Nginx+websocket的描述,但我不知道我在哪里犯了错误。

根据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;
   }

最新更新