How to Configure NGINX to Serve Static Files from a Directory with Autoindex


3 views

When you need to serve static files from a directory using NGINX, the configuration primarily revolves around the server block in your NGINX configuration files. The default configuration usually lives in /etc/nginx/sites-available/default or similar.

Here's the essential configuration to serve files from /media/user/data:

server {
    listen 80;
    server_name yourdomain.com;

    location /data {
        alias /media/user/data/;
        autoindex on;
    }
}

alias vs root: While both can be used, alias is generally better for this use case as it maps the URL path exactly to the filesystem path.

autoindex on: This enables directory listing, allowing users to browse through files and subdirectories.

If you encounter 403 errors, check these aspects:

# Verify NGINX user has access
sudo chown -R www-data:www-data /media/user/data
sudo chmod -R 755 /media/user/data

# Ensure parent directories have execute permission
sudo chmod +x /media
sudo chmod +x /media/user

For better control, consider these additional directives:

location /data {
    alias /media/user/data/;
    autoindex on;
    autoindex_exact_size off;
    autoindex_localtime on;
    
    # Security restrictions
    allow 192.168.1.0/24;
    deny all;
}

After making changes:

sudo nginx -t  # Test configuration
sudo systemctl restart nginx

Then access http://yourdomain.com/data to verify the directory listing works.

Issue: "403 Forbidden" despite correct permissions
Solution: Check SELinux status (on CentOS/RHEL):

sudo setenforce 0  # Temporarily disable
# Or set proper context:
sudo chcon -Rt httpd_sys_content_t /media/user/data

Issue: Directory listing not showing
Solution: Ensure autoindex on is present and not being overridden by other configuration files.


When setting up static file serving in NGINX, we need three critical components:

  1. Proper root directory configuration
  2. Directory listing (autoindex) functionality
  3. Correct file permissions

Here's the complete server block configuration that should be placed in either /etc/nginx/sites-available/default or a new configuration file in /etc/nginx/conf.d/:


server {
    listen 80;
    server_name yourdomain.com;

    location /data {
        alias /media/user/data/;
        autoindex on;
        
        # Optional security settings
        autoindex_exact_size off;
        autoindex_localtime on;
        
        # Enable CORS if needed
        add_header Access-Control-Allow-Origin *;
    }
}

The critical distinction between root and alias directives:

  • root: Appends the URI to the path (e.g., request for /data/file becomes /media/user/data/data/file)
  • alias: Maps the URI exactly to the path (e.g., request for /data/file becomes /media/user/data/file)

The common permission issues and solutions:


# Check current permissions
ls -ld /media/user/data

# Set proper permissions (choose appropriate level)
sudo chmod -R 755 /media/user/data
sudo chown -R www-data:www-data /media/user/data

For production environments, consider these additional settings:


location /data {
    # Basic settings
    alias /media/user/data/;
    autoindex on;
    
    # Security enhancements
    disable_symlinks off; # Or 'on' for better security
    
    # Performance tweaks
    sendfile on;
    tcp_nopush on;
    
    # Cache control headers
    expires 1d;
    add_header Cache-Control "public";
}

After making changes, always:

  1. Test configuration syntax: sudo nginx -t
  2. Reload NGINX: sudo systemctl reload nginx
  3. Verify access: curl -I http://localhost/data/
Issue Solution
403 Forbidden Check directory permissions and SELinux contexts
404 Not Found Verify path mappings (alias vs root)
Symbolic links not working Set disable_symlinks off