Boost Web Performance: Master Nginx Static & Dynamic Separation

This article explains how Nginx static‑and‑dynamic separation works, why it dramatically improves response speed and concurrency, and provides complete configuration examples for serving static assets, proxying dynamic requests, and integrating caching, CDN and load‑balancing for optimal web performance.

Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Boost Web Performance: Master Nginx Static & Dynamic Separation

Nginx static & dynamic separation (also called dynamic‑static separation) is a common web‑service optimization technique that routes dynamic requests (e.g., PHP, Java, Python) and static resources (HTML, CSS, JS, images, videos) to different handling paths.

Benefits

By letting Nginx serve static files directly, the backend application servers experience reduced load, overall performance improves, and Nginx’s ability to handle concurrent static requests far exceeds that of typical backend servers.

Architecture Diagram

Nginx static/dynamic separation architecture
Nginx static/dynamic separation architecture

Static Request Configuration

server {
    listen 80;
    server_name example.com;
    # 静态资源路径匹配
    location ~* \.(html|css|js|jpg|png|ico|mp4) {
        root /data/static;
        expires 30d;               # cache for 30 days
        access_log off;            # optional: disable logging to reduce I/O
    }
}

Dynamic Request Configuration

location /api/ {
    proxy_pass http://backend_server;   # forward to backend (Tomcat, Node, etc.)
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;  # preserve client IP
}

Complete Server Block Example

server {
    listen 80;
    server_name your_domain.com;
    root /data/www/html;               # default website root (may exclude static files)

    # Serve static assets first
    location ~* \.(html|htm|gif|jpg|jpeg|png|bmp|swf|css|js|woff|woff2|ttf|svg|ico) {
        root /data/www/static;
        expires 30d;
        access_log off;
        log_not_found off;
    }

    # Proxy API requests to backend
    location /api/ {
        proxy_pass http://backend_server;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }

    # Handle PHP files via FastCGI
    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    # Additional dynamic language handlers can be added here (e.g., Python, Java)
}

Combining Nginx caching, a CDN, and load‑balancing can further amplify website performance and reliability.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

configurationcachingweb performanceNginxstatic assetsdynamic requests
Mike Chen's Internet Architecture
Written by

Mike Chen's Internet Architecture

Over ten years of BAT architecture experience, shared generously!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.