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

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/`.
    
    ```bash
    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**
    
    ```bash
    sudo nginx -t
    ```
    
    Ensure there are no syntax errors in your configuration.
    
5. **Reload Nginx**
    
    ```bash
    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`).
    
    ```bash
    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`.
    
    ```bash
    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**
    
    ```bash
    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**
    
    ```bash
    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.