Nginx 无法访问可用站点中的根目录

in4*_*3sh 2 linux nginx server

我在 Digital Ocean Droplet (Ubuntu) 上设置了基本的 nginx。我只有一个 root 用户。在我的 nginx 配置文件中,我有

server {
    listen 80;

    server_name site.com;

    root /var/www/html/site;
}
Run Code Online (Sandbox Code Playgroud)

我尝试将其更改为

root /root/site
Run Code Online (Sandbox Code Playgroud)

但它给了我 403 Forbidden 错误。当我将其更改为 nginx 默认目录时

/var/www/html/site;
Run Code Online (Sandbox Code Playgroud)

一切正常。

为什么它给我这个错误?我知道只有root用户才能访问根目录,但为什么浏览器不能只从那里读取文件?是否可以创建另一个文件夹,而不是像“/var/www/html/23rdsquad;”那样 我的服务器上的某个位置(不是 /root 或 /var/www)并使用它?

小智 5

您的 Nginx 用户无权读取目录 /root/site,因此:

  • 检查目录权限

    user@user:~$ls -l /根/站点 | grep 站点

    drwxrwxrwx 7 用户 用户 4096 前 17 16:56 网站

  • 检查Nginx用户

    user@user:~$ps aux|grep nginx|grep -v grep

  • Nginx用户配置为“ /etc/nginx/nginx.conf

    vim /etc/nginx/nginx.conf

    通常你有“用户www-data ;”


1)更改任一Nginx用户的目录权限。

2)重启Nginx服务

user@user:~$ sudo systemctl restart nginx
Run Code Online (Sandbox Code Playgroud)