首页 > 解决方案 > Nginx 更改根路径后返回 404

问题描述

我已经在我的 Digital Ocean droplet 上安装了 Nginx,并且我已经为我的 react 应用程序进行了构建,现在我想将构建 index.html 文件提供给 Nginx,

/etc/nginx/sites-enabled/default

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        root /root/project-name/dist;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }
}

当我在浏览器中访问服务器时,我收到一条 404 消息。

路径/root/project-name/dist有效:

在此处输入图像描述

如果我将根路径更改为:

root /var/www/html;

然后我会显示默认的 Nginx 页面:

在此处输入图像描述

和文件夹都在同一路径中rootvar

在此处输入图像描述

那么为什么我不能将我的根目录指向不同的文件夹呢?

标签: nginx

解决方案


解决了错误。

步骤 1. 阅读日志(!!)

Nginx:stat()失败(13:权限被拒绝)

我的 nginx 日志位于/var/log/nginx/. 显示error.log

2021/10/04 18:18:41 [crit] 1565#1565: *3 stat() "/root/project-name/dist/" 失败(13:权限被拒绝),客户端:82.73.195.213,服务器:_ ,请求:“GET / HTTP/1.1”,主机:“104.248.82.123”

步骤 2. 更改对文件夹的访问权限:

chmod +x /root/
chmod +x /root/project-name
chmod +x /root/project-name/dist

推荐阅读