Click here to Skip to main content
15,867,895 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I host a flask app in Ubuntu 18.04 LTS with Nginx and Gunicorn. I use Flask-SocketIO==5.0.1 for backend and "https://cdn.socket.io/3.1.3/socket.io.min.js" for front end. here is my server configuration:

server {
    server_name toollaza.com;

    location /static {
        alias /home/ubuntu/toollaza/video5/static;
    }

    location / {
        proxy_pass http://localhost:8000;
        include /etc/nginx/proxy_params;
        proxy_redirect off;
    }

    location /socket.io {
        include proxy_params;
        proxy_http_version 1.1;
        proxy_buffering off;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_pass http://localhost:8000/socket.io;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/toollaza.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/toollaza.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}
server {
    if ($host = toollaza.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    server_name toollaza.com;
    return 404; # managed by Certbot


}


But the problem is browser can not connect with the server via socket. Here are some errors that I found in the browser console:

Failed to load resource: the server responded with a status of 400 (BAD REQUEST)
WebSocket connection to '<URL>' failed: WebSocket is closed before the connection is established.
22WebSocket connection to '<URL>' failed: WebSocket is closed before the connection is established.
WebSocket connection to 'wss://toollaza.com/socket.io/?EIO=4&transport=websocket&sid=hqBln0W4o4QOXAtMAAAB' failed: WebSocket is closed before the connection is established.
GET https://toollaza.com/socket.io/?EIO=4&transport=polling&t=NjISAoG&sid=jDAmKGDdhq6xxiSYAAAC 400 (BAD REQUEST)
polling-xhr.js:206 POST https://toollaza.com/socket.io/?EIO=4&transport=polling&t=NjISDgF&sid=Qlz9s9KQZ9QTUMDUAAAB 400 (BAD REQUEST)



Where is the problem?

Thank you

What I have tried:

server {
    server_name toollaza.com;

    location /static {
        alias /home/ubuntu/toollaza/video5/static;
    }

    location / {
        proxy_pass http://localhost:8000;
        include /etc/nginx/proxy_params;
        proxy_redirect off;
    }

    location /socket.io {
        include proxy_params;
        proxy_http_version 1.1;
        proxy_buffering off;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_pass http://localhost:8000/socket.io;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/toollaza.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/toollaza.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}
server {
    if ($host = toollaza.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    server_name toollaza.com;
    return 404; # managed by Certbot


}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900