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

Nginx反向代理配置出现404错误,寻求解决方案

Nginx反向代理404问题排查与解决

问题场景

配置Nginx反向代理后,访问http://192.168.29.118/portainer/返回404 page not found,但直接访问http://192.168.29.118:9445或在服务器本地执行curl http://127.0.0.1:9445均正常。

现有配置

Nginx主配置文件 /etc/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/*;
}

站点配置文件 /etc/nginx/sites-enabled/default

server {
    
    listen 80 default_server;
    listen [::]:80 default_server;

    index index.html index.htm index.nginx-debian.html;

    location /portainer/ {
        proxy_pass  http://127.0.0.1:9445;
    }
}

日志信息

access.log记录(error.log无相关内容):

192.168.29.67 - - [21/Oct/2022:13:20:43 +0000] "GET /portainer/ HTTP/1.1" 404 43 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:106.0) Gecko/20100101 Firefox/106.0"

问题原因与解决方法

问题出在proxy_pass的路径匹配逻辑上:当location路径以/结尾时,若proxy_pass不带结尾的/,Nginx会把location的完整路径(包括/portainer/)传递给后端服务。此时后端收到的请求是http://127.0.0.1:9445/portainer/,但Portainer的根路径是/,因此返回404。

修复方案

修改站点配置中的proxy_pass,在末尾添加/,让Nginx把/portainer/替换为/传递给后端:

location /portainer/ {
    proxy_pass  http://127.0.0.1:9445/;
}

修改后执行以下命令验证配置并重启Nginx生效:

sudo nginx -t && sudo systemctl restart nginx

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 21:40:33