Errors while trying to deploy a Streamlit app with Nginx

Hello everyone! I am trying to deploy a streamlit application on Linux (Ubuntu 18.04.4) machine. I am not deploying it via docker, I am just editing Nginx configuration file at /etc/nginx/nginx.conf. Here is a full file:

events {
	worker_connections 768;
	# multi_accept on;
}

http{
    server {
        listen 80;
        listen 443 ssl;

        server_name ar-hand-api-stg.centralus.cloudapp.azure.com;
        index index.php index.html index.htm;

        ssl_certificate /etc/nginx/ssl/domain-crt.crt;
        ssl_certificate_key /etc/nginx/ssl/domain-key.key;

		ssl_protocols       TLSv1.2;

        ssl_session_cache    shared:SSL:10m;
        ssl_session_timeout  10m;

        ssl_ciphers  AES256-SHA;
        ssl_prefer_server_ciphers  on;
		
		client_max_body_size 2G;

        # Streamlit server
        location /dashboard {
            proxy_pass http://localhost:8501/dashboard;
        }
        location ^~ /static {
            proxy_pass http://127.0.0.1:8501/dashboard/static;
        }
        location ^~ /healthz {
            proxy_pass http://127.0.0.1:8501/dashboard/healthz;
        }
        location ^~ /vendor {
            proxy_pass http://127.0.0.1:8501/dashboard/vendor;
        }
        location /dashboard/stream {
            proxy_pass http://localhost:8501/dashboard/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;
        }
    }
}

I am running Streamlit via the next command:

streamlit run streamlit_app.py --server.port 8501 --server.baseUrlPath /dashboard/ --server.enableCORS false --server.enableXsrfProtection false --server.headless=true

And then I am trying to connect to my web app via the next link: https://ar-hand-api-stg.centralus.cloudapp.azure.com/dashboard/

This result are next errors in the terminal (I also attach a screenshot):

Unchecked runtime.lastError: The message port closed before a response was received.

ar-hand-api-stg.centralus.cloudapp.azure.com/:1          GET https://ar-hand-api-stg.centralus.cloudapp.azure.com/dashboard/vendor/bokeh/bokeh-2.4.1.min.js net::ERR_INCOMPLETE_CHUNKED_ENCODING 200 (OK)

bokeh-widgets-2.4.1.min.js:38 Uncaught TypeError: bokeh.register_plugin is not a function
    at bokeh-widgets-2.4.1.min.js:38:20
    at bokeh-widgets-2.4.1.min.js:43:1
    at bokeh-widgets-2.4.1.min.js:32:3
    at bokeh-widgets-2.4.1.min.js:33:3

ar-hand-api-stg.centralus.cloudapp.azure.com/:1          GET https://ar-hand-api-stg.centralus.cloudapp.azure.com/dashboard/static/js/5.df97478a.chunk.js net::ERR_INCOMPLETE_CHUNKED_ENCODING 200 (OK)

Can anyone please help me here? I’ve already tried a dozen of different Nginx and Streamlit configurations, nothing really works. I will really appreciate any help here!

PS I’ve seen some similar topics, I’ve taken configuration from there. Still can’t deploy my app, it just doesn’t load.

I resolved the issue and successfully deployed a Streamlit application with Nginx server with the conf file above.

The problem was in the permission for Nginx, it couldn’t enter /var/lib/nginx/ folder.

A symptom of this problem can be found in log file for Nginx. if something like:

2022/09/06 10:45:51 [crit] 29192#29192: *6 open() "/var/lib/nginx/proxy/3/00/0000000003" failed (13: Permission denied) while reading upstream, client: 46.53.249.156, server: ar-hand-api-stg.centralus.cloudapp.azure.com, request: "GET /dashboard/static/js/5.df97478a.chunk.js HTTP/1.1", upstream: "http://127.0.0.1:8501/dashboard/static/js/5.df97478a.chunk.js", host: "ar-hand-api-stg.centralus.cloudapp.azure.com", referrer: "https://ar-hand-api-stg.centralus.cloudapp.azure.com/dashboard/"

If this is the case it might be resolved with the next commands:

sudo chown -R www-data:www-data /var/lib/nginx/

or

sudo chmod -R 777 www-data:www-data /var/lib/nginx/

These commands should give nginx required permissions. Then everything works as intended