How Nginx Static‑Dynamic Separation Boosts Web Performance
This article explains the principle of Nginx static‑dynamic separation, shows how to configure location blocks and caching for static assets, and demonstrates how proxying dynamic requests to backend servers can reduce response time from 200 ms to 50 ms, increasing QPS several‑fold.
Static resources (HTML, CSS, JS, images, video) have fixed content and can be served directly from disk, while dynamic requests require application‑server processing such as database queries. Nginx, acting as a reverse‑proxy, can split these two traffic types using location directives.
Static‑Dynamic Classification Principle
Nginx examines the request URI; if the file extension matches a predefined list (e.g., jpg, png, gif, css, js, ico, webp), the request is treated as static. Otherwise it is forwarded to the backend application server cluster.
Typical Configuration
server {
# Match static file extensions
location ~* \.(jpg|jpeg|gif|png|css|js|ico|webp)$ {
root /usr/share/nginx/html/assets/;
expires 7d; # Enable strong caching
access_log off; # Reduce disk I/O for static files
}
# All other requests go to the dynamic backend
location / {
proxy_pass http://127.0.0.1:8080;
}
}The location block uses a regular expression ( ~*) to match static file extensions. The root directive points to the directory containing static assets. expires 7d adds a 7‑day cache header, and access_log off disables logging for static files to save I/O.
Dynamic requests are handled by the proxy_pass directive, which forwards traffic to an upstream application server (e.g., Tomcat, Gunicorn) listening on port 8080.
Performance Impact
By separating static and dynamic traffic, a typical website can reduce average response time from about 200 ms to 50 ms, and increase QPS by 2–5×, because static files are served directly by Nginx without involving backend processing.
Mike Chen's Internet Architecture
Over ten years of BAT architecture experience, shared generously!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
