How to use Streamlit with Nginx?

Hi guys,

Thanks for created great product.
I’m setting up Nginx proxy front of Streamlit, but unsuccessful.
The error in browser: “WebSocket connection to ‘ws…stream’ failed: Error during WebSocket handshake: Unexpected response code: 403”

My application is listing streamlit’s components
http://13.114.97.39:8501 => work
http://streamlit.fullstackstation.com => not work

3 Likes

I resolved this problem by disable CORS
`

The value below was set in ~/.streamlit/config.toml

enableCORS = false
`

2 Likes

@virusvn

Awesome!

I have added you to the awesome-streamlit and the awesome-streamlit App.

Thanks for sharing. Hope it’s ok to share on the awesome-streamlit list?

3 Likes

@virusvn This looks great, thanks for sharing with the community!

2 Likes

I am trying to forward port 80 to 8501 but could not. Any idea ?

Hi @moh555m555 – Setting up port forwarding can be a frustrating process, especially on cloud servers.

There are several webservers that can accomplish what you need. I often find Apache2 to be easier to set up for simple cases.

See my response on your earlier question: Permission denied in ec2 port 80

This is my nginx configuration for setup streamlit at subpath https://fullstackstation.com/streamlit-components-demo:

location /streamlit-components-demo {
    proxy_pass http://127.0.0.1:8501/;
}
location ^~ /static {
    proxy_pass http://127.0.0.1:8501/static/;
}
location ^~ /healthz {
    proxy_pass http://127.0.0.1:8501/healthz;
}
location ^~ /vendor {
    proxy_pass http://127.0.0.1:8501/vendor;
}
location /stream { # most important config
    proxy_pass http://127.0.0.1:8501/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;
}

For subdomain, just setup the /stream part is enough.
Hope this help.

11 Likes

Hi virusvn,

Do you have a reproducible example using streamlit hello?

I can’t seem to get the websockets connection even with your nginx.conf.

WebSocket connection to 'ws://SOME_DOMAIN:8501/stream' failed: WebSocket is closed before the connection is established.

Actually, I was able to make it work. I was using port 3000 to test, which is I think privileged for development.

2 Likes

Thanks for the information here @virusvn.

This just about works for me except that: if I want the app to be hosted at /myapp, I find that streamlit looks for /myapp/stream and /myapp/healthz, but looks for static items at an absolute /static URL.

This is probably by design but it could easily conflict with /static based on the main site.

The sample nginx configuration given above implies that you are seeing something different - stream accessed at /stream instead of /streamlit-components-demo/stream (although my nginx knowledge is limited), so maybe I’m missing some other configuration you’ve already set.

Is there a configuration option in streamlit asking for static assets to be fetched from a relative URL, i.e. /myapp/static in my case?

1 Like

@tokestermw Hi could you share some tips that made it work for you? My app is also encountering serious slow downs with Nginx using the same configs discussed here. WebSocket is closed before the connection is established
When I access the server directly using IP:8501, the app loads instantly but accessing through domain name and Nginx just keeps the app on Connecting... Please wait

@Anh_Chu Having the same problems as you do. Any ideas?

I’m using a reverse proxy with Nginx based on @virusvn recommendation on configuration: if I visit using IP:<streamlit-port> everything works just fine, if I visit going through Nginx IP/app (where I redirect to IP:<streamlit-port> with Nginx, it succesfully redirects, but streamlit remains stuck in Connecting... Please Wait. Nginx logs shows 403 forbidden errors on /healthz.

I’m using streamlit 0.56, and using Docker with docker-compose and 2 services: one container to deploy the app, and one container for nginx. They are both part of the network created by default, so callable by their service name.

Update:

The nginx configuration here helped me: Host streamlit app at a subfolder . The problem was that I was redirecting IP/app to IP:<streamlit-port> but I was doing so for the stream endpoint without prefixing with app. I set the location block to /app/stream/ (like in the list of the answer), and it worked.

2 Likes

Glad you resolved it. I think my problem is simply a DNS resolver problem on the WebSocket because end users of my app actually report spotless performance haha. Using the same settings as you do.

2 Likes

You can combine with the --server.baseUrlPath config option to make your nginx setup simpler (because everything will be nested inside your subfolder)

Here’s what I’ve been using for instance:

location /nlp/viewer/ {
	proxy_pass         http://34.70.xxx.yyy:8000/nlp/viewer/;
	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_buffering    off;
	proxy_http_version 1.1;
	# Also requires websocket:
	proxy_set_header Upgrade $http_upgrade;
	proxy_set_header Connection "upgrade";
	proxy_read_timeout 86400;
}
7 Likes

Thank you very much. Could you please also provide equivalent config for Apache? I’d really appreciate it as I know very little about networking. Thanks!

When I do that, I cannot start streamlit:
Traceback (most recent call last):
File “/home/ubuntu/miniconda3/envs/ms-mint/bin/streamlit”, line 5, in
from streamlit.cli import main
File “/home/ubuntu/miniconda3/envs/ms-mint/lib/python3.9/site-packages/streamlit/init.py”, line 76, in
from streamlit.delta_generator import DeltaGenerator as _DeltaGenerator
File “/home/ubuntu/miniconda3/envs/ms-mint/lib/python3.9/site-packages/streamlit/delta_generator.py”, line 18, in
from streamlit import caching
File “/home/ubuntu/miniconda3/envs/ms-mint/lib/python3.9/site-packages/streamlit/caching.py”, line 40, in
from streamlit.hashing import Context
File “/home/ubuntu/miniconda3/envs/ms-mint/lib/python3.9/site-packages/streamlit/hashing.py”, line 39, in
from streamlit.folder_black_list import FolderBlackList
File “/home/ubuntu/miniconda3/envs/ms-mint/lib/python3.9/site-packages/streamlit/folder_black_list.py”, line 39, in
if config.get_option(“global.developmentMode”):
File “/home/ubuntu/miniconda3/envs/ms-mint/lib/python3.9/site-packages/streamlit/config.py”, line 86, in get_option
parse_config_file()
File “/home/ubuntu/miniconda3/envs/ms-mint/lib/python3.9/site-packages/streamlit/config.py”, line 991, in parse_config_file
_update_config_with_toml(file_contents, filename)
File “/home/ubuntu/miniconda3/envs/ms-mint/lib/python3.9/site-packages/streamlit/config.py”, line 910, in _update_config_with_toml
for name, value in options.items():
AttributeError: ‘bool’ object has no attribute ‘items’

@Soren , your issue is likely in the streamlit config file in ~/.streamlit/config.toml. It is possible it is not formatted as a toml file, thus the parser fails to read it resulting in your error. See this for how to format the config properly: Streamlit configuration — Streamlit 0.75.0 documentation

This will help you to deploy Streamlit app on Nginx: Deploy Streamlit app on Nginx | by Bala Murugan N G | Medium

1 Like

@ngbala6 welcome to the Community, I read your post through the link you shared and I must say it was superb and I recommend anyone having a challenge setting up Nginx

Thanks! It works!