Nginx 提供的静态文件显示 404

问题描述 投票:0回答:1

社区。 我需要你帮助我配置 Nginx。

我有两个反应项目(管理和前端)

所以我想使用 Nginx 来部署这些。

以下是我的 nginx 配置。

server {
    listen 80;
    server_name example.com;

    location /admin {
        alias /home/ubuntu/dwq/admin/build;
        index index.html;
    }

    location /main {
        alias /home/ubuntu/dwq/frontend/build;
        index index.html;
    }

    location /api/ {
        proxy_pass http://localhost:8000/api/;
    }
}

但是当我尝试访问 example.com/admin 或 example.com/main 时,找不到 css 和 js 文件,并返回 404。

server {
    listen 80;
    server_name example.com;

    location /admin {
         alias /home/ubuntu/dwq/admin/build;
         index index.html;
    }

    location / {
        root /home/ubuntu/dwq/frontend/build;
        index index.html;
    }

    location /api/ {
        proxy_pass http://localhost:8000/api/;
    }
}

如果我像上面那样配置,那么前端项目运行良好,但管理仍然没有显示任何内容(因为静态文件为 404)

为什么会发生这种情况以及如何解决这个问题? 我很高兴听到反馈。 谢谢。

我想正确配置,以便两个 React 项目都能在 /admin 和 /main 下正常运行。

reactjs nginx static nginx-config nginx-location
1个回答
0
投票

我假设您正在使用 create-react-app。

来自部署文档

创建 React App 会生成一个构建,假设您的应用程序托管在服务器根目录。

确保这两个应用程序都是使用您计划在反向代理中使用的相对路径构建的。

© www.soinside.com 2019 - 2024. All rights reserved.