0

Streamlit app

I have a streamlit app running on port 8501:

https://dharmatech.dev:8501/fed-net-liquidity/

enter image description here

Here's how I'm starting the app:

streamlit run \
        --browser.serverAddress dharmatech.dev \
        --browser.serverPort 8501 \
        --server.sslCertFile /home/dharmatech/fullchain.pem \
        --server.sslKeyFile /home/dharmatech/privkey.pem \
        --server.baseUrlPath fed-net-liquidity \
        Fed_Net_Liquidity.py

Apache configuration

Here's the contents of /etc/apache2/sites-available/dharmatech.dev-le-ssl.conf:

<IfModule mod_ssl.c>
<VirtualHost *:443>
    ServerAdmin [email protected]
    ServerName dharmatech.dev
    ServerAlias www.dharmatech.dev
    DocumentRoot /var/www/dharmatech.dev
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    Include /etc/letsencrypt/options-ssl-apache.conf
    SSLCertificateFile /etc/letsencrypt/live/dharmatech.dev/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/dharmatech.dev/privkey.pem

    RewriteEngine On
    RewriteCond %{HTTP:Upgrade} =websocket [NC]
    RewriteRule /fed-net-liquidity/(.*)         wss://dharmatech.dev:8501/fed-net-liquidity/$1 [P,L]

    RewriteCond %{HTTP:Upgrade} !=websocket [NC]
    RewriteRule /fed-net-liquidity/(.*)           https://dharmatech.dev:8501/fed-net-liquidity/$1 [P,L]

    SSLProxyEngine on
    #ProxyPass        /fed-net-liquidity https://dharmatech.dev:8501/fed-net-liquidity
    ProxyPassReverse /fed-net-liquidity https://dharmatech.dev:8501/fed-net-liquidity


    #ProxyPass        /fed-net-liquidity wss://dharmatech.dev:8501/fed-net-liquidity
    ProxyPassReverse /fed-net-liquidity wss://dharmatech.dev:8501/fed-net-liquidity


</VirtualHost>
</IfModule>

Results

When I go to:

https://dharmatech.dev/fed-net-liquidity/

here's the result:

enter image description here

Question

What's a good way to set up the proxy so that

https://dharmatech.dev/fed-net-liquidity/

results in the app being properly rendered?

apache2ctl -S output:

$ sudo apache2ctl -S
VirtualHost configuration:
*:443                  dharmatech.dev (/etc/apache2/sites-enabled/dharmatech.dev-le-ssl.conf:2)
*:80                   dharmatech.dev (/etc/apache2/sites-enabled/dharmatech.dev.conf:1)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex ssl-stapling: using_defaults
Mutex proxy: using_defaults
Mutex ssl-cache: using_defaults
Mutex default: dir="/var/run/apache2/" mechanism=default
Mutex watchdog-callback: using_defaults
Mutex rewrite-map: using_defaults
Mutex ssl-stapling-refresh: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33
Group: name="www-data" id=33
6
  • 1
    Your backend server sends the redirect. Configure it to not do that. Usually you can configure a base URL. Commented May 11 at 5:14
  • Hi @GeraldSchneider 🙋‍♂️. By backend server, do you mean the streamlit application on port 8105? I checked the command-line options for it and I didn't see one that seems to be related to redirection.
    – dharmatech
    Commented May 11 at 7:36
  • @GeraldSchneider I think one thing I may need to update the config for websockets. I've added a section to the question on that. I'm now getting a slightly different result.
    – dharmatech
    Commented May 11 at 7:36
  • 1
    You can configure your streamlit server with a base URL with config.toml: docs.streamlit.io/develop/api-reference/configuration/… Commented May 11 at 9:05
  • 1
    For websocket proxy, see httpd.apache.org/docs/2.4/mod/mod_proxy.html#wsupgrade Commented May 11 at 15:52

2 Answers 2

1

i try quick answer ...

change streamlit host to local all https will be handle by apache
streamlit run
--browser.serverAddress localhost
--browser.serverPort 8501
Fed_Net_Liquidity.py

Using localhost .

change reverse proxy :

RewriteRule /fed-net-liquidity/(.*)         wss://localhost:8501/fed-net-liquidity/$1 [P,L]
RewriteCond %{HTTP:Upgrade} !=websocket [NC]
RewriteRule /fed-net-liquidity/(.*)           http://localhost:8501/fed-net-liquidity/$1 [P,L]
SSLProxyEngine on

#ProxyPass        /fed-net-liquidity http://localhost:8501/fed-net-liquidity

ProxyPassReverse /fed-net-liquidity http://localhost:8501/fed-net-liquidity

#ProxyPass        /fed-net-liquidity wss://localhost:8501/fed-net-liquidity

ProxyPassReverse /fed-net-liquidity wss://localhost:8501/fed-net-liquidity                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

</VirtualHost>

</IfModule>

so let me know when can help opzteams

11
  • Hey Ops team 🙋‍♂️. I've setup a similar config and updated the question with the results.
    – dharmatech
    Commented May 11 at 9:38
  • can change to localhost without https
    – Ops team
    Commented May 11 at 9:40
  • The streamlit server is running on https.
    – dharmatech
    Commented May 11 at 9:41
  • 1
    can change streamlit to working http since all https will be handle apache , except you open port 8501 but will be looping
    – Ops team
    Commented May 11 at 9:43
  • 1
    just like this [streamlit] (streamlit.aichat.host) @dharmatech
    – Ops team
    Commented May 11 at 14:17
0

The key was to add --server.enableCORS false when running streamlit.

Here's how I'm starting streamlit:

streamlit run \
        --browser.serverPort 8501 \
        --server.sslCertFile /home/dharmatech/fullchain.pem \
        --server.sslKeyFile /home/dharmatech/privkey.pem \
        --server.baseUrlPath fed-net-liquidity \
        --server.enableCORS false \
        Fed_Net_Liquidity.py

The apache configuration file located at:

/etc/apache2/sites-available/dharmatech.dev-le-ssl.conf

contains:

<IfModule mod_ssl.c>
<VirtualHost *:443>
    ServerAdmin [email protected]
    ServerName dharmatech.dev
    ServerAlias www.dharmatech.dev
    DocumentRoot /var/www/dharmatech.dev
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    Include /etc/letsencrypt/options-ssl-apache.conf
    SSLCertificateFile /etc/letsencrypt/live/dharmatech.dev/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/dharmatech.dev/privkey.pem

    RewriteEngine On
    RewriteCond %{HTTP:Upgrade} =websocket [NC]
    RewriteRule /fed-net-liquidity/(.*)         wss://dharmatech.dev:8501/fed-net-liquidity/$1 [P,L]

    RewriteCond %{HTTP:Upgrade} !=websocket [NC]
    RewriteRule /fed-net-liquidity/(.*)           https://dharmatech.dev:8501/fed-net-liquidity/$1 [P,L]

    SSLProxyEngine on
    #ProxyPass        /fed-net-liquidity https://dharmatech.dev:8501/fed-net-liquidity
    ProxyPassReverse /fed-net-liquidity https://dharmatech.dev:8501/fed-net-liquidity


    #ProxyPass        /fed-net-liquidity wss://dharmatech.dev:8501/fed-net-liquidity
    ProxyPassReverse /fed-net-liquidity wss://dharmatech.dev:8501/fed-net-liquidity


</VirtualHost>
</IfModule>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .