Nginx 403 Error: Directory Index of [Folder] Is Forbidden
The 403 Forbidden error with the message Directory index of [folder] is forbidden in Nginx typically occurs when Nginx is trying to serve a directory listing, but directory listing is not enabled or the permissions do not allow access.
Here’s how you can address this issue:
1. Check Nginx Configuration
Ensure that your Nginx configuration allows access to the directory and handles directory indexing correctly.
Enable Directory Listing
If you want to enable directory listing, you need to include the autoindex directive in your Nginx configuration.
Open Your Nginx Configuration File
This could be in
/etc/nginx/nginx.conf, or in a site-specific configuration file located in/etc/nginx/sites-available/or/etc/nginx/conf.d/.sudo nano /etc/nginx/sites-available/defaultAdd or Update the
locationBlockAdd or modify the
locationblock to enable directory indexing:server { listen 80; server_name example.com; location / { root /var/www/html; index index.html index.htm; autoindex on; # Enable directory listing } }The
autoindex on;directive enables the directory listing for the specified location.Save and Close the File
Test the Configuration
sudo nginx -tEnsure there are no syntax errors in your configuration.
Reload Nginx
sudo systemctl reload nginx
2. Ensure Proper Permissions
Make sure that the Nginx user has the necessary permissions to access the directory and its contents.
Check Directory Permissions
Verify that the directory and its parent directories have the correct permissions. The directory should be readable by the Nginx user (often
www-data).sudo chmod 755 /var/www/html sudo chmod 755 /var/www/html/folderCheck Ownership
Ensure the directory is owned by the correct user and group. For example, if your Nginx user is
www-data, the directory should be owned bywww-data.sudo chown -R www-data:www-data /var/www/html/folder
3. Check Index File Configuration
If you want to serve a specific index file and not enable directory listing, ensure that the index directive is properly configured.
Modify the
indexDirectiveMake sure your
locationblock specifies anindexfile:location / { root /var/www/html; index index.html index.htm; }This tells Nginx to serve
index.htmlorindex.htmif present in the directory.Save and Close the File
Test and Reload Nginx
sudo nginx -t sudo systemctl reload nginx
4. Ensure No .htaccess Files or Conflicting Configurations
Although .htaccess files are used by Apache and not Nginx, check for any conflicting directives or configurations that may have been ported from Apache.
5. Verify Nginx Logs
If you continue to face issues, check the Nginx error logs for more details.
View Nginx Error Log
sudo tail -f /var/log/nginx/error.logLook for any specific error messages that might provide more insight into the issue.
Summary
- Enable Directory Listing: Use
autoindex on;in yourlocationblock to enable directory listing. - Ensure Permissions: Verify that directory and file permissions are set correctly for Nginx.
- Configure Index Files: Ensure the
indexdirective is correctly configured if you don't want directory listing. - Check Logs: Review Nginx error logs for more details.
By following these steps, you should be able to resolve the 403 Forbidden error related to directory indexing in Nginx.