与 nginx 并行服务两个 Streamlit 应用程序会导致 403 或 404

问题描述 投票:0回答:2

第一次在这里使用nginx,似乎我错过了一些东西。

我有两个 Streamlit 应用程序分别在端口 8501 和 8502 上提供服务。我可以通过直接访问它们各自的 : 来访问。我还有一个 URL 指向提供服务的 EC2 实例。

但是当路由

/app1
/app2
位置时,nginx 要么给我 403 并停止,要么给我 404 并返回到
/
我想 nginx 配置有问题。有人可以帮我正确设置它 - 或者指出我可能出现问题的其他地方吗?

以下是

default
/etc/nginx/sites-enabled/
文件的内容:

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)
  • 如上所示,两个位置的结果都是 403
  • 从位置中的 proxy_pass 地址中删除尾部“/”会导致 404

我尝试在每个应用程序的

baseUrlPath
文件中命名
.streamlit/config.toml
(并将其添加到位置 proxy_pass),但没有帮助。以下是我当前在每个应用程序的 config.toml 上的内容:

[logger]
level = "debug"

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

(app2 的端口 8502 除外)

感谢您的任何见解!


更新:开始工作了!

有两个变化,我认为第一个变化是造成差异的原因,但第二个变化可能是其中的原因:

  1. 取消注释两个 Streamlit 配置上的
    baseUrlPath
    ,并将
    /app1
    /app2
    添加到各自的
    proxy_pass
    调用中(不带尾随的
    /
  2. 这个解决方案,添加了所有建议的行(以前我只有一半),并在每个位置下(而不是像上面那样为整个服务器,我认为每个位置都会继承它)。

稍后我仍将测试所有这些行是否确实需要,以及它们是否可以从服务器定义继承 - 但现在它可以工作。

nginx http-status-code-404 reverse-proxy http-status-code-403 streamlit
2个回答
0
投票

你可以试试

location ~* ^/app1/(.*)$ {
  proxy_pass http://127.0.0.1:8501/$1$is_args$args;
}

这至少应该将请求

domain.com/app1/test/index.html
代理到
127.0.0.1:8501/test/index.html
- 所以删除 app1 部分。

如果这不起作用,您应该查看日志。首先,如果 nginx 成功获取请求,然后查看应用程序的日志 - 它获取了哪个请求以及该请求应该如何成功。


编辑:

您还可以尝试以下方法:

location /app1 {
    proxy_pass http://127.0.0.1:8501; # No trailing slash here
}

0
投票

更新:开始工作了!

有两个变化,我认为第一个变化是造成差异的原因,但第二个变化可能是其中的原因:

Uncommented the baseUrlPath on both Streamlit configs and added /app1 and /app2 to their respective proxy_pass calls (without the trailing /)
From this solution, added all the lines suggested (previously I had only half of them), and under each location (rather than for the whole server as above, from where I thought each location would have inherited it).

稍后我仍将测试所有这些行是否确实需要,以及它们是否可以从服务器定义继承 - 但现在它可以工作。

© www.soinside.com 2019 - 2024. All rights reserved.