Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 849
Aleks Avatar asked Mar 14 '15 00:03

Aleks


People also ask

Does NGINX use sockets?

NGINX 1.3. 13 and later and all NGINX Plus releases support proxying of WebSocket connections, which allows you to utilize Socket.IO.

Can I use Socket.IO without node JS?

Is it possible to use socket.io without any node. js dependencies? The short answer is yes. You will however have Flash dependency.

Does Socket.IO need a server?

Socket.io, and WebSockets in general, require an http server for the initial upgrade handshake. So even if you don't supply Socket.io with an http server it will create one for you.

Does Socket.IO use WSS?

Note: You can use either https or wss (respectively, http or ws ).


2 Answers

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";     } 
like image 115
Paulo Henrique Martins Avatar answered Sep 19 '22 20:09

Paulo Henrique Martins


I you still have problems, like I did, here is what you need to do: align the 3 configurations. Let's say you want to communicate over "mysocket":

niginx:

  location /mysocket/ {       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;       proxy_cache_bypass $http_upgrade;     } 

nodejs:

const io = require('socket.io')(server, {   path: '/mysocket' }); 

client:

var io = require('socket.io-client'); const socket = io('http://myserver.com, {     path: '/mysocket' }); 
like image 41
Delcon Avatar answered Sep 23 '22 20:09

Delcon