Nginx代理URL路径拼接问题(页面报404)

案例一

✅ 问题重现

文件所在位置:/usr/share/nginx/html/test/index.html

nginx配置 A(访问异常):

bash 复制代码
location /test {
    root /usr/share/nginx/html/test;
    index index.html index.htm;
}

访问 http://yourdomain.com/test → 报 404


nginx配置 B(访问正常):

bash 复制代码
location /test {
    root /usr/share/nginx/html;
    index index.html index.htm;
}

访问 http://yourdomain.com/test → 正常显示


🔍 根本原因:root 指令的路径拼接规则

📌 Nginx 的 root 是这样工作的:

root 指定的是"根目录",最终文件路径 = root 路径 + 完整的 URI

也就是说:

  • root /path/to;
  • 请求 /test
  • Nginx 实际查找的文件是:/path/to/test

❌ 为什么配置 A 报 404?

bash 复制代码
location /test {
    root /usr/share/nginx/html/test;
}

当用户访问:

arduino 复制代码
http://yourdomain.com/test

Nginx 会这样拼路径:

ruby 复制代码
root 路径: /usr/share/nginx/html/test
URI: /test
→ 实际查找文件: /usr/share/nginx/html/test/test

它去找的是 /usr/share/nginx/html/test/test 这个文件或目录!

但你实际的文件结构是:

bash 复制代码
/usr/share/nginx/html/test/index.html

所以 Nginx 找不到 /test/test,返回 404


✅ 为什么配置 B 能正常工作?

bash 复制代码
location /test {
    root /usr/share/nginx/html;
}

请求 /test 时:

ruby 复制代码
root 路径: /usr/share/nginx/html
URI: /test
→ 查找路径: /usr/share/nginx/html/test

这正好对应你的文件:

bash 复制代码
/usr/share/nginx/html/test/index.html

所以 Nginx 找到了 index.html,返回成功 ✅


✅ 正确做法:用 alias 替代 root(推荐)

如果你希望 /test 映射到 /usr/share/nginx/html/test 目录,并且访问 /test 就显示该目录下的 index.html,应该使用 alias

bash 复制代码
location /test {
    alias /usr/share/nginx/html/test;
    index index.html index.htm;
    try_files $uri $uri/ =404;
}

🔍 alias 的规则是:

alias替换 location 匹配的部分

所以:

  • location /test
  • alias /usr/share/nginx/html/test
  • 请求 /test → 实际路径:/usr/share/nginx/html/test

完美匹配你的文件结构 ✅


✅ 总结对比

配置 实际查找路径 是否正确
root /usr/share/nginx/html/test; /usr/share/nginx/html/test/test ❌ 错了,多了一层 /test
root /usr/share/nginx/html; /usr/share/nginx/html/test ✅ 正确
alias /usr/share/nginx/html/test; /usr/share/nginx/html/test ✅ 推荐,语义清晰

✅ 推荐最终配置

bash 复制代码
server {
    listen 80;
    server_name yourdomain.com;

    location /test {
        alias /usr/share/nginx/html/test;
        index index.html index.htm;
        try_files $uri $uri/ =404;
    }

    # 其他 location...
}

✅ 验证步骤

  1. 确保文件存在:

    bash 复制代码
    ls -l /usr/share/nginx/html/test/index.html
  2. 测试 Nginx 配置:

    复制代码
    sudo nginx -t
  3. 重载 Nginx:

    复制代码
    sudo systemctl reload nginx
  4. 访问:

    arduino 复制代码
    http://yourdomain.com/test

记住一句话

  • root 时:路径是 root + URI
  • alias 时:location 路径被替换为 alias 路径

所以 /test 映射到一个目录时,优先使用 alias,避免路径拼接错误。

案例二

这是我自己写node服务,carapp对应静态资源,carapi对应api服务

正常访问:

相关推荐
前端小张同学35 分钟前
服务器部署 gitlab 占用空间太大怎么办,优化思路。
后端
databook1 小时前
Manim实现闪光轨迹特效
后端·python·动效
武子康1 小时前
大数据-98 Spark 从 DStream 到 Structured Streaming:Spark 实时计算的演进
大数据·后端·spark
该用户已不存在2 小时前
6个值得收藏的.NET ORM 框架
前端·后端·.net
文心快码BaiduComate2 小时前
文心快码入选2025服贸会“数智影响力”先锋案例
前端·后端·程序员
neoooo2 小时前
🌐 Cloudflare Tunnel vs ZeroTier:两个世界的内网穿透哲学
后端
涡能增压发动积2 小时前
当你不了解“异步”时请慎用“异步”——记一次生产环境故障排查之旅
后端
文心快码BaiduComate2 小时前
用Comate Zulu开发一款微信小程序
前端·后端·微信小程序
用户8356290780512 小时前
Python 删除 Excel 工作表中的空白行列
后端·python
Json_2 小时前
使用python-fastApi框架开发一个学校宿舍管理系统-前后端分离项目
后端·python·fastapi