Nginx 403 Error: Directory Index of [Folder] Is Forbidden

Better Stack Team
Updated on October 7, 2024

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.

  1. 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
    
  2. Add or Update the location Block

    Add 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.

  3. Save and Close the File

  4. Test the Configuration

     
    sudo nginx -t
    

    Ensure there are no syntax errors in your configuration.

  5. 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.

  1. 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
    
  2. 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 by www-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.

  1. Modify the index Directive

    Make sure your location block specifies an index file:

     
    location / {
        root /var/www/html;
        index index.html index.htm;
    }
    

    This tells Nginx to serve index.html or index.htm if present in the directory.

  2. Save and Close the File

  3. 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.

  1. 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 your location 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.

Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

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 us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build 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.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github