HxHippy

Static File Serving

Optimizing Nginx for serving static files efficiently.

Last updated: 2025-01-15

Basic Static File Configuration

server {
    listen 80;
    server_name static.example.com;

    root /var/www/static;

    location / {
        try_files $uri $uri/ =404;
    }
}

Optimized Static Serving

server {
    listen 80;
    server_name example.com;

    root /var/www/html;

    # Enable sendfile for efficiency
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;

    # Disable access log for static files (performance)
    location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff|woff2)$ {
        access_log off;
        expires 30d;
        add_header Cache-Control "public, immutable";
    }
}

Asset Caching Strategy

# Short-lived assets (may change)
location ~* \.(html|htm)$ {
    expires -1;
    add_header Cache-Control "no-store, no-cache, must-revalidate";
}

# CSS and JS (version in filename or query)
location ~* \.(css|js)$ {
    expires 1y;
    add_header Cache-Control "public, immutable";
}

# Images
location ~* \.(jpg|jpeg|png|gif|ico|svg|webp)$ {
    expires 30d;
    add_header Cache-Control "public";
}

# Fonts
location ~* \.(woff|woff2|ttf|eot|otf)$ {
    expires 1y;
    add_header Cache-Control "public, immutable";
    add_header Access-Control-Allow-Origin *;
}

# Media
location ~* \.(mp4|webm|ogg|mp3|wav)$ {
    expires 30d;
    add_header Cache-Control "public";
}

Serving from Alias

# Serve static from different directory
location /static/ {
    alias /var/www/static/;
    expires 30d;
}

# Or with root (appends location)
location /assets/ {
    root /var/www;  # Serves from /var/www/assets/
    expires 30d;
}

Directory Listing

# Enable directory listing
location /files/ {
    alias /var/www/files/;
    autoindex on;
    autoindex_exact_size off;  # Show KB/MB instead of bytes
    autoindex_localtime on;
}

File Download Headers

# Force download
location /downloads/ {
    alias /var/www/downloads/;

    if ($request_filename ~* \.(pdf|zip|rar)$) {
        add_header Content-Disposition "attachment";
    }
}

# Better approach with types
location /downloads/ {
    alias /var/www/downloads/;
    types { }
    default_type application/octet-stream;
}

Security for Static Files

# Deny access to hidden files
location ~ /\. {
    deny all;
}

# Deny access to sensitive files
location ~* \.(env|log|sql|bak|old|tmp)$ {
    deny all;
}

# Deny access to source files
location ~* \.(php|py|pl|sh)$ {
    deny all;
}

ETag Configuration

# Enable ETag (default)
etag on;

# Disable ETag (if using expires)
location /static/ {
    etag off;
    expires 1y;
}

Range Requests (Video/Audio)

location /videos/ {
    alias /var/www/videos/;

    # Enable range requests for seeking
    max_ranges 1;

    # Large files
    sendfile on;
    tcp_nopush on;
    aio threads;
    directio 512;
}

Complete Static Server

server {
    listen 80;
    server_name static.example.com;

    root /var/www/static;

    # Performance
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;

    # Gzip
    gzip on;
    gzip_static on;
    gzip_types text/plain text/css application/javascript image/svg+xml;

    # Default
    location / {
        try_files $uri $uri/ =404;
    }

    # Images
    location ~* \.(jpg|jpeg|png|gif|ico|svg|webp)$ {
        expires 30d;
        access_log off;
    }

    # CSS/JS
    location ~* \.(css|js)$ {
        expires 1y;
        access_log off;
    }

    # Fonts
    location ~* \.(woff|woff2|ttf|eot|otf)$ {
        expires 1y;
        access_log off;
        add_header Access-Control-Allow-Origin *;
    }

    # Security
    location ~ /\. {
        deny all;
    }
}
beginner Performance Updated 2025-01-15
  • nginx
  • static files
  • assets
  • performance
  • caching