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/default
Add or Update the
location
BlockAdd or modify the
location
block 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 -t
Ensure 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/folder
Check 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
index
DirectiveMake sure your
location
block specifies anindex
file:location / { root /var/www/html; index index.html index.htm; }
This tells Nginx to serve
index.html
orindex.htm
if 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.log
Look for any specific error messages that might provide more insight into the issue.
Summary
- Enable Directory Listing: Use
autoindex on;
in yourlocation
block to enable directory listing. - Ensure Permissions: Verify that directory and file permissions are set correctly for Nginx.
- Configure Index Files: Ensure the
index
directive 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.
Make your mark
Join the writer's program
Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.
Write for usBuild on top of Better Stack
Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.
community@betterstack.comor submit a pull request and help us build better products for everyone.
See the full list of amazing projects on github