Mastering Nginx Static‑Dynamic Separation: Boost Performance & Maintainability
This article explains the concept of static‑dynamic separation in web architecture, outlines its performance and maintenance benefits, and provides detailed Nginx configuration examples—including location blocks for static assets, API proxying, and fallback routing—to illustrate how to implement the technique effectively.
Static‑dynamic separation is a common technique in large‑scale web architectures. It divides a site into static resources (HTML, CSS, JavaScript, images, video, audio, fonts) and dynamic content that requires backend processing and database interaction.
Separating these concerns improves performance, scalability, and maintainability: static files can be served directly by Nginx or a CDN with aggressive caching, while backend developers focus on business logic and API handling.
The core of Nginx static‑dynamic separation lies in the location directive. Nginx matches the request URI against location blocks and routes the request accordingly.
Request Flow
When a client request arrives, Nginx determines whether the URI points to a static resource or a dynamic endpoint. Static requests are read from the local file system, whereas dynamic requests are proxied to backend services such as FastCGI, Tomcat, or Node.js.
Typical Nginx Configuration
server {
listen 80;
server_name www.example.com;
# Static assets
location ^~ /static/ {
root /usr/share/nginx/html;
expires 30d;
access_log off;
}
# API proxy
location /api/ {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# Fallback for front‑end routing
location / {
root /usr/share/nginx/html;
index index.html;
try_files $uri $uri/ /index.html;
}
}The ^~ /static/ prefix tells Nginx to serve any request starting with /static/ directly from the local static directory. The /api/ block forwards API calls to a Java backend (or any other service). The final location / block handles all other requests, supporting single‑page applications by falling back to index.html.
By isolating static resources from dynamic processing, the overall response time is reduced and the system becomes easier to maintain.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
