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!

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>

3 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

1 Like

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.