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服务

正常访问:

相关推荐
q***697717 小时前
Spring Boot与MyBatis
spring boot·后端·mybatis
IUGEI18 小时前
synchronized的工作机制是怎样的?深入解析synchronized底层原理
java·开发语言·后端·c#
间彧18 小时前
GraalVM Native Image:跨平台能力与编译模式深度解析
后端
间彧18 小时前
GraalVM Native Image 与传统 JVM 内存管理:云原生时代的技术选型指南
后端
r***123818 小时前
SpringBoot最佳实践之 - 使用AOP记录操作日志
java·spring boot·后端
b***748818 小时前
前端GraphQL案例
前端·后端·graphql
LSL666_19 小时前
SpringBoot自动配置类
java·spring boot·后端·自动配置类
q***783719 小时前
Spring Boot 3.X:Unable to connect to Redis错误记录
spring boot·redis·后端
t***265920 小时前
SpringBoot + vue 管理系统
vue.js·spring boot·后端
qq_124987075321 小时前
基于springboot的疾病预防系统的设计与实现(源码+论文+部署+安装)
java·spring boot·后端·毕业设计