Nginx
How to Fix 403 Forbidden Error in Nginx
Quick Fix
The most common cause of 403 errors in Nginx is: Incorrect file or directory permissions
Quick solution: Directories should be 755 and files 644: sudo find /var/www/site -type d -exec chmod 755 {} \;
The 403 Forbidden error in Nginx indicates a problem specific to your Nginx configuration or environment. This guide provides platform-specific solutions.
Common Causes in Nginx
- Incorrect file or directory permissions
- The Nginx user (www-data/nginx) cannot read the files
- Missing index file with autoindex off
- A deny rule in the Nginx configuration
- Wrong root or alias path
- SELinux blocking access on RHEL/CentOS
Step-by-Step Solutions
Solution 1: Fix File and Directory Permissions
- Directories should be 755 and files 644: sudo find /var/www/site -type d -exec chmod 755 {} \;
- sudo find /var/www/site -type f -exec chmod 644 {} \;
- Make sure every parent directory is executable (755) so Nginx can traverse it
Solution 2: Fix Ownership for the Nginx User
- Check the user Nginx runs as: grep 'user' /etc/nginx/nginx.conf (usually www-data or nginx)
- Set ownership: sudo chown -R www-data:www-data /var/www/site
- Reload Nginx: sudo systemctl reload nginx
Solution 3: Check for a Missing Index or Deny Rule
- If the URL is a directory, ensure an index file exists or set: index index.html index.php;
- Search your config for deny directives: grep -r 'deny' /etc/nginx/
- Confirm the root/alias path points to the correct directory
- Test and reload: sudo nginx -t && sudo systemctl reload nginx
Solution 4: Check SELinux (RHEL/CentOS/Rocky)
- See if SELinux is enforcing: getenforce
- Allow Nginx to read the web root: sudo chcon -R -t httpd_sys_content_t /var/www/site
- For persistent rules use semanage fcontext, then restorecon
- Review denials: sudo ausearch -m avc -ts recent
Prevention Tips
- Monitor Nginx error logs regularly
- Implement proper health checks and monitoring
- Keep Nginx and dependencies up to date
- Set up alerting for error rate increases
- Document your configuration changes
- Test configuration changes in staging first