【问题标题】:Nginx 403 Forbidden for files simply for being inside of the home folderNginx 403 禁止仅用于位于主文件夹内的文件
【发布时间】:2019-10-28 06:33:05
【问题描述】:

我使用标准 dnf 在 Fedora 29 上安装了标准 Nginx。

我在 nginx.conf 的顶部将用户更改为“root”。我还尝试在 nginx.conf 的顶部将用户设置为“用户名”。我也试过这个,同时将用户留在默认的“nginx”。

服务器块内部:

root /usr/share/nginx/html (THIS WORKS!)
root /home/username/path/to/destination (403 FORBIDDEN)

我已将 index.html 复制到工作地点和禁止目的地之间的每个文件夹位置,如下所示:

/usr/share/nginx/html/index.html (THIS WORKS!)
/usr/share/nginx/index.html (THIS WORKS!)
/usr/share/index.html (THIS WORKS!)
/usr/index.html (THIS WORKS!)
/index.html (THIS WORKS!)
/home/index.html (403 FORBIDDEN)
/home/username/index.html (403 FORBIDDEN)
/home/username/path/index.html (403 FORBIDDEN)
/home/username/path/to/index.html (403 FORBIDDEN)
/home/username/path/to/destination/index.html (403 FORBIDDEN)

我逐步将 nginx.conf 服务器块根行更改为上述每个文件夹,并且每次都重新启动 nginx:

root    /usr/share/nginx/html;              (THIS WORKS!)
root    /usr/share/nginx;                   (THIS WORKS!)
root    /usr/share;                         (THIS WORKS!)
root    /usr;                               (THIS WORKS!)
root    /;                                  (THIS WORKS!)
root    /home;                              (403 FORBIDDEN)
root    /home/username;                     (403 FORBIDDEN)
root    /home/username/path;                (403 FORBIDDEN)
root    /home/username/path/to;             (403 FORBIDDEN)
root    /home/username/path/to/destination; (403 FORBIDDEN)

每次:

sudo service nginx restart

以下是每个文件夹的权限:

drwxr-xr-x. 2 root root /usr/share/nginx/html
drwxr-xr-x.   3 root root /usr/share/nginx
drwxr-xr-x.  279 root root /usr/share
drwxr-xr-x.  12 root root /usr
dr-xr-xr-x.  19 root  root /
drwxr-xr-x.  4 root  root /home
drwxr-xr-x. 26 username username /home/username
drwxrwxrwx. 15 username username /home/username/path
drwxr-xr-x.  4 username username /home/username/path/to
drwxr-xr-x.  6 username username /home/username/path/to/destination

以下是复制到上述所有文件夹的 index.html 文件的权限:

-rw-r--r--. 1 root root 3700 Nov 20 12:32 index.html

【问题讨论】:

    标签: nginx fedora


    【解决方案1】:

    为目录中的读写http服务应用访问上下文安全性。

    在 readhat、fedora 和 centos 中默认使用 selinux。

    chown nginx:nginx -R "dir_name"
    chcon -R -t httpd_sys_rw_content_t "dir_name"
    semanage fcontext -a -t httpd_sys_rw_content_t "dir_name"
    

    【讨论】:

      【解决方案2】:

      我认为,您不应该在 nginx.conf 中更改用户。最常见的建议是保留默认用户 www-data。

      最好将您的 用户名 添加到 www-data 组或类似的东西。 尝试往这个方向搜索,也许这个链接对你有帮助:https://askubuntu.com/a/9411

      【讨论】:

        猜你喜欢
        • 2018-11-06
        • 1970-01-01
        • 2011-10-11
        • 1970-01-01
        • 2013-10-17
        • 2017-12-05
        • 1970-01-01
        • 2018-01-10
        相关资源
        最近更新 更多