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

如何配置Nginx反向代理至非根路径(/)?

问题分析与解决方案

当将UptimeKuma代理到/test子路径时出现空白页,核心原因是UptimeKuma前端资源(JS、CSS、图片等)的引用路径默认基于根路径(/),代理后浏览器会向/而非/test请求这些资源,导致资源加载失败。以下是关键修复配置:

必要配置项

  • 路径重写:移除请求的/test前缀,转发到UptimeKuma的根路径
  • 前缀转发头:告诉UptimeKuma它被代理在子路径下,自动调整资源引用路径
  • 完整代理头:确保后端服务能正确处理请求上下文

完整Nginx配置示例

server {
    listen 80;
    server_name localhost;

    location /test {
        # 重写路径,去掉/test前缀后转发给后端
        rewrite ^/test(.*)$ $1 break;
        proxy_pass http://kuma-dashboard:3001/;
        
        # 基础代理配置
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        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;
        
        # 关键:告知UptimeKuma代理前缀
        proxy_set_header X-Forwarded-Prefix /test;
    }
}

备选修复(若X-Forwarded-Prefix不生效)

如果UptimeKuma未识别前缀头,可直接替换页面中的绝对路径引用:

location /test {
    # 保留上述所有代理配置
    
    # 替换页面中所有根路径引用为/test前缀
    sub_filter 'href="/' 'href="/test/';
    sub_filter 'src="/' 'src="/test/';
    sub_filter_once off; # 替换所有匹配项,而非仅第一个
}

注:sub_filter依赖Nginx的ngx_http_sub_module模块,官方Docker Nginx镜像默认已包含该模块。

配置完成后重启Nginx容器,访问http://SERVER_LOCAL_IP/test即可正常加载页面。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 04:23:13