You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Nginx反向代理两个Streamlit应用出现403/404错误求助

问题描述

EC2实例上部署了两个Streamlit应用,分别运行在8501、8502端口,直接访问端口正常,且有绑定到该EC2的域名。配置Nginx将/app1、/app2路由到对应应用时,要么返回403错误,要么返回404并跳转至根路径/,怀疑是Nginx配置问题。

当前配置

Nginx配置(/etc/nginx/sites-enabled/default)

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name = <my domain>;

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    # Required to enable Streamlit's websockets (?)
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    proxy_read_timeout 2h;

    location /app1 {
        proxy_pass http://127.0.0.1:8501/;
    }

    location /app2 {
        proxy_pass http://127.0.0.1:8502/;
    }

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        # try_files $uri $uri/ =404;
#       proxy_pass http://127.0.0.1:8501;
    }
}

备注

  • 根路径/的路由已注释(无需根路径服务,可返回404)
  • 当前配置下两个location均返回403错误
  • 去掉proxy_pass地址末尾的/后,返回404错误

Streamlit配置(.streamlit/config.toml,app2对应端口为8502)

[logger]
level = "debug"

[server]
port = 8501
#baseUrlPath = "app1"
headless = true
enableCORS = false
enableXsrfProtection = false
问题解决

通过两处修改解决问题,其中第一处为关键:

  • 取消两个Streamlit配置中baseUrlPath的注释,将proxy_pass路径改为对应/app1、/app2(去掉末尾的/)
  • 将所有必要的代理配置项添加到每个location块下(而非全局)

修改后的Nginx配置示例(以/app1为例):

location /app1 {
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    proxy_read_timeout 2h;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_pass http://127.0.0.1:8501/app1;
}

修改后的Streamlit config.toml示例:

[server]
port = 8501
baseUrlPath = "app1"
headless = true
enableCORS = false
enableXsrfProtection = false

后续可测试配置项的必要性与继承性,目前应用已正常运行。

内容的提问来源于stack exchange,提问作者Vitor Falleiros

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 01:23:38