Nginx setup WebSocket connection error with a subdomain name

Summary

  • I’m trying to host my streamlit service on a subdomain name of my domain on my Ubuntu server using Nginx via http not https.
  • No docker is involved.
  • I got “WebsocketConnection WebSocket onerror” and “WebSocket connection to ‘wss://xxxx.xxxxx.com/_stcore/stream’ failed: (anonymous) @ main.ac67fab5.js:2” from Console
  • I tried all the solutions in the forum with keyword “Nginx” but non worked so far.
  • My streamlit service work nicely on the port 12345 of my server, but doesn’t work on my domain set up with Nginx.
  • Thanks in advance for any suggestions!

Code

My nginx.conf:

        server {
                listen 80;
                server_name xxxx.xxxxx.com;
                location / {
                        proxy_pass http://127.0.0.1:12345/;
                       }
                location /streamlit {
                        proxy_pass http://127.0.0.1:12345/streamlit;
                        proxy_http_version 1.1;
                        proxy_set_header Upgrade $http_upgrade;
                        proxy_set_header Connection "upgrade";
                        }
        }

Console output:

Source of error

maybe it’s because you are trying to upgrade the connection? It looks like it remains HTTP the whole time and never goes to HTTPS – just spitballing

My setup for elastic beanstalk has a few more arguments, not sure whether they are actually needed or not:

    location / {
      proxy_pass  http://127.0.0.1:8501;
      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-Proto $scheme;
      proxy_read_timeout 600;

I have the same problem, have you solved it?

exactly same problem for me…anyone solved it?

I fixed it. It’s actually very simple.
Nginx needs to be configured to route the websocket connection , aka ‘stream’.
To do this you need to ADD something like this to nginx conf:

 location /stream {
      proxy_pass  http://127.0.0.1:8501/stream;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.