websocket无法使用nginx连接ec2

时间:2013-08-20 13:42:02

标签: node.js nginx amazon-ec2 socket.io

我在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的说法,但我不知道我在哪里犯了错误。

1 个答案:

答案 0 :(得分:0)

根据 Nginx documentation,您可以在 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;
   }