<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:media="http://search.yahoo.com/mrss/"><channel><title>Python package on IT Space</title><link>https://blog.jiatool.com/en/series/Python-package/</link><description>Recent content in Python package on IT Space</description><generator>Hugo -- gohugo.io</generator><language>en</language><managingEditor>jia@jiatool.com (Jia)</managingEditor><webMaster>jia@jiatool.com (Jia)</webMaster><copyright>&amp;copy;{year}, Jia All Rights Reserved</copyright><lastBuildDate>Sat, 09 Nov 2024 20:40:00 +0800</lastBuildDate><atom:link href="https://blog.jiatool.com/en/series/Python-package/index.xml" rel="self" type="application/rss+xml"/><item><title>Deploying Streamlit with Nginx on a Custom URL Path</title><link>https://blog.jiatool.com/en/posts/streamlit_nginx/</link><pubDate>Sat, 09 Nov 2024 20:40:00 +0800</pubDate><author>jia@jiatool.com (Jia)</author><atom:modified>Sat, 09 Nov 2024 20:40:00 +0800</atom:modified><guid>https://blog.jiatool.com/en/posts/streamlit_nginx/</guid><description>(This article was translated by AI and then reviewed by a human.) Preface After creating a web app using Streamlit , aside from deploying it on Streamlit Community Cloud, we also have the option to self-host it (for example, using Docker or Kubernetes). * Previous post: (2023年新版) 使用 Streamlit 簡單又快速建立 Dashboard 網頁 I've recently</description><content:encoded>&lt;p>&lt;em>(This article was translated by AI and then reviewed by a human.)&lt;/em>&lt;/p>
&lt;h2 id="preface">Preface&lt;/h2>
&lt;p>After creating a web app using &lt;a href="https://streamlit.io/" target="_blank" rel="noopener">
Streamlit
&lt;/a>, aside from deploying it on Streamlit Community Cloud, we also have the option to self-host it (for example, using Docker or Kubernetes).&lt;/p>
&lt;p>* Previous post: &lt;a href="https://blog.jiatool.com/posts/streamlit_2023" target="_blank" rel="noopener">
(2023年新版) 使用 Streamlit 簡單又快速建立 Dashboard 網頁
&lt;/a>&lt;/p>
&lt;br/>
&lt;p>I've recently had a need to set up a reverse proxy using Nginx, but instead of routing to the root path (&lt;code>/&lt;/code>), I wanted to specify a different path (like &lt;code>/myapp&lt;/code>).&lt;/p>
&lt;p>However, after going through several online articles and still struggling with the setup, I'm sharing the final successful configuration here to help others avoid similar issues.&lt;/p>
&lt;br/>
&lt;!--adsense-->
&lt;br/>
&lt;h2 id="setup-details">Setup Details&lt;/h2>
&lt;p>I needed to configure Nginx so that when another computer connects to a specific path on my host machine (e.g., &lt;code>https://mysite.com/myapp&lt;/code>), it forwards the request to the Streamlit app's port (e.g., &lt;code>http://localhost:8000&lt;/code>).&lt;/p>
&lt;br/>
&lt;h3 id="environment">Environment&lt;/h3>
&lt;p>I'm running a Docker environment within WSL (Linux subsystem) on Windows 11, where my Streamlit app is running.&lt;/p>
&lt;p>I also ran an Nginx server (v1.24.0) on Windows so that other computers within my company can connect to my localhost.&lt;/p>
&lt;br/>
&lt;h3 id="nginx-configuration">Nginx Configuration&lt;/h3>
&lt;p>Since Streamlit uses WebSocket, a few extra configurations were needed.&lt;/p>
&lt;blockquote>
&lt;p>WebSocket is a kind of network protocol used to create a &amp;ldquo;persistent, two-way communication&amp;rdquo; channel between a server and a client. It&amp;rsquo;s a continuous connection where the server can actively send data to the client without having to wait for a request from the client.&lt;br />
This makes WebSocket especially suitable for real-time applications, such as instant messaging and online games.&lt;/p>
&lt;/blockquote>
&lt;br/>
&lt;p>Edit the &lt;code>nginx.conf&lt;/code> file, which can typically be found in the &lt;code>nginx installation path&lt;/code>, &lt;code>/usr/local/nginx/conf&lt;/code>, &lt;code>/etc/nginx&lt;/code>, or &lt;code>/usr/local/etc/nginx&lt;/code>.&lt;/p>
&lt;pre>&lt;code>http {
...
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 80;
listen 443 ssl;
ssl_certificate &amp;quot;(ellipsis)/conf/certs/demo.pem&amp;quot;;
ssl_certificate_key &amp;quot;(ellipsis)/conf/certs/demo.key&amp;quot;;
# Streamlit app
location /myapp {
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
}
}
&lt;/code>&lt;/pre>&lt;br/>
&lt;p>I also found another way to set up the &lt;code># Streamlit app&lt;/code> part, which can achieve the same result:&lt;/p>
&lt;pre>&lt;code> location /myapp/_stcore/stream {
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
location /myapp {
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
&lt;/code>&lt;/pre>&lt;br/>
&lt;h3 id="streamlit-command">Streamlit command&lt;/h3>
&lt;p>This is a crucial part. Many articles focus on Nginx configuration but don't mention the necessary parameters for the &lt;code>streamlit run&lt;/code> command:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre class="chroma">&lt;code class="language-shell" data-lang="shell">streamlit run streamlit.py --server.port &lt;span class="m">8000&lt;/span> --server.enableCORS &lt;span class="nb">false&lt;/span> --server.enableXsrfProtection &lt;span class="nb">false&lt;/span> --server.baseUrlPath &lt;span class="s2">&amp;#34;myapp&amp;#34;&lt;/span>
&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;br/>
&lt;br/>
&lt;p>As a side note, you can disable sending usage statistics to Streamlit:&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre class="chroma">&lt;code class="language-shell" data-lang="shell">--browser.gatherUsageStats &lt;span class="nb">false&lt;/span>
&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;br/>
&lt;!--adsense-->
&lt;br/>
&lt;h2 id="conclusion">Conclusion&lt;/h2>
&lt;p>I'm not sure if this is the best configuration, but at least it works for me (🤣.&lt;/p>
&lt;br/>
&lt;p>I've listed a few reference articles below. If the above steps don't help, you can check these links for more information.&lt;/p>
&lt;br/>
&lt;br/>
&lt;hr />
&lt;p>References:&lt;br />
&lt;a href="https://docs.streamlit.io/develop/api-reference/configuration/config.toml" target="_blank" rel="noopener">
config.toml | Streamlit API reference
&lt;/a>&lt;br />
&lt;a href="https://discuss.streamlit.io/t/how-to-use-streamlit-with-nginx/378" target="_blank" rel="noopener">
How to use Streamlit with Nginx? | Streamlit Discuss
&lt;/a>&lt;br />
&lt;a href="https://discuss.streamlit.io/t/deploy-streamlit-with-nginx-with-ssl/61770" target="_blank" rel="noopener">
Deploy Streamlit with NGINX with SSL | Streamlit Discuss
&lt;/a>&lt;br />
&lt;a href="https://discuss.streamlit.io/t/deploy-streamlit-with-nginx-docker/52907" target="_blank" rel="noopener">
Deploy streamlit with nginx + docker | Streamlit Discuss
&lt;/a>&lt;br />
&lt;a href="https://discuss.streamlit.io/t/run-streamlit-app-on-specific-url-path/32147" target="_blank" rel="noopener">
Run streamlit app on specific URL path | Streamlit Discuss
&lt;/a>&lt;/p>
&lt;br/>
&lt;blockquote>
&lt;p>平靜的海面造就不出優秀的水手。&lt;/p>
&lt;/blockquote></content:encoded><dc:creator>Jia</dc:creator><media:content url="https://blog.jiatool.comimages/cover/streamlit_nginx.jpg" medium="image"><media:title type="html">featured image</media:title></media:content><media:content url="https://blog.jiatool.comimages/posts/streamlit_nginx_meta.jpg" medium="image"><media:title type="html">meta image</media:title></media:content><category>Streamlit</category><category>Nginx</category><category>Share</category><category>Python package</category></item></channel></rss>