Resolving Nginx 403 Forbidden Error When Enabling Autoindex Module


5 views

When configuring Nginx's autoindex feature, the 403 Forbidden error typically stems from one of these three fundamental issues:

# Critical permission check commands
ls -ld /home/www/public/pics
stat /home/www/public/pics
ps aux | grep nginx

The minimal working configuration should include both proper root declaration and permissions:

server {
    listen 80;
    server_name www.domain.com;

    location /pics/ {
        root /home/www/public;
        autoindex on;
        
        # Essential security additions
        autoindex_exact_size off;
        autoindex_localtime on;
    }
}

Run these commands to verify and correct permissions:

chmod 755 /home/www
chmod 755 /home/www/public
chmod 755 /home/www/public/pics
chown -R nginx:nginx /home/www/public/pics

For systems with SELinux enabled (common on CentOS/RHEL):

# Check current context
ls -Z /home/www/public/pics

# Apply proper context
chcon -R -t httpd_sys_content_t /home/www/public/pics
semanage fcontext -a -t httpd_sys_content_t "/home/www/public/pics(/.*)?"
restorecon -Rv /home/www/public/pics

When basic fixes don't work, try these diagnostic techniques:

# Check error logs with tail
tail -f /var/log/nginx/error.log

# Test configuration before applying
nginx -t

# Verify worker process permissions
ps aux | grep nginx | grep -v grep

For more complex directory structures:

location ^~ /media/ {
    alias /mnt/storage/media/;
    autoindex on;
    disable_symlinks off;
    
    # Customize directory listing
    autoindex_format html;
    add_header X-Directory-Listing "enabled";
}

Remember to test after each configuration change and reload Nginx with systemctl reload nginx or nginx -s reload.


server {
    listen   80;
    server_name www.domain.com;

    access_log /home/www/log/access.log;
    error_log /home/www/log/error.log;

    location / {
        root   /home/www/public/;
        index  index.html index.php;
    }

    location /pics {
        autoindex on;
    }
}

The 403 Forbidden error in Nginx when using autoindex typically occurs due to permission issues or misconfigured root directives. The error appears even though the autoindex module is properly compiled into Nginx (which you've confirmed with nginx -V).

For autoindex to work properly, you need three essential elements:

location /directory {
    autoindex on;
    root /correct/path;
    allow all; # Optional security consideration
}

1. Missing Root Directive: Your current configuration lacks a root directive for the /pics location block.

location /pics {
    autoindex on;
    root /home/www/public; # Must match parent location's root
}

2. Permission Issues: Nginx worker process needs read access to the directory:

sudo chmod -R 755 /home/www/public/pics
sudo chown -R www-data:www-data /home/www/public/pics

Here's a fully functional configuration that addresses all potential issues:

server {
    listen 80;
    server_name www.domain.com;

    access_log /home/www/log/access.log;
    error_log /home/www/log/error.log;

    root /home/www/public;

    location / {
        index index.html index.php;
    }

    location /pics/ {
        autoindex on;
        autoindex_exact_size off;
        autoindex_localtime on;
        
        # Optional security settings
        satisfy any;
        allow all;
    }
}

If the error persists after implementing the above solutions:

# Check Nginx error logs
tail -f /home/www/log/error.log

# Verify directory permissions
namei -l /home/www/public/pics

# Test configuration before reloading
sudo nginx -t