nginxserverfedora

nginx: "/root/index.html" forbidden (13: Permission denied)


I am installing nginx. Here is the steps I followed:

gpasswd -a nginx root

chmod g+x /root

(sorry, couldn't correctly format as code)

service nginx restart

I visited my_domain_name.com and got 403 error. /var/log/nginx/error.log content:

"/root/index.html" is forbidden (13: Permission denied), client: 117.211.86.108, server: my_domain_name.com, request: "GET / HTTP/1.1", host: "my_domain_name.com"

Solution

  • Oh! Please don't disable SELinux.

    Don't use /root — it's a trap!

    First — do you really need to serve files from /root? That's actually the home directory for the root user account, not meant to be the web root.

    Instead, use /var/www/html or (my preference) /srv/www.

    If you do use /root, make sure you're not exposing ssh keys or authorized_keys files, database passwords, or anything similar. Using /root to serve web pages is really just a bad idea all around. (But... no shame here: it's clearly confusing, and kind of a trap set by history due to the name 'root' being used for multiple concepts.)

    Make SELinux work for you!

    Second, rather than disabling SELinux (which, in this case, is protecting you from doing something dangerous), you should configure it properly. In Fedora Linux, the SELinux policy as designed so nginx shares this with other webservers, so, using /srv/www/yoursite as the root,

    chcon -R -t httpd_sys_content_t /srv/www/yoursite
    

    should do it.

    (This answer should also apply to all of the "Enterprise Linux" distributions that are downstream from Fedora Linux — that is, RHEL, CentOS Linux, CentOS Stream, Alma Linux, Rocky Linux, etc., etc.)