I am using socket.io behind nginx which is behind azure load balancer. How ever On the client side I am constantly getting errors like
WebSocket connection to 'ws://**********/socket.io/? session_id=i9bk_iqkVrUveKwvIBz4fMNDbkoYuaITQ_APO73sgQd6-tQBaRkjp8RR8N9LTA5LnqMeKXzZg5AXXgjEevFKqSKRJJI8iaK3&id=dc978ae038af4746baf68ead35d182f4&EIO=3&transport=websocket&sid=61LGLpdw53xaMYqBAAJR' failed: Error during WebSocket handshake: Unexpected response code: 502
Also nginx gives the error below
[error] 15348#0: *84812 upstream prematurely closed connection while reading response header from upstream, client: 10.100.50.14, server: _, request: "GET /prod/socket.io/?session_id=nR0P30IDeUutoavDyjcqAQ8hUw_3l7dtAHQ3tqzW4zVT8eBOxwbHZq_7mWd9K7qRNO2Aq45QXm8w2KSvzyFlq3O4w7P2tl2q&id=955bb63a4f804b42b9d85ac8cf9172a7&EIO=3&transport=websocket&sid=xXGRnAsjKX6Gj-SAAAls HTTP/1.1", upstream: "http://127.0.0.1:3000/socket.io/?session_id=nR0P30IDeUutoavDyjcqAQ8hUw_3l7dtAHQ3tqzW4zVT8eBOxwbHZq_7mWd9K7qRNO2Aq45QXm8w2KSvzyFlq3O4w7P2tl2q&id=955bb63a4f804b42b9d85ac8cf9172a7&EIO=3&transport=websocket&sid=xXGRnAsjKX6Gj-SAAAls"
Anyone have an idea about the reason?
Nginx conf:
proxy_pass http://lb-prod/; # Load balance the URL location "/" to the upstream lb1
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
The problem here was about session stickyness. Configuring my load balancer to use sticky session solved the problem!
There is a blog from Nginx offical website which is the best practice for using NGINX and NGINX Plus with Node.js and Socket.IO.
I think it's very helpful for you now.
Related
I've dealt with this error in the nginx error log for the past few hours.
*2 connect() failed (111: Connection refused) while connecting to upstream, client: my ip, server: my domain, request: "GET / HTTP/2.0", upstream: "http://127.0.0.1:3000/", host: "my domain"
I'm currently trying to deploy a next.js app with nginx using engintron for CPanel as well as pm2.
default.conf
server {
listen [::]:80 default_server ipv6only=off;
server_name my domain domain-ip;
# deny all; # DO NOT REMOVE OR CHANGE THIS LINE - Used when Engintron is disabled to block Nginx from becoming an open proxy
# Set the port for HTTP proxying
set $PROXY_TO_PORT 8080;
include common_http.conf;
common_http.conf
location / {
proxy_pass http://127.0.0.1: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;
}
There aren't any errors on pm2's front, and sudo nginx -t works just fine, so I'm confused on what exactly the issue is.
Any sort of help is appreciated, have a good rest of your day :)
Fixed the issue, for some reason my pm2 wasn't working properly, after a clean reinstall I got this issue fixed, but now I've got a 404 not found for my index file, the fun life of being a developer lol
I have set up my node.js server using elastic beanstalk. Recently I have seen that my server health is degrading to sever from ok state and I keep getting emails for that.
While checking the logs in the error. logs of Nginx I found out that most of the errors are occurring because of
2022/04/23 18:34:14 [error] 3620#0: *14 upstream prematurely closed connection while reading response header from upstream, client: 172.31.54.200, server: , request: "POST /parse/functions/processCloverRequest HTTP/1.1", upstream: "http://155.0.0.1:8081/parse/functions/processCloverRequest", host: "server-i9cck-env.us-east-1.elasticbeanstalk.com"
I have also handled each error in the application and increased the read and connect timeout of Nginx.
location / {
proxy_pass http://nodejs;
proxy_set_header Connection "";
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_read_timeout 300s;
proxy_connect_timeout 75s;
}
But still I am getting the emails and the health of my instances degrades. Is there a way to resolve this issue?
I have make my nodejs app, hosted it on digital ocean server connect it to domain name, and all works fine, but when i'm trying to put ssl certificate (using https module instead of http), it doesn't works. Here is a code:
var sslopt = {
key : fs.readFileSync('./ssl/server.key'),
cert : fs.readFileSync('./ssl/server.crt'),
ca : [fs.readFileSync('./ssl/ca1.crt'), fs.readFileSync('./ssl/ca2.crt')]
};
var server = https.createServer(sslopt,function(req,res){
...
});
server.listen(8001,function(err){
...
});
My nodejs app running fine but if i'm trying to access it, I just see the 502 Bad Gateway error, and no requests was sent to my nodejs app. When I have opened my nginx error log I see the errors
-date- -time- [error] 18116#18116: *1 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: -ip-, server: -server-, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8001/", host: -host-
But the most strange thing that if I'm trying to get access with https protocol and port 8001 (https://{domainname}.com:8001) I can see my app working fine, but connection is not secured.
I just can't understand what I'm doing wrong...
P.S.
my nginx config file
server {
listen *:443;
listen *:80;
server_name {myhostname};
access_log /var/log/nginx/qt.access.log;
error_log /var/log/nginx/qt.error.log;
root /srv/qt;
index index.html index.htm index.php;
# Headers to pass to proxy server.
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_cache_bypass $http_upgrade;
proxy_http_version 1.1;
proxy_redirect off;
# Go to next upstream after if server down.
proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
proxy_connect_timeout 5s;
# Gateway timeout.
proxy_read_timeout 20s;
proxy_send_timeout 20s;
# Buffer settings.
proxy_buffers 8 32k;
proxy_buffer_size 64k;
location / {
proxy_pass http://127.0.0.1:8001;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Recognizing that this is an old post, hopefully this will still help someone out. I just figured out what seems to be the same thing and it was proxy server related.
My NodeJS server worked fine without SSL but gave the 502 without ever reaching the server when I used https.
// =============================================================================
// START THE SERVER
var port = process.env.PORT || 8080; // set our port
https.createServer({
key: fs.readFileSync('./certs/private.key'),
cert: fs.readFileSync('./certs/cert.crt')
}, app).listen(port);
//app.listen(port);
console.log('API Active on port ' + port);
My client and server are behind a pfSense firewall using a Squid3 proxy server.
pfSense controls the DNS for api.mydomain.com and routes the traffic to my dev server when I make a call to api.mydomain.com:8080/myroute. I have a valid SSL/TLS cert for api.mydomain.com
With the app.listen(port) line uncommented everything worked great.
With the https.createServer(...) uncommented I got the 502 error and traffic never reached the server.
To fix:
In pfSense click Services -> Squid Proxy Server
Click on the ACLs
configuration page
At the bottom of the page find the section
called Squid Allowed Ports.
In the field For ACL SSL Ports, enter the port your
application is using (in my case 8080).
For me, rainbows appeared and bluebirds did sing.
I'm trying to deploy my laravel app with socket.io and I have everything up and running. Node, redis, etc. but when socket event is fired I get this exact error message.
XMLHttpRequest cannot load http://localhost/socket.io/?EIO=3&transport=polling&t=Lbe2fnV. Cross-origin redirection denied by Cross-Origin Resource Sharing policy.
I have gone into my nginx config file and added the following:
upstream app_yourdomain {
server 127.0.0.1:3000;
keepalive 8;
}
location ~* \.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;
proxy_cache_bypass $http_upgrade;
}
but it doesn't do anything.
How can I get rid of the cross-orgin error?
My biggest success came from this post
NodeJS server on DigitalOcean using socket.io returns connection refused
Try to set the origins property like so.
io.set('origins', 'http://localhost:3000');
You can put in every url you allow to get the request from for example
io.set('origins', 'http://google.com:80');
or allow all
io.set('origins', '*:*');
More about the origin property can be found here
I'm using Express.js to create a server to which I can connect using web sockets.
Even though it eventually seems to work (that, is connects and passes an event to the client), I initially get an error in Chrome's console:
Unexpected response code: 502
On the backend, the socket.io only logs warn - websocket connection invalid.
However, nginx logs this:
2012/02/12 23:30:03 [error] 25061#0: *81 upstream prematurely closed
connection while reading response header from upstream, client:
71.122.117.15, server: www.example.com, request: "GET /socket.io/1/websocket/1378920683898138448 HTTP/1.1", upstream:
"http://127.0.0.1:8090/socket.io/1/websocket/1378920683898138448",
host: "www.example.com"
Note: I have nginx dev running: nginx version: nginx/1.1.14 so it should support HTTP/1.1.
Also note that if I just use the node.js server without the nginx it works without any warnings.
Finally, here is my nginx config file:
server {
listen 0.0.0.0:80;
server_name www.example.com;
access_log /var/log/nginx/example.com.log;
location / {
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://node;
proxy_redirect off;
}
}
upstream node {
server 127.0.0.1:8090;
}
Any help would be greatly appreciated. I tried the fix suggested in this question but that didn't work either.
nginx has some kind of Web Socket support in unstable 1.1 branch only. See Socket.IO wiki.
Afaik there are currently only few stable Node.js based http proxies that support Web Sockets properly.
Check out node-http-proxy (we use this):
https://github.com/nodejitsu/node-http-proxy
and bouncy:
https://github.com/substack/bouncy
Or you can use pure TCP proxy such as HAproxy
Update!
nginx (1.3.13>=) supports websockets out of the box!
http://nginx.org/en/docs/http/websocket.html