I set a nginx server to serve my static angular site and want to use sails to provide a rest api to deal with a mongo database. I set a prefix of /api to the sails server blueprints and set nginx proxy like this:
location /api/ {
proxy_pass http://localhost:1337;
# the following is required for WebSockets
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header Port $server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass_request_headers on;
}
the nginx server is set to http://localhost:10018 and in my client angular site i create a socket.io connection like this:
var socket = io.connect('http://localhost:10018', {
path: '/api/socket.io'
});
The problem is that handshake is not performed and i'm getting a 404 response from the server with socket.io!!!! Is that a problem with the nginx? Or with my sails config?
PS.: I'm using sails 0.10, nginx 1.4.7 and socket.io 1.0.
path: '/api/socket.io'is NOT correct
------------------ 原始邮件 ------------------
发件人:brunomacf "[email protected]"
时 间:2014/06/20 11:08:41 星期五
收件人:balderdashysails "[email protected]"
抄送人:
主 题:[sails] socket.io connection through nginx (#1868)
I set a nginx server to serve my static angular site and want to use sails to provide a rest api to deal with a mongo database. I set a prefix of /api to the sails server blueprints and set nginx proxy like this:
location /api/ {
proxy_pass http://localhost:1337;
# the following is required for WebSockets
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header Port $server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass_request_headers on;
}
the nginx server is set to http://localhost:10018 and in my client angular site i create a socket.io connection like this:
var socket = io.connect('http://localhost:10018', {
path: '/api/socket.io'
});
The problem is that handshake is not performed and i'm getting a 404 response from the server with socket.io!!!! Is that a problem with the nginx? Or with my sails config?
—Reply to this email directly or view it on GitHub.
Ok...so what would be the correct path? I've tried the default path of '/socket.io' and even though the connection is not stablished!!
Anyone???
First off, be patient. You can't expect people to answer immediately. People tend to completely ignore you if you're impatient.
Secondly, remove the path like so if your backend is on the same server as your front end:
var socket = io.connect();
And this if it's on a different host:
var socket = io.connect('http://localhost:10018');
Realize that the prefix option is only for API calls. The socket.io server is always running on the host unless you're working with socket namespaces.
Hi RWOverdijk!! Sorry about my impatience!! It's because i'm working for the first time with sails in a project of mine with a limited time!!! Well....everything is in the same host: the sails server backend application is running in port 1337 and the the nginx server is on port 10018!! When i tried to connect with
var socket = io.connect();
socket.on('connect', function() { console.log('connected');});
or
var socket = io.connect('http://localhost:10018');
socket.on('connect', function() { console.log('connected');});
the 'connected' message is never print out (and my laptop fan is going crazy!!!)......
This is the nginx configuration that i'm using to serve my site and proxy the /api requests to the sails server:
server {
listen 10018;
server_name localhost;
root /Users/Bruno/Dropbox/Projetos/Solido/Sites/solido_app/build;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
location /socket.io/ {
# proxy to the sails server
proxy_pass http://localhost:1337;
# the following is required for WebSockets
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header Port $server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass_request_headers on;
}
location /api/ {
# proxy to the sails server
proxy_pass http://localhost:1337;
# the following is required for WebSockets
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header Port $server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass_request_headers on;
}
}
Why are you not sending it through to / though? Sails serves files, too. Or do you want to use it as a backend only? Either way, I think the /api bit is causing trouble. I haven't used nginx for this before so I can't help. Perhaps someone else can :)
For now, you'll have to debug. Lift sails with sails lift --verbose and see if you can spot any errors.
I just want to use sails as a backend server! The static files (on / path) will be provided by nginx and the sails must run on /api path!! I just get some response from sails (running with --verbose) when i try to connect socket.io like this:
var socket = io.connect('http://localhost:10018', {path: '/api/socket.io'});
and the response that sails is providing is 404!! What the heck is this???? I think that the nginx configuration is all right....the problem is with sails sockets!!
With --verbose and trying to create socket connection like:
var socket = io.connect('http://localhost:10018');
i get and infinite print of
verbose: unhandled socket.io url
in the sails console!!!
Ok....i found the problem!! I was using socket.io 1.0 in my client code, while sails 0.10 still using socket.io 0.9.14!!! I'm closing this issue, but just to update my nginx conf file to future users:
server {
listen 10018;
server_name localhost;
root /Users/Bruno/Dropbox/Projetos/Solido/Sites/solido_app/build;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
location /socket.io/ {
proxy_pass http://localhost:1337;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location /api/ {
proxy_pass http://localhost:1337;
}
}
my older conf file is buggy and cause the connection on socket.io to be rejected in the first heartbeat and get connected just in the second heartbeat!! I'm using nginx 1.4.7 version! :)
Thanks for everyone!! Long life to javascript!!!
I hit the same problem, but can not correct it by your way, can provide the sample of io.connect parameter
var socket = io.connect(provider.url,{path: '/api/socket.io/'});
at your side?
just to complete @brunomacf nginx config, for socket to work in nginx, you need the config mentioned:
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
and also you need to add this to nginx.conf:
##
# Virtual Host Configs
##
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
Check https://www.nginx.com/blog/websocket-nginx/ for more info
Most helpful comment
First off, be patient. You can't expect people to answer immediately. People tend to completely ignore you if you're impatient.
Secondly, remove the path like so if your backend is on the same server as your front end:
And this if it's on a different host:
Realize that the
prefixoption is only for API calls. The socket.io server is always running on the host unless you're working with socket namespaces.