由于某种原因无法在 nginx 上更改 root

Ale*_*lex 2 nginx

出于某种原因,我无法更改根目录。我与 上的默认根目录具有相同的权限/usr/share/nginx/html,并且我还755对其他文件夹中的所有文件和文件夹具有递归权限。我什至尝试将用户和组来回更改为 root 和 nginx,看看它是否有任何区别,但没有。我不断收到403 Forbidden

所以基本上我做了这个文件夹

$ mkdir -p /srv/www/test.com/public_html

$ vi /srv/www/test.com/public_html/index.html

$ vi /srv/www/test.com/public_html/test.php

$ chmod -R 755 /srv/www/test.com/public_html
Run Code Online (Sandbox Code Playgroud)

尝试将用户和组更改为 nginx

$ chown -R nginx:nginx /srv/www
Run Code Online (Sandbox Code Playgroud)

配置主conf文件

$ vi /etc/nginx/nginx.conf
Run Code Online (Sandbox Code Playgroud)

这是我的配置的样子,包含被注释掉

user  nginx;
worker_processes  1;
error_log  /var/log/nginx/error.log;   
pid        /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;
    index index.php index.html index.htm;

    server {

        listen       80;
        server_name  _;
        root /srv/www/test.com/public_html;
        # root  /usr/share/nginx/html;

        location / {
                autoindex on;
        }

        error_page  404 /404.html;

        location = /40x.html { }


        error_page   500 502 503 504  /50x.html;

        location = /50x.html { }

        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_pass   unix:/var/run/php-fpm/www.sock;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include        fastcgi_params;
        }

    }

}       
Run Code Online (Sandbox Code Playgroud)

一切都在注释掉的默认根文件夹中工作,但是当我更改根目录时,我得到 403 禁止。我觉得我已经尽我所能,更改用户和文件权限等等。php-fpm 将 nginx 作为用户和组。并且用户在 main.conf 文件中设置为 nginx。我只使用这一个文件来配置 nginx,并将包含的内容注释掉。

我不知道该怎么做。现在被卡住了几个小时。任何帮助将不胜感激

Xav*_*cas 6

为清楚起见,我将我的评论作为答案发布,这是由 SELinux 引起的,将策略模式设置为宽松semanage permissive -a httpd_t是一种“快速而肮脏”的解决方案。

干净的解决方案是设置 SELinux 上下文chcon -Rt httpd_sys_content_t /srv/www/test.com并查找潜在的新 SELinux 投诉(如果有)(取决于您在配置文件中接下来设置的内容)。

  • `restorecon` 也可以工作,因为这是 `/srv/www` 的默认上下文。 (2认同)