Websocket connection issue on django production server - javascript

I am facing an issue with the WebSocket protocol. I am using Django channels/daphne. Everything is working normally on the development local server. I think the issue is with NGINX settings. The error code is 1006
Here is the reverse proxy that I am using:
location ~* \.(js|css|jpg|png|jpeg|html)$
{
try_files $uri $uri/ /;
}
location /
{
proxy_pass http://127.0.0.1:8000;
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_set_header REMOTE-HOST $remote_addr;
#Persistent connection related configuration
add_header X-Cache $upstream_cache_status;
#Set Nginx Cache
proxy_ignore_headers Set-Cookie Cache-Control expires;
add_header Cache-Control no-cache;
expires 12h;
}
location /ws/ {
proxy_pass http://127.0.0.1:8001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect off;
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_set_header X-Forwarded-Host $server_name;
}
#PROXY-END/

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:

Node Socket Server Errror on nginx server

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

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?

Nginx not asking for client cert when POST method with payload

I have configured nginx for ssl-client-authentication. It is working fine for GET and POST (with out payload). But when we use POST with body, client is not passing the certificate.
Either nginx is not asking for the cert
Or the client(javascript) itself is ignoring the cert i'm not sure
Nginx Configuration
upstream abc-abc.com {
ip_hash;
server 172.16.x.x:8987;
}
server {
listen 443 ssl;
client_max_body_size 100M;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_certificate ssl/bundle.crt;
ssl_certificate_key ssl/abc.key;
ssl_verify_client optional;
ssl_client_certificate /certs/client_ca.cert;
# Use Server preference
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_dhparam /etc/ssl/certs/dhparam.pem;
server_name "~^abc-(?:[A-Za-z0-9]{0,21}[A-Za-z0-9])?-?abc.com";
proxy_buffering off;
error_log /proc/self/fd/2;
access_log /proc/self/fd/1;
server_tokens off;
location = /robots.txt {
return 200 "User-agent: *\nDisallow: /\n";
}
location / {
proxy_pass https://abc-abc.com;
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-Forwarded-Proto $scheme;
# HTTP 1.1 support
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_read_timeout 36000s;
proxy_set_header X-SSL-Serial $ssl_client_serial;
proxy_set_header X-SSL-Verify $ssl_client_verify;
proxy_set_header X-SSL-SDN $ssl_client_s_dn;
proxy_set_header X-SSL-Cert $ssl_client_escaped_cert;
proxy_set_header X-SSL-Fingerprint $ssl_client_fingerprint;
proxy_set_header X-SSL-Start-Date $ssl_client_v_start;
proxy_set_header X-SSL-End-Date $ssl_client_v_end;
}
}
Please help, Thanks.

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;
}

Categories