Solve Nginx access /root/test/ 403 Forbidden issues
When using Nginx as a web server, you may encounter403 Forbiddenmistake. For example, suppose you put the HTML file in/root/test/
, return when accessing403 Forbidden, but if it is changed to/data/test/
, access is normal again. This situation is usually withFile access permissionsorSELinuxRelated.
This article will analyze the causes of the problem and provide detailed solutions.
Problem recurs
Nginx configuration
AssumptionThe configuration is as follows:
server { listen 80; server_name ; location / { root /root/test; index ; } }
When visiting/
When Nginx reports an error:
403 Forbidden
However, if modifiedroot
for/data/test
, then the access is normal.
Possible reasons
/root directory permission issues
By default,/root/The directory belongs toroot
User, and onlyroot
Account can be accessed. The Nginx process usuallynginx
orwww-data
This kind of ordinary user is running, so it cannot be read/root/test/
。
Check the Nginx running user:
ps aux | grep nginx
Sample output:
nginx 1234 0.0 0.1 123456 4567 ? S 10:00 0:00 nginx: worker process
herenginx
is a user of the Nginx process.
SELinux Limitations
If the server has SELinux enabled, it may block Nginx access/root/
Table of contents. Check with the following command:
getenforce
If returnEnforcing
, indicating that SELinux is enabled, which may cause a 403 error.
Solution
Solution 1: Move the file to /data/or /var/www/ (recommended solution)
Nginx site files are usually placed in/var/www/
or/data/
Directory, not/root/
. The following steps can be used to correct it:
mkdir -p /data/test mv /root/test/ /data/test/ chown -R nginx:nginx /data/test chmod -R 755 /data/test
Modify Nginx configuration:
server { listen 80; server_name ; location / { root /data/test; index ; } }
Then reload Nginx:
systemctl restart nginx
✅ This can avoid permission issues, and it is recommended to use it!
Solution 2: Modify /root directory permissions (not recommended)
If you must use it/root/test/
, you can modify directory permissions:
chmod -R 755 /root/test chmod o+rx /root
But this mayReduce server security,because/root/
The directory is originally private, it is recommended to usePlan 1。
Solution 3: Adjust SELinux (if applicable)
ifgetenforce
Command returnEnforcing
, indicating that SELinux may restrict Nginx access/root/test/
. You can try:
setenforce 0
If 403 disappears, it means it is caused by SELinux. You can use the following command to allow Nginx to access/root/test
:
chcon -R --reference=/var/www/html /root/test
or:
chcon -R -t httpd_sys_content_t /root/test
Then restart Nginx:
systemctl restart nginx
Summarize
-
✅ Recommended practices: Move site files to
/data/
or/var/www/
and modify the Nginx configuration. -
🚨 Not recommended:Revise
/root/
Directory permissions may bring security risks. -
🔍If SELinux is enabled, need to use
chcon
Give the correct security context.
This is the article about Nginx access /root/down 403 Forbidden problem solving. For more information about Nginx 403 Forbidden, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!