Streamlit + Nginx: WebsocketConnection WebSocket onerror / recv() failed (104: Unknown error)

Hello community,

I have issues setting up a Streamlit app that runs in a docker container behind a Nginx reverse proxy. I believe I read all the threads that exist on the topic - still I wasn’t able to fix my problem so far…

The error I get on the frontend is:

WebsocketConnection WebSocket onerror

The screen remains blank and nothing shows.

In the Nginx logs I see this:

2024/02/16 17:08:24 [error] 118768#118768: *5 recv() failed (104: Unknown error) while reading response header from upstream, client: ***, server: xyz.org, request: "GET /_stcore/host-config HTTP/1.1", upstream: "http://127.0.0.1:58798/_stcore/host-config", host: "xyz.org", referrer: "http://xyz.org/"

It works by adding the port to the domain like http://xyz.org:58798

My Nginx config:

server {
    listen 80;
    listen [::]:80;

    server_name xyz.org www.xyz.org;
	location / {
			proxy_pass http://127.0.0.1:58798/;
			proxy_http_version 1.1;
			proxy_redirect off;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_set_header Host $http_host;
			proxy_set_header Upgrade $http_upgrade;
			proxy_set_header Connection "upgrade";
			proxy_read_timeout 86400;
   }
    location /static {
            proxy_pass http://127.0.0.1:58798/static/;
			proxy_http_version 1.1;
		   proxy_set_header Upgrade $http_upgrade;
		   proxy_set_header Connection "upgrade";
    }
    location /vendor {
            proxy_pass http://127.0.0.1:58798/vendor;
    }

    location /_stcore/health {
            proxy_pass http://127.0.0.1:58798/_stcore/health;
    }

    location /_stcore/allowed-message-origins {
            proxy_pass http://127.0.0.1:58798/_stcore/allowed-message-origins;
    }

    location /_stcore/stream {
            proxy_pass http://127.0.0.1:58798/_stcore/stream;
            proxy_http_version 1.1;
            proxy_redirect off;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_read_timeout 86400;
    }
}

My Streamlit config:

[theme]
primaryColor="#1474d3"
textColor="#000000"

[server]
port=58798
runOnSave=true
enableCORS=false

[logger]
level="debug"
messageFormat = "%(asctime)s %(message)s"

Any help and hints will be much appreciated!
Dorian

Hello community,

after readings tons of threads and posts, I finally found a NGINX config [1] that works for me. I would like to share it with you and hope, that this might helpful for somebody:

upstream app{                                                               
  server 127.0.0.1:58798;                                                         
}                                                                                 
                                                                                
server {                                                                          
  listen 80;                                                                      
  server_name xyz.org www.xyz.org;              
                                                                                  
  location / {                                                                    
     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;                                        
                                                                                  
      proxy_pass http://app;                                                 
      proxy_redirect off;                                                         
                                                                                  
      proxy_http_version 1.1;                                                     
      proxy_set_header Upgrade $http_upgrade;                                     
      proxy_set_header Connection "upgrade";                                      
  }                                                                               
} 

1: Stackoverflow

1 Like

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