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

使用Nginx反向代理本地Streamlit应用异常排查(默认配置失效)

问题:Nginx默认配置下无法代理Streamlit应用到80端口

我在同一服务器上运行了一个由user1启动的Streamlit应用(端口8501),想通过Nginx将其暴露在80端口。初始使用Nginx安装后的默认配置文件nginx.conf:

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}


#mail {
#       # See sample authentication script at:
#       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#       # auth_http localhost/auth.php;
#       # pop3_capabilities "TOP" "USER";
#       # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#       server {
#               listen     localhost:110;
#               protocol   pop3;
#               proxy      on;
#       }
#
#       server {
#               listen     localhost:143;
#               protocol   imap;
#               proxy      on;
#       }
#}

同时在/etc/nginx/conf.d/myserver.conf中配置了代理规则:

server {
    listen 80;
    location / {
        proxy_pass http://0.0.0.0:8501/;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

    access_log /var/log/nginx/access-8501.log;
    error_log /var/log/nginx/error-8501.log;
}

但访问服务器80端口时,始终显示Nginx默认主页,错误日志无内容。若将nginx.conf精简为以下配置,代理则正常工作:

events {
}
http {
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        include /etc/nginx/conf.d/*.conf;
}

请问初始默认配置存在什么问题?如何通过代理隐藏Streamlit应用?


分析与解决方案

问题根源

初始nginx.conf中最后一行是include /etc/nginx/sites-enabled/*;,Nginx默认安装后,sites-enabled目录下会有一个软链接指向/etc/nginx/sites-available/default——这个默认配置文件里包含一个监听80端口的server块,且被设置为默认服务器。

Nginx加载配置时会按顺序处理多个server块:

  1. 先加载conf.d/myserver.conf中的代理server块
  2. 再加载sites-enabled/default中的默认server块

当多个server块监听同一端口时,若请求未匹配到指定的server_name,Nginx会优先使用默认服务器。因此访问80端口时,Nginx匹配了默认server块返回主页,你的代理配置未生效。

解决方法

以下三种方案任选其一即可:

方案1:禁用默认站点配置

删除sites-enabled中的默认软链接,让Nginx只加载你的代理配置:

sudo unlink /etc/nginx/sites-enabled/default
sudo nginx -t  # 检查配置语法合法性
sudo systemctl reload nginx

方案2:提升代理配置优先级

在conf.d/myserver.conf的server块中添加server_name指令,让它的匹配优先级高于默认配置:

server {
    listen 80;
    server_name _;  # 匹配所有未指定Host的请求,也可替换为你的实际域名
    location / {
        proxy_pass http://0.0.0.0:8501/;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

    access_log /var/log/nginx/access-8501.log;
    error_log /var/log/nginx/error-8501.log;
}

修改后执行配置检查和重载:

sudo nginx -t
sudo systemctl reload nginx

方案3:修改默认站点配置

编辑/etc/nginx/sites-available/default,注释掉原有的location /块并替换为你的代理规则,或者直接删除整个默认server块。修改后同样需要检查配置并重载Nginx。

验证

完成上述操作后,访问服务器80端口即可正常加载Streamlit应用,实现通过Nginx代理隐藏8501端口的需求。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 05:17:32