Nginx 说 open() "/etc/nginx/conf.d/foo.conf" failed (13: Permission denied)

yeg*_*256 13 centos nginx

这是我所拥有的ls -al /etc/nginx

total 52
drwxr-xr-x.  4 root root 4096 Jul 28 04:16 .
drwxr-xr-x. 78 root root 8192 Jul 28 03:37 ..
drwxr-xr-x.  2 root root   26 Jul 28 03:55 conf.d
drwxr-xr-x.  2 root root    6 May 10 09:21 default.d
-rw-r--r--.  1 root root 1034 May 10 09:21 fastcgi.conf
-rw-r--r--.  1 root root  964 May 10 09:21 fastcgi_params
-rw-r--r--.  1 root root 2837 May 10 09:21 koi-utf
-rw-r--r--.  1 root root 2223 May 10 09:21 koi-win
-rw-r--r--.  1 root root 3957 May 10 09:21 mime.types
-rw-r--r--.  1 root root 1033 Jul 28 03:43 nginx.conf
-rw-r--r--.  1 root root  596 May 10 09:21 scgi_params
-rw-r--r--.  1 root root  623 May 10 09:21 uwsgi_params
-rw-r--r--.  1 root root 3610 May 10 09:21 win-utf
Run Code Online (Sandbox Code Playgroud)

这是我/var/log/nginx/error.log之后看到的sudo service nginx start

[emerg] 20360#0: open() "/etc/nginx/conf.d/foo.conf" failed
(13: Permission denied) in /etc/nginx/nginx.conf:33
Run Code Online (Sandbox Code Playgroud)

这就是我所拥有的ls -al /etc/nginx/conf.d/

$ ls -al /etc/nginx/conf.d/
total 8
drwxr-xr-x. 2 root root   26 Jul 28 03:55 .
drwxr-xr-x. 4 root root 4096 Jul 28 04:16 ..
-rw-r--r--. 1 root root  230 Jul 28 03:50 foo.conf
Run Code Online (Sandbox Code Playgroud)

怎么了?

yae*_*shi 20

当您permission denied因未知原因在文件访问等方面出现错误时,可能与 SELinux 相关。尤其是当你看到一段类似以下权限drwxr-xr-x.所示ls -l为有问题的文件/目录,可以把它们贴错标签(你可以看到它ls -Z)而导致的问题。

您应该首先通过运行来检查当前的 SELinux 模式getenforce。如果显示Enforcing,则Permissive通过运行暂时将模式设置为setenforce 0,然后查看您的应用程序是否工作。

请参阅您的 SELinux 发行版指南以获得永久修复,包括在启动时设置 SELinux 模式、重新标记文件或目录、更新策略等。这是CentOSHowto