0

Flask socket.io not working on live website which uses gunicorn and nginx. Code works perfectly with gunicorn on host machine

Nginx Config below:

server {

    listen 80;
    listen 443 ssl;
    server_name domain_name;
    ssl_certificate /home/azureuser/code/proj/cert.pem;
    ssl_certificate_key /home/azureuser/code/proj/key.pem;
    
    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/azureuser/code/proj;
    }
    
    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
    
    location /socket.io {
        include proxy_params;
        proxy_http_version 1.1;
        proxy_buffering off;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_pass http://127.0.0.1:443/socket.io;
    }
}

Browser throws 400 error for /socket.io requests. Any Suggestions?

gre_gor
  • 6,669
  • 9
  • 47
  • 52
Nikhil
  • 1

1 Answers1

0

Change your location parameter to:

location ^~ /socket {
    rewrite  ^/socket/(.*)  /$1 break; #used to send request to base url
    proxy_pass http://127.0.0.1:443;
    proxy_redirect off;
    proxy_pass_request_headers on;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;
    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_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
}

Refer to nginx as webserver incl. socket.io and node.js / ws:// 400 Bad Request for more detail

karel
  • 5,489
  • 46
  • 45
  • 50
Angad
  • 1