Error 502 (Bad Gateway) "_stcore/" streamlit in AWS using nginx

Hello anyone can help me in my streamlit in AWS using nginx, the website is displaying but I’m getting this error when running my code. Thank you

image

nginx.conf:

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

        client_max_body_size 20M;

        location / {
        proxy_pass      http://localhost:8501;
        }

        location ^~ /_stcore/static {
            proxy_pass http://localhost:8501/static;
            proxy_http_version 1.1;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header Sec-WebSocket-Extensions $http_sec_websocket_extentions;
            proxy_read_timeout 85400;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-Proto $scheme;
        }

        location ^~ /_stcore/healthz {
            proxy_pass http://localhost:8501/_stcore/healthz;
        }

        location ^~ /_stcore/vendor {
            proxy_pass http://localhost:8501/_stcore/vendor;
        }

        location /_stcore/stream {
            proxy_pass http://localhost:8501/_stcore/stream;
            proxy_http_version 1.1;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header Sec-WebSocket-Extensions $http_sec_websocket_extentions;
            proxy_read_timeout 85400;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-Proto $scheme;
        }

        root         /usr/share/nginx/html;

        include /etc/nginx/default.d/*.conf;

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }

Did you figure out a solution?

I looks like there was a similar thread in the past and @Caroline linked a working solution here.

I don’t think we have changed paths since then, so it should hopefully still work.

Hi, thanks for your reply, but unfortunaly, It still does not work. It still prints a lot of messages like this

nginx          | 192.168.120.253 - - [08/Sep/2024:06:07:58 +0000] "GET /_stcore/stream HTTP/1.1" 400 34 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36" "174.73.145.75"

Here is the config of nginx that I copied and pasted

server {
    server_name website.com;

    location / {
                proxy_pass http://streamlit_app: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_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
    }

        location /_stcore/stream {
                proxy_pass http://streamlit_app:8501/_stcore/stream;
                proxy_http_version 1.1;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $host;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_read_timeout 86400;
        }

}

Hey @Chuong , sorry to hear that it still does not work! Without knowing the specifics of your deployment environment, it is pretty difficult to debug and support this.
If I were you, I would probably add a simple, static route to nginx like

location /hello {
  default_type text/plain;
  echo "hello, world!";
}

and see whether you can hit that endpoint successfully to figure out at which level the issue happens.

hey @raethlein i used same config as shown in the above shared solution. and added this line to the config

       location /hello {
                root /var/www/html;
                try_files /hello.html =404;
       }

and its working fine and rendering the html page.
but the main app is showing the skeleton ui , with nginx access log as :
“GET /_stcore/stream HTTP/1.1” 400 34 “-” “Mozilla/5.0 …”.