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

Nginx根路径可正常服务静态文件,/admin子路径返回ERR_EMPTY_RESPONSE

Nginx配置问题:根路径正常访问,/admin路径返回ERR_EMPTY_RESPONSE

我在服务器上配置Nginx为前端应用提供静态文件服务,根路径http://xxx.85.127.14/可正常访问,但访问http://xxx.85.127.14/admin时浏览器返回ERR_EMPTY_RESPONSE错误。不过本地测试curl -I http://localhost/admin可正常运行。

服务器与环境详情

  • 操作系统: Ubuntu 20.04
  • Web服务器: Nginx
  • 前端框架: React/Vue(按需选择)
  • 部署目录:
    • /root/deployment/user/dist(对应根路径/)
    • /root/deployment/admin/dist(对应/admin路径)

当前Nginx配置文件(/etc/nginx/sites-available/default)

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name xxx.85.127.14;
    location / {
        root /root/deployment/user/dist;
        index index.html;
        try_files $uri /index.html;
    }

    location /admin {
        root /root/deployment/admin/dist;
        index index.html;
        try_files $uri /index.html;
    }


    location /api/ {
        proxy_pass http://localhost:5000/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

问题原因

location /admin块中的root配置逻辑错误。Nginx使用root时,会将当前location的路径(即/admin)拼接到root指定的目录后,最终尝试访问的路径是/root/deployment/admin/dist/admin——但你的admin静态文件实际存放在/root/deployment/admin/dist下,导致Nginx找不到对应资源,返回空响应。

修复方案

推荐使用alias替换root,alias会直接将location路径映射到指定目录,不会进行路径拼接,更适配这种子路径独立部署的场景:

修改后的location /admin配置如下:

location /admin {
    alias /root/deployment/admin/dist;
    index index.html;
    try_files $uri $uri/ /admin/index.html;
}

注意:try_files的最后一项要改为/admin/index.html,确保当访问admin下的路由(如/admin/dashboard)时,能正确指向admin应用的入口文件,而不是跳转到根路径的index.html。

验证步骤

  1. 执行sudo nginx -t检查配置文件语法是否正确
  2. 若检查通过,执行sudo systemctl reload nginx重启Nginx服务
  3. 重新访问http://xxx.85.127.14/admin测试

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 08:18:24