现象

nginx直接用systemctl start nginx命令启动后访问前端页面 出现403错误查看nginx日志发现 open() “***” failed (13: Permission denied)这一错误,但是直接用/usr/sbin/nginx -c /etc/nginx/nginx.conf启动后访问前端页面没有任何问题,这实在令人困惑

初步排查

初步怀疑是权限问题 于是给前端目录最高权限 chmod -R 777 <前端目录> ,再次用systemctl restart nginx重启nginx还是报权限的错误

继续摸排

继续搜索资料发现 nginix默认用户是nobody与用户目录不一致可能会出现权限的错误,于是修改nginx.conf文件将#user nobody; 改为 user root; 例如:

user  root;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;
    
        charset utf-8;
    
        #access_log  logs/host.access.log  main;
    
        location / {
            root   /root/www/;          ## 设置的地方
            index  index.html index.htm;
        }
                #error_page  404              /404.html;
    
            # redirect server error pages to the static page /50x.html
            #
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
    
            # proxy the PHP scripts to Apache listening on 127.0.0.1:80
            #
            #location ~ \.php$ {
            #    proxy_pass   http://127.0.0.1;
            #}
    
            # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
            #
            #location ~ \.php$ {
            #    root           html;
            #    fastcgi_pass   127.0.0.1:9000;
            #    fastcgi_index  index.php;
            #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            #    include        fastcgi_params;
            #}
    
            # deny access to .htaccess files, if Apache's document root
            # concurs with nginx's one
            #
            #location ~ /\.ht {
            #    deny  all;
            #}
        }
}

然后执行 systemctl restart nginx重启nginx,执行ps aux | grep nginx 查看进程都是root用户启动的,但是仍然报权限错误,无语了都.

最后

在StackOverflow的一个回答下面发现是SELinux的锅,原文: Using NGINX and NGINX Plus with SELinux

解决办法

查看SELinux状态

运行命令getenforce,验证SELinux状态。
返回状态如果是enforcing,表明SELinux已开启。

选择临时关闭或者永久关闭SELinux

  • 执行命令setenforce 0临时关闭SELinux。

  • 永久关闭SElinux。

    运行以下命令,编辑SELinux的config文件。

    vi /etc/selinux/config
    找到SELINUX=enforcing,按i进入编辑模式,将参数修改为SELINUX=disabled
    在这里插入图片描述 修改完成后,按下键盘Esc键,执行命令:wq,保存并退出文件,重启服务器后即可生效

或者选择保留SELinux设置并修改资源目录的安全上下文

执行 chcon -t httpd_sys_content_t -R <资源目录> 修改 安全上下文
例如: chcon -t httpd_sys_content_t -R /home/path/site

参考

  1. open() “/root/project/static/*.css” failed (13: Permission denied) nginx
  2. Using NGINX and NGINX Plus with SELinux
  3. https://blog.csdn.net/weixin_43640082/article/details/102574434
  4. 开启或关闭SELinux
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐