Websocket handshake error with Nginx - node.js

I am using Socket.io in a webpage, but it doesn't work. In the console.log I get the following error:
socket.io.js:2 WebSocket connection to 'ws://www.somewebpage.com/socket.io/?EIO=3&transport=websocket&sid=uoSGxATXKabezBn8AABP' failed: Error during WebSocket handshake: Unexpected response code: 400
I have this Nginx configuration:
location / {
proxy_pass http://melo;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
What can I check to solve this?

Related

twilio application showing 404 error after deployment to Linux server

We have developed a testing application in Twilio Sandbox and it is running fine in Heroku server. But when we are trying to put it in Linux (centos 8.1.1911) server, getting 404 error.
conf file in Linux server:
location /myapp/ {
proxy_pass http://13.71.23.139:3020/;
proxy_ssl_server_name on;
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;
error:
*212 connect() failed (111: Connection refused) while connecting to upstream
Any suggestions?

expressjs setting tls connect https nginx server for request

How can set the request of expressjs to properly identify a TLS connection with https nginx server so that I can perform authentication through getPeerCertificate?
this is my nginx config to transfer request to expressjs api
location /api {
proxy_pass http://10.88.132.14:4337/api;
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;
}
You need to pass the SSL-token and then manually decode it. You pass it through adding X-SSL-CERT with the $ssl_client_escaped_cert. Make sure you are using Nginx 1.13 or later as the $ssl_client_escaped_cert didn't exist in 1.12.
location /api {
proxy_pass http://10.88.132.14:4337/api;
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;
proxy_set_header X-SSL-CERT $ssl_client_escaped_cert;
}
Now you can't use getPeerCertifice() as this requires the full SSL-connection. Instead you decode the x-ssl-cert header from above using the x509 package:
let cert = req.headers['x-ssl-cert'];
try {
cert = decodeURIComponent(cert);
console.log(x509.getSubject(cert));
} catch (error) {
console.log('Bad SSL-certificate?', error);
}

Webpack Dev Server with NGINX proxy_pass

I'm trying to get webpack-dev-server running inside a Docker container then accessing it through a NGINX host. The initial index.html loads but the Web Sockets connection to the dev server cannot connect.
VM47:35 WebSocket connection to 'ws://example.com/sockjs-node/834/izehemiu/websocket' failed: Error during WebSocket handshake: Unexpected response code: 400
I'm using the following config.
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream webpack_dev_server {
server node;
}
server {
server_name _;
listen 80;
root /webpack_dev_server;
location / {
proxy_pass http://webpack_dev_server;
}
location /sockjs-node/ {
proxy_pass http://webpack_dev_server/sockjs-node/;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host; # pass the host header - http://wiki.nginx.org/HttpProxyModule#proxy_pass
proxy_http_version 1.1; # recommended with keepalive connections - http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_http_version
# WebSocket proxying - from http://nginx.org/en/docs/http/websocket.html
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
}
Proxy pass should be ip and port of your webpack-dev-server container and you need proxy_redirect off;
location /sockjs-node {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://node:8080;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
Also don't forget to add poll to your webpack-dev middleware
watchOptions: {
aggregateTimeout: 300,
poll: 1000
}

Nginx conf for socket.io

I am using socket.io on my nginx server.
I followed the doc from nginx to configure my server (http://nginx.com/blog/nginx-nodejs-websockets-socketio/):
location = / {
proxy_pass_header Server;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect off;
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_set_header X-Forwarded-Proto $scheme;
proxy_pass http://frontend;
proxy_intercept_errors on;
error_page 501 502 503 504 /50x.html;
}
upstream frontend {
server 127.0.0.1:7300;
}
However I still get the following error in my browser:
WebSocket connection to 'ws://<MY DOMAIN>/socket.io/?EIO=3&transport=websocket&sid=oway-6XKt7_HvqfQAAAC'
failed: Error during WebSocket handshake: Unexpected response code: 400
Everything works fine locally so I get the problem comes from my nginx.
What should I change to make it work?
Many thanks

websocket unable to connect on ec2 with nginx

I am running node.js and socket.io on EC2, using nginx v1.4.1
Here is my /etc/nginx/sites-available/default
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 80;
server_name mysite.com;
location / {
proxy_pass http://localhost:4321;
proxy_set_header Host $http_host;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
}
However I get
WebSocket connection to 'ws://mysite.com/socket.io/1/websocket/nS7f2eI5jAZ-pIN_8fai' failed: Unexpected response code: 502
and from node:
debug - setting request GET /socket.io/1/websocket/nS7f2eI5jAZ-pIN_8fai
debug - set heartbeat interval for client nS7f2eI5jAZ-pIN_8fai
warn - websocket connection invalid
Then it switches to xhr-polling.
I have followed exactly how so many online guides say about Nginx + websocket, but I don't know where I made a mistake.
As per the Nginx documentation, you can try the following in your nginx config file.
location /wsapp/ {
proxy_pass http://your-host:your-port/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
}

Resources