Nginx says open() "/etc/nginx/conf.d/foo.conf" failed (13: Permission denied)
When you are getting permission denied
errors on file access etc. for unknown reason, it might be related to SELinux. Especially when you see a period following permissions like drwxr-xr-x.
shown by ls -l
for the file/dir in question, they could be mislabeled (you can see it by ls -Z
) and cause the problem.
You should first check current SELinux mode by running getenforce
. If it says Enforcing
, then temporarily set the mode to Permissive
by running setenforce 0
, and see if your application works afterwards.
Please consult your distribution's guide on SELinux for permanent fix, including setting the SELinux mode on start up, relabeling files or directories, updating policies, etc. Here's Howto for CentOS.
Altering whole SElinux is not always the right answer. I'm not an expert but i'd compare it with disabling firewall if a port is not accessible.
Other quick solution could be to "restore SE context" of files, especially if those are copied from somewhere.
See https://www.thegeekstuff.com/2017/05/restorecon-examples/ for more details.
These are commands worth to try before disabling SElinux
sudo restorecon /etc/nginx/conf.d/
sudo restorecon /etc/nginx/conf.d/*