Question

Socket.io call throwing 400 Bad Request for some users

Posted June 22, 2021 616 views
NginxNode.jsUbuntu 18.04

Hi,
We have hosted the Nodejs application using nginx as webserver with digicert ssl mapped to it.

nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 768;
}

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    types_hash_max_size 2048;
    server_tokens off;
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    ssl_protocols TLSv1.2 TLSv1.3; 
    ssl_prefer_server_ciphers on;
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log debug;
    add_header Set-Cookie "HttpOnly; Secure";
    add_header X-Frame-Options "SAMEORIGIN";
    gzip off;
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}
etc/nginx/sites-enabled/default_type
server
{
        listen 80;
        server_name mydomain.com www.mydomain.com;
        return 301 https://$host$request_uri;
}
server {
    large_client_header_buffers 4 16k;
    listen 443 ssl;
    server_name mydomain.com www.mydomain.com;
    access_log /var/log/nginx/nginx.vhost.access.log;
    error_log /var/log/nginx/nginx.vhost.error.log;
    ssl_certificate /etc/ssl/certs/bundle.crt;
    ssl_certificate_key /etc/ssl/certs/www_mydomain_com.key;
    ssl_prefer_server_ciphers on;

    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;

    location / {
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_redirect off;
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $http_host;
        proxy_pass https://www.mydomain.com:8080;
    }


    location ~ ^/(room|socket\.io) {
        proxy_pass https://www.mydomain.com:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $http_host;
        proxy_cache_bypass $http_upgrade;
        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;
        proxy_pass_request_headers on;
    }

    location /wsapp {
        proxy_pass https://www.mydomain.com:8080;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_hide_header 'Access-Control-Allow-Origin';
    }   
}
Server.js
const server = require('https').createServer(credentials, app).listen(8080, 'www.mydoamin.com');
const io = require('socket.io')(server,
    {
    cors: { 
        origin: ["https://www.mydomain.com:*"], 
        methods: ["GET", "POST"],
            transports: ['websocket', 'polling'],
            autoConnect: true,
            pingInterval: 25000, 
            pingTimeout: 180000, 
    },
    allowEIO3: true,
    cookie: {
        name: "domainio",
        httpOnly: false,
        secure: true
      }
    });
client.js
var socket = io.connect('https://www.mydomain.com',
    {
        'transports': ['polling'],
        'autoConnect': true,
        'pingInterval': 25000, // default - 25000
        'pingTimeout': 180000, // default - 60000  
        'reconnection': true,
        'reconnectionDelay': 1000,
        'reconnectionDelayMax': 5000,
        'reconnectionAttempts': 5,
        'secure': true
    });
edited by MattIPv4

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hi @swapnilbaxi,

I’ll urge to you check your Nginx Error Logs, the error will most certainly be logged there. Once it’s logged there it will be a more specific one rather than just Generic 400 Error.

Once you have the specific error you can further troubleshoot what’s going on. Until tell it’s just some shots in the dark.

If you are unable to find a solution once you have the errors from the Nginx Error_log, you can post them here, we’ll try to help out!