Webscoket errors while proxying through HTTPS with apache2

Hello everyone!

I’m trying to deploy streamlit app on my VPS. Server thst’s in use is Apache2 with SSL certificates generated by certbot.

Streamlit app works well on http://localhost:8501/, as I want to make it public and safe with HTTPS, I’m using apache2 proxy. The configuration is as follows:

<IfModule mod_ssl.c>
<VirtualHost *:443>
    ServerName my.server-name.com
    #RewriteCond %{SERVER_NAME} =my.server-name.com

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

    ProxyPassReverse / http://localhost:8501/

SSLCertificateFile /path/to/cert/
SSLCertificateKeyFile /path/to/key/
</VirtualHost>
</IfModule>

Apache2 shows no errors whatsoever. When I try to access https://my.server-name.com/. Browser properly uses HTTPS with certs, I’m also redirected to streamlit app, BUT it’s stuck on:

“Please wait…”

When I look at the browser console (F12) it shows followsing errors:

WebsocketConnection WebSocket onerror

and:

Uncaught Error: Unsupported state transition.
State: PINGING_SERVER
Event: CONNECTION_TIMED_OUT

and general information:

WebSocket connection to 'wss://my.server-name.com/stream' failed: WebSocket is closed before the connection is established.

Is there anyting that I should pay attention to? The configuration was taken from streamlit deployment guide. I would be glad for any guidance or suggestions.