Socket.io + nginx + SSL = * 31上游过早关闭连接

时间:2015-10-14 17:04:41

标签: node.js ssl nginx websocket socket.io

我正在尝试将Node.js服务器(带有socket.io)作为主应用程序的pub / sub服务器运行。我将它推送到服务器并创建了一个子域(使用SSL)。我的客户端HTML页面可以加载socket.io/socket.io.js,但WSS握手不能按预期工作:

WebSocket connection to 

'wss://ws.example.com/socket.io/?EIO=3&transport=websocket&sid=ogEegJXhjFh5lplgAAAF' 

failed: Error during WebSocket handshake: Unexpected response code: 502

我的 nginx 中定义了 SSL 子域名,如下所示:

server {
    listen 80;
    listen 443 default ssl;
    server_name ws.example.com;

    # ssl on;
    ssl_certificate     /etc/nginx/certificates/certificate-ws-example-com.crt;
    ssl_certificate_key /etc/nginx/certificates/ws.example.com.key;

    # Redirect all non-SSL traffic to SSL.
    if ($ssl_protocol = "") {
        rewrite ^ https://$host$request_uri? permanent;
    }

    location / {
        proxy_pass http://localhost:6969;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection upgrade;
        proxy_set_header Host $host;

        access_log /var/log/nginx/example_production.access.log;
        error_log /var/log/nginx/example_production.error.log;
    }
}

我的 Node.js (pm2)服务器响应我:

[error] 29726#0: *31 upstream prematurely closed connection
while reading response header from upstream, 

  client: 86.228.47.218, 
  server: ws.example.com, 
  request: 
    "GET /socket.io/?EIO=3&transport=websocket&sid=ogEegJXhjFh5lplgAAAF HTTP/1.1",
  upstream: 
    "http://127.0.0.1:6969/socket.io/?EIO=3&transport=websocket&sid=ogEegJXhjFh5lplgAAAF", 
  host: "ws.example.com"

0 个答案:

没有答案