“response code: 502” using socket.io and nginx 1.4

2019-07-16 16:17发布

问题:

I am trying to make running my app (using socket.io) under nginx, and i get an WebSocket connection to 'ws://...' failed: Unexpected response code: 502 error on the server side. I have read that nginx suport the websockets since the v.1.3 so i just have upgrade to the nginx v1.4.1 (stable).

Here is my conf:

server {
       listen 80;
       server_name website.com;

        location / {
          proxy_pass    http://127.0.0.1:3000;
          proxy_http_version 1.1;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection "upgrade";
          access_log    /var/log/nginx/website/access.log;
          error_log    /var/log/nginx/website/error.log;
        }
}

UPDATE:

I have to say that i can reach the app (so i do listen the port 3000) and socket.io is working unless this error, but in some cases, the emit call desn't send any arguments so i get undefined.

What am i missing here to avoid this error?

回答1:

I have found something to avoid this error:

io.set('transports', ['xhr-polling']);

So socket.io does now AJAX calls, it does the job but certainly a way much more slowly, so if someone has the real key to this issue i am still very interested!



标签: nginx