Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
373 views
in Technique[技术] by (71.8m points)

Socket.io with nginx

I'm trying to serve static files by nginx 1.6 and to proxy socket traffic coming from Node.js web server with socket.io .

This is the relevant part of nginx.conf:

location /socket.io/ {
            proxy_pass http://localhost:3000;       
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "Upgrade";
            proxy_set_header Host $host;
        }

It works perfectly directly between the browser and Node.js, but socket.io takes too long when proxied with nginx 1.6. A handshaking protocol takes too much time, but if left uninterrupted it eventually starts to work after a couple of minutes.

Static files delivery by nginx works perfectly.

What could be the problem?

UPDATE:

I analysed a bit the network traffic and determined that the following request lasts around a minute (it's exactly when the upgrade is requested):

Sec-WebSocket-Key: LhZ1frRdl+myuwyR/T03lQ==
Cookie: io=1-A7tpvwmoGbrSvTAAA5
Connection: keep-alive, Upgrade
Upgrade: websocket
....

The expected response is code 101 and:

Connection: upgrade
Sec-WebSocket-Accept: HXx3KKJadQYjDa11lpK5y1nENMM=
Upgrade: websocket
...

instead, the browser receives 400 and:

Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: http://localhost:8888
Connection: keep-alive
Content-Type: application/json
Server: nginx/1.6.2
Transfer-Encoding: chunked

UPDATE 2:

I determined that the same configuration works perfectly on my office computer, meaning that it's my home computer issue. Anyway, would be very nice to determine what exactly is going wrong.

question from:https://stackoverflow.com/questions/29043879/socket-io-with-nginx

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In a running server, the nginx's configuration being used here is:

  # Requests for socket.io are passed on to Node on port 3000
  location ~* .io {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_set_header X-NginX-Proxy false;

      proxy_pass http://localhost:3000;
      proxy_redirect off;

      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...