D
D
Dmitry2015-09-01 11:02:59
Nginx
Dmitry, 2015-09-01 11:02:59

How to properly configure Nginx & Node + socket.io?

Hello. Started setting up a test application to test the socket (video chat). Put node on port 8888, the site itself is on port 80.
index.html : pastebin.com/2454EYmi
server.js : pastebin.com/H90XS3Mc
_ mistake. What needs to be changed in nginx so that when entering the site itself, it is proxied to port 8888? nginx settings: pastebin.com/duiFQxrw Thanks.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
O
ofstudio, 2015-09-10
@ofstudio

I have an application on meteor.js - it has sockets inside it. The settings are

# =========================== live.wcs.life ===========================
map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

server {
    server_name live.wcs.life www.live.wcs.life;
    listen 80;
    access_log /srv/www/logs/live.wcs.life.access.log;
    error_log /srv/www/logs/live.wcs.life.error.log;
    location / {
        proxy_pass http://127.0.0.1:8888;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade; # allow websockets
        proxy_set_header Connection $connection_upgrade;
        proxy_set_header X-Forwarded-For $remote_addr; # preserve client IP

        # This setting allows the browser to cache the application in a way compatible 
        # with Meteor on every applicaiton update the name of CSS and JS file is different,
        # so they can be cache infinitely (here: 30 days)
        # The root path (/) MUST NOT be cached
         if ($uri != '/') {
             expires 30d;
         }    
    } 

}

S
standemchuk, 2015-09-10
@standemchuk

nginx, unfortunately, does not yet know how to do websocket proxying.
But, if this option suits you, you can configure socket.io to use the xhr-polling transport .
This means that with the help of AJAX requests, socket.io will emulate the behavior of websockets and this is basically sufficient in most cases. But still I advise you to check whether it suits you in your particular use case.
Configuration example:

var socket = require('socket.io').listen(80, {
  // options can go here
  transports: ['xhr-polling']
});

S
SibTeam, 2017-07-20
@SibTeam

In nginx config:

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 true;
      proxy_pass http://localhost:8888;
      proxy_redirect off;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question