【问题标题】:403 nginx (1.13.1) on a folder outside of /usr/share/nginx/403 nginx (1.13.1) 在 /usr/share/nginx/ 之外的文件夹上
【发布时间】:2017-11-22 19:59:30
【问题描述】:

CentOS 7 + nginx 1.13.1

SELinux -> 当前模式:许可

在 root 帐户下做所有事情。

/root/raid 与 /usr/share/nginx/html/raid 是同一个文件夹,因为它是从 /dev/md0 挂载的:

mount /dev/md0 /usr/share/nginx/html/raid
mount /dev/md0 /root/raid

如果我尝试将 nginx.conf 中的根文件夹更改为 /usr/share/nginx/ 之外的 smth,例如 /root/raid,我会收到 403 错误:(

这是我的 nginx.conf:

user  nginx;
worker_processes auto;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;

    keepalive_timeout  65;

    include /etc/nginx/conf.d/*.conf;


    server {
        listen       80 default_server;
        server_name  _ server1 server1.domain.com;

        root         /usr/share/nginx/html/raid;
        #root         /root/raid;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }

    }
}

我这样做了:

# chown -R nginx:nginx /usr/share/nginx/html/raid
# chmod -R 775 /usr/share/nginx/html/raid

或者这个(不是真的需要,因为它们是同一个文件夹,对吧?):

# chown -R nginx:nginx /root/raid
# chmod -R 775 /root/raid

# ls -la /usr/share/nginx/html/raid
total 28
drwxrwxr-x. 3 nginx nginx 4096 Jun 20 02:56 .
drwxr-xr-x. 3 root  root    18 Jun 20 02:56 ..
-rwxrwxr-x. 1 nginx nginx 3650 Oct 31  2016 404.html
-rwxrwxr-x. 1 nginx nginx  537 May 30 18:10 50x.html
-rwxrwxr-x. 1 nginx nginx  924 Jun 16 21:49 index.html
-rwxrwxr-x. 1 nginx nginx   19 Jun  8 18:48 info.php
-rwxrwxr-x. 1 nginx nginx    1 Jun 20 02:56 test

# ls -la /root/raid
total 28
drwxrwxr-x. 3 nginx nginx 4096 Jun 20 02:56 .
dr-xr-x---. 6 root  root   192 Jun 20 02:23 ..
-rwxrwxr-x. 1 nginx nginx 3650 Oct 31  2016 404.html
-rwxrwxr-x. 1 nginx nginx  537 May 30 18:10 50x.html
-rwxrwxr-x. 1 nginx nginx  924 Jun 16 21:49 index.html
-rwxrwxr-x. 1 nginx nginx   19 Jun  8 18:48 info.php
-rwxrwxr-x. 1 nginx nginx    1 Jun 20 02:56 test

只要我将 nginx.conf 中的根路径改回 /usr/share/nginx/html/raid 并且网站正确打开。

我也试过了:

# setsebool -P httpd_can_network_connect on
# chcon -Rt httpd_sys_content_t /root/raid
# chcon -R --reference=/usr/share/nginx /root/raid

不应该真的需要许可模式,对吧?

nginx 日志显示如下:

/var/log/access.log:

192.168.0.103 - - [20/Jun/2017:12:45:33 +0300] "GET / HTTP/1.1" 403 571 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36" "-"

/var/log/error.log:

2017/06/20 12:45:33 [error] 18114#18114: *4 "/root/raid/index.html" is forbidden (13: Permission denied), client: 192.168.0.103, server: _, request: "GET / HTTP/1.1", host: "server1.domain.com"

我忘记了什么? :)

【问题讨论】:

    标签: nginx centos7 selinux


    【解决方案1】:

    (1)不要将同一设备安装到不同的安装点,这不是根本原因,但不要永远这样做。
    (2)挂载点“/root/raid”,这是根路径“/root”中的DIR,“nginx:nginx”无法读取“/root”中的子目录,对吧?不要将/dev/md0挂载在“/root”,将挂载点改为其他逻辑目录,如“/srv/app/raid”。
    (3)如果要更改webroot,更好的方法是使用软链接,例如:

    ln -s /srv/app/raid /other_path/raid
    

    所以 "/other_path/raid" 是你的新 webroot 。

    【讨论】:

      猜你喜欢
      • 2018-12-15
      • 1970-01-01
      • 2014-12-13
      • 2017-12-05
      • 2019-10-15
      • 1970-01-01
      • 1970-01-01
      • 2011-11-21
      • 2015-05-25
      相关资源
      最近更新 更多