Node Socket Server Errror on nginx server - javascript

I have created a socket for my messaging app which is working fine on the local server. Connection and Listeners are working properly.
But When I updated that on the server, Then not able to establish the connection.
I am using pm2 for running ports on nginx server.
This is how my configuration looks like here -
/etc/nginx/sites-available/default
location / {
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;
}
location /socket {
proxy_pass http://localhost:3001;
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;
}
PS: MY js code seems fine as that is working fine on local server so not adding here. I am assuming it's a problem from here with the configuration

Related

Graphql web socket connection failed over production

I Have tried every possible Nginx config but it's showing connection failed every time.
config file:
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
#try_files $uri $uri/ =404;
proxy_pass http://127.0.0.1:1337;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
}
location /graphql {
proxy_pass https://bakbakgraphql.com/graphql;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
}
Error:

Socket.io not working with nginx reverse proxy?

I'm trying to use socket.io with my nginx reverse proxy server, but it seems not working..
Chrome showing this wonderful waterfall:
screenshot:
and server's response is always like this:
96:0{"sid":"UU9d1DfZL5RD29kAAACq","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":5000}2:40
nginx.conf:
server {
listen 443 ssl;
server_name socket.my.domain;
ssl_certificate /etc/letsencrypt/live/socket.my.domain/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/socket.my.domain/privkey.pem;
location / {
proxy_pass http://127.0.0.1:1027/;
chunked_transfer_encoding off;
proxy_buffering off;
proxy_set_header Upgrade websocket;
proxy_set_header Connection "upgrade";
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr; # This line.
proxy_set_header Host $host;
proxy_http_version 1.1;
}
location /socket.io/ {
proxy_pass http://127.0.0.1:1027/socket.io/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect off;
proxy_buffers 8 32k;
proxy_buffer_size 64k;
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;
}
}
socket.io server:
const io = require("socket.io")(1027);
io.on("connection", (socket) => {
console.log('connected');
});
front-end javascript:
let socket = io("https://socket.my.domain");
socket.on("connect", () => {
console.log(socket.id);
});
Server keep logging the message 'connected', but client doesn't log the message.
What's wrong with my code?

create-react-app code changes behind nginx reverse proxy not reloading in browser

Ive installed a new CRA template and Im unable to see file changes reflect in the browser. Im reverse proxying React app through nginx using unmodfied CRA installation (unmodified apart from setting .env VAR PORT corresponding to standard nginx websocket proxy config). Still I see persistent browser error:
webpackHotDevClient.js:60 WebSocket connection to 'wss://react.syntapse.co.uk/sockjs-node' failed: Error during WebSocket handshake: Unexpected response code: 301
.env file
PORT=3121
nginx configuration
server {
listen 80;
server_name react.syntapse.co.uk;
location /sockjs-node/ {
proxy_pass http://0.0.0.0:3121/sockjs-node/;
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;
}
location / {
proxy_pass http://0.0.0.0:3121/;
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;
}
}
I haven;t taken any specific steps to enable HMR or any form of hot reloading just expecting code changes to update in the browser. I'm trying out different platforms and ive got near identical nginx config for default angular and vue apps which just work with HMR out the box so I guess React config needs tweaking to work with through proxy but the documentation is a bit sparse.
Any help or insights into using React with nginx reverse proxy are much appreciated.
Thanks
Answer in plain sight the whole time! drop the end slash.
location /sockjs-node {
proxy_pass http://0.0.0.0:3121/sockjs-node;
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;
}

Making Socket.io path work with NGiNX proxy_pass

I would like to use NGiNX to proxy pass the following:
https://something.com/node.js/foo/bar/baz
into this:
https://something.com:3000/foo/bar/baz
I have successfully done it with the following NGiNX config:
location ~ /node.js/(.*) {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "";
add_header Cache-Control "public";
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_pass http://127.0.0.1:3000/$1$is_args$args;
}
location / {
try_files $uri /index.php$is_args$args;
}
The problem is the following: when I instantiate socket.io with the default path, it serves socket.io.js here:
https://something.com/node.js/socket.io/socket.io.js
So far so good, but then this javascript tells the client to make requests here, which fail for obvious reasons:
https://something.com/socket.io/...
So then I try to instantiate socket.io with the path option like so:
io.listen(server, { path: '/node.js/socket.io'} );
But the problem is that now the socket.io.js file is hosted here:
https://something.com/node.js/node.js/socket.io/socket.io.js
Is there a way to tell socket.io.js where to host the file socket.io.js but still use the given path?
What is the normal solution to this proxy_pass stuff with socket.io??
Well different ways to solve your issue but I would use below one
location ~ /node.js/(.*) {
rewrite "^/node.js/node.js/(.*)$" /node.js/$1 last;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "";
add_header Cache-Control "public";
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_pass http://127.0.0.1:3000/$1$is_args$args;
}
location / {
try_files $uri /index.php$is_args$args;
}

Node.JS/socket.io and safari

I have a node.js software that use socket.io 1.3.0 lib for communication between client and server.
In my architecture the server is behind a proxy nginx.
I test the software with all last browser version.
Everything works except when i use Safari (last version).
I read that Safari uses an earlier version of the protocol WebSocket (hixie-76).
My question is: there is a way to change in socket.io the protocol version when i use Safari browser?
This is my nginx (1.7.12) configuration:
location / {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $http_host;
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_redirect off;
proxy_pass http://internalhost:8080/;
}
Thanks in advance.

Categories