2
votes

I'm using NGINX to handle my Nodejs servers and now I want to start using socket.io on them, doing research before starting to play with socket.io i found Sailsjs which I grew rather found of and now currently using.

The problem I meet though, is that the client can't connect through NGINX on socket.io, or sails.io.

This is the URL path for my current APP: https://localhost/economy

And the url for sails.io is, as standard: https://localhost/economy/assets/js/dependencies/sails.io.js

Now! Using FileSeek I found out where sails.js use "require("socket.io")", which is in the "loadSocketIO.js" (node_modules\sails\lib\hooks\sockets\lib). I edited the file to say:

var io = sails.io = sails.ws =
    SocketServer.listen(sails.hooks.http.server, {
  resource: 'economy',
        logger: {
            info: function (){}
        }
    });

adding the resource: 'economy', line. Then on the client side I did the same:

var socket = io.connect('https://localhost/economy/socket.io', { resource: 'economy' });

I've been playing with this and tried different ways, like "economy/socket.io", the full socket.io path etc.

Any help, is a lot of help!

I've been struggling for a day now so I figured i'd ask for some help :)

3

3 Answers

3
votes

You shouldn't need to change anything on the Sails side. Try reading up on proxy_pass for your location directive in Nginx.

http://nginx.org/en/docs/http/websocket.html

This is an excerpt from the above link:

location /chat/ {

    proxy_set_header        X-Real-IP       $remote_addr;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

    proxy_pass http://localhost:1337;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}
2
votes

SOLUTION!

Run everything as it is, the only part you have to edit is in NGINX.

set a server block with the servername as economy.localhost/, and you're done :) Remember to link economy.localhost to 127.0.0.1 or set a cname if this is a production server :)

0
votes

The example showed here worked for me.

Basically for nginx WebSocket support you need to include the following config.

server {
    server_name app.domain.com;
    location / {
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_pass http://localhost:8080;
    }
}