Configuring Apache 2.4 for proxy

My question is how to configure Apache 2.4 for proxy operation.

I have a streamlit app that works correctly and is hosted at:
http://dashboard-aircare.mapshalli.org:8501/

I have configured Apache 2.4 proxy settings so that the app is available here:
http://dashboard-aircare.mapshalli.org/

I get the following error in console:
main.35aa1d35.chunk.js:1 WebSocket connection to 'ws://dashboard-aircare.mapshalli.org/stream' failed: Error during WebSocket handshake: Unexpected response code: 400

My Apache proxy setting is as follows:

 <VirtualHost *:80>
    	ServerName dashboard-aircare.mapshalli.org

     	ProxyPass / http://dashboard-aircare.mapshalli.org:8501/
       	ProxyPassReverse / http://dashboard-aircare.mapshalli.org:8501/

     	ProxyPass "/stream" "ws://dashboard-aircare.mapshalli.org:8501/stream"
       	ProxyPassReverse "/stream"  "ws://dashboard-aircare.mapshalli.org:8501/stream"
    	
    </VirtualHost>

Most of the answers in this forum point to a standard stackoverflow response regarding Apache proxy configuration. I am hoping to get a concrete answer that is specific to streamlit. Thank you!

1 Like

I had a very similiar and got this answer: see if it helps: Correct html content-type for Streamlit?

1 Like

Thank you! Following the path, I found the answer.
<VirtualHost *:80>
   ServerName dashboard-aircare.mapshalli.org

   RewriteEngine On
   RewriteCond %{HTTP:Upgrade} =websocket
   RewriteRule /(.*) ws://localhost:8501/$1 [P]
   RewriteCond %{HTTP:Upgrade} !=websocket
   RewriteRule /(.*) http://localhost:8501/$1 [P]
   ProxyPassReverse / http://localhost:8501
</VirtualHost>

5 Likes

No problem, just glad I could help. Just i could do the same and make my attempt work as well :stuck_out_tongue:

In your case, I believe you are viewing partial content because the websocket is not getting forwarded. streamlit is relying on websockets for transporting its data.

Now, I have the full configuration that works even over https
https://dashboard-aircare.mapshalli.org/

<VirtualHost *:80>
    RewriteEngine on
    RewriteCond %{SERVER_NAME} =dashboard-aircare.mapshalli.org
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

<VirtualHost *:443>
   ServerName dashboard-aircare.mapshalli.org

   RewriteEngine On
   RewriteCond %{HTTP:Upgrade} =websocket
   RewriteRule /(.*) ws://localhost:8501/$1 [P]
   RewriteCond %{HTTP:Upgrade} !=websocket
   RewriteRule /(.*) http://localhost:8501/$1 [P]
   ProxyPassReverse / http://localhost:8501

    SSLCertificateFile somepath
    SSLCertificateKeyFile someotherpath
</VirtualHost>

We are permanently directing from http:80 to https:443 and then proxying https:443 to http:8501 and ws:8501

4 Likes

Thank you so much for your help, but I’m afraid it still isn’t working. Like I said in my post, I’m fairly noob with these subjects, so I’m afraid I’m missing something basic.

This is the config I used:

<VirtualHost *:80>
RewriteEngine on
RewriteCond %{SERVER_NAME} =streamlit.group.pt
RewriteRule ^ http://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

<VirtualHost :443>
ServerName streamlit.group.pt
RewriteEngine On
RewriteCond %{HTTP:Upgrade} =websocket [NC]
RewriteRule /(.
) ws://streamlit.group.pt:8502/$1 [P,L]

RewriteCond %{HTTP:Upgrade} !=websocket [NC]
RewriteRule /(.*) http://streamlit.group.pt:8502/$1 [P,L]

ProxyPassReverse / http://streamlit.group.pt:8502

And the streamlit file is located at /var/www/example.com/html/, called streamlit_test.py, as I followed this tutorial to get me started in Apache configuration.

Also, the streamlit server was running at http://streamlit.group.pt:8502, by way of using:

streamlit run streamlit_test.py --server.port 8502 --browser.serverAddress streamlit.group.pt

As I tried to access the different websites, these were the results I got:
http://streamlit.group.pt/ - Error 503 Service Unavailable
http://streamlit.group.pt:8502 - ERR_CONNECTION_TIMED_OUT directy in the browser.
http://streamlit.group.pt/index.html - ERR_TOO_MANY_REDIRECTS directly in the browser.
http://streamlit.group.pt/streamlit_test.py - ERR_TOO_MANY_REDIRECTS directly in the browser.

I don’t want to bother you anymore with this, but any help would be appreciated as I’m completely at a loss here.

Can you please use the first solution?

<VirtualHost *:80>
   ServerName streamlit.group.pt

   RewriteEngine On

   RewriteCond %{HTTP:Upgrade} =websocket
   RewriteRule /(.*) ws://localhost:8502/$1 [P]
   RewriteCond %{HTTP:Upgrade} !=websocket
   RewriteRule /(.*) http://localhost:8502/$1 [P]

   ProxyPassReverse / http://localhost:8502
</VirtualHost>

Once this is done please post the messages that you see. If you can keep it running, I can have a look from my system.

Ok, using the following configuration:

<VirtualHost *:80>
ServerName streamlit.group.pt

RewriteEngine On

RewriteCond %{HTTP:Upgrade} =websocket
RewriteRule /(.) ws://localhost:8502/$1 [P]
RewriteCond %{HTTP:Upgrade} !=websocket
RewriteRule /(.
) http://localhost:8502/$1 [P]

ProxyPassReverse / http://localhost:8502
</ VirtualHost>

<VirtualHost :443>
ServerName streamlit.group.pt
RewriteEngine On
RewriteCond %{HTTP:Upgrade} =websocket [NC]
RewriteRule /(.
) ws://streamlit.group.pt:8502/$1 [P,L]

RewriteCond %{HTTP:Upgrade} !=websocket [NC]
RewriteRule /(.*) http://streamlit.group.pt:8502/$1 [P,L]

ProxyPassReverse / http://streamlit.group.pt:8502

</ VirtualHost>

these were the responses I got:
http://streamlit.group.pt/ - Error 503 Service Unavailable
http://streamlit.group.pt:8502 - ERR_CONNECTION_TIMED_OUT directy in the browser.
http://streamlit.group.pt/index.html - Error 503 Service Unavailable.
http://streamlit.group.pt/streamlit_test.py - Error 503 Service Unavailable.

I wish I could keep it running, but these links and servers are currently only working inside my company’s intranet, hence why I need to use a VPN. Sorry about that and once again, thanks for your time and help!

You may want to look at the apache error log file stored at /var/log/httpd/error_log

The required modules like mod_rewrite, mod_proxy, and mod_proxy_wstunnel may not be enabled.

I’m having troubles accesing those files due to file permissions :s

But I checked and i have those modules installed (used this tutorial).

Thank you for the script and i have changed the config file. The next question is when i run the Apache webserver, what will be the address given in the browser? Sorry because iam very new to hosting apps on webserver.

Hi everybody,

I have the same problem I need to run my streamlit application on my apache2 server directly.

Today I run the streamlit command run streamlit.py, it works perfectly. Now I would like to deploy this on my apache2 server, Do you have a concise and simple tutorial to do this?

thank you in advance for your interest.

Apache 2.4.48 on win10, streamlit runs well on 8502
I got “internal server error” on Chrome browser when accessing localhost:8083
<VirtualHost :8083>
ServerName localhost (also tried localhost:8083)
RewriteEngine On
RewriteCond %{HTTP:Upgrade} =websocket
RewriteRule /(.
) ws://localhost:8502/$1 [P]
RewriteCond %{HTTP:Upgrade} !=websocket
RewriteRule /(.*) http://localhost:8502/$1 [P]
ProxyPassReverse / http://localhost:8502

update: loaded 2 extra proxy modules in apache fixed above issue.
but drop to “please wait” for ever issue.
LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so (enabled for streamlit)
LoadModule proxy_module modules/mod_proxy.so (default loaded)
LoadModule proxy_http_module modules/mod_proxy_http.so (enabled for streamlit)

8502 has below settings as instructed, but not work.
–server.enableCORS=false
–server.enableXsrfProtection=false
–server.enableWebsocketCompression=false

1 Like

I have tried with this method and config file on my server which runs on ubuntu 20.04 with apche2.
it prints “please wait…” and nothing else. I literally tried every method on the internet to solve this. any help here?
how can I diagnose this?

thanks

Hi All,
Just would like to share some of my recent experience with streamlit.
I find that I could not find a way to proxy streamlit with apache2 on a VPS (ssh and Ubuntu 18).
Finally I end up using streamlit with nginx which work perfectly well now.

  1. I first stop apache by:
    sudo service apache2 stop
  2. Then, I followed this tutorial to set up a let’s encrypt SSL:
    Update: Using Free Let’s Encrypt SSL/TLS Certificates with NGINX - NGINX
  3. Finally, I followed this solution from agray:
    Streamlit, docker, Nginx, ssl/https

Hope these help.
thanks,