Boost Web Performance 10x: Master Nginx Caching with Real-World Configs

This article explains how Nginx’s caching features can dramatically accelerate web services, detailing the cache workflow, control headers, and four practical configuration patterns—including static asset caching, reverse-proxy caching, sub-second caching, and FastCGI caching—complete with code examples and diagrams.

Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Boost Web Performance 10x: Master Nginx Caching with Real-World Configs

Nginx is a core component of large‑scale architectures; its caching capability can significantly improve response speed and reduce backend load.

Why Use Nginx Cache

Enabling cache can greatly lower backend pressure by avoiding repeated requests for static resources such as homepages, hot lists, images, and popular APIs, thereby saving CPU and memory and increasing concurrent handling capacity.

Cache Working Mechanism

When a client request arrives, Nginx first checks the local cache (disk or memory). If the request hits the cache, the cached content is returned directly; otherwise, Nginx forwards the request to the upstream server, stores the response in the cache, and returns it to the client.

Cache Control

The validity of cached content is governed by response headers such as Cache-Control, Expires, ETag, and Last-Modified, as well as Nginx directives like proxy_cache_valid and proxy_cache_key, which together define the caching strategy.

Cache Configuration

1. Static Resource Cache (Browser + Nginx)

<ol><li>location /static {</li><li>    root /var/www/html;</li><li>    expires 30d;</li><li>    add_header Cache-Control "public, max-age=2592000";</li><li>}</li></ol>

2. Reverse Proxy Cache

<ol><li>proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=my_cache:200m max_size=30g inactive=60m use_temp_path=off;</li><li>server {</li><li>    location /api/hot/ {</li><li>        proxy_pass http://backend_pool;</li><li>        proxy_cache my_cache;</li><li>        proxy_cache_key "$scheme$host$request_uri";</li><li>        proxy_cache_valid 200 302 10m;</li><li>        proxy_cache_valid 404 1m;</li><li>        proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;</li><li>        proxy_cache_lock on;</li><li>        proxy_cache_lock_timeout 5s;</li><li>        add_header X-Cache $upstream_cache_status;</li><li>    }</li><li>}</li></ol>

3. Sub‑Second Cache

<ol><li>location /api/top/ {</li><li>    proxy_cache my_cache;</li><li>    proxy_cache_valid 200 1s;</li><li>    proxy_cache_use_stale updating;</li><li>}</li></ol>

4. FastCGI Cache (PHP, WordPress, etc.)

<ol><li>fastcgi_cache_path /var/cache/nginx/fastcgi levels=1:2 keys_zone=fcg_cache:100m inactive=60m;</li><li>server {</li><li>    location ~ \.php$ {</li><li>        fastcgi_pass unix:/run/php-fpm.sock;</li><li>        fastcgi_cache fcg_cache;</li><li>        fastcgi_cache_key "$scheme$request_method$host$request_uri $cookie_user";</li><li>        fastcgi_cache_valid 200 302 10m;</li><li>        add_header X-Fastcgi-Cache $upstream_cache_status;</li><li>    }</li><li>}</li></ol>
Nginx缓存实战:这4个配置让性能飙升10倍!-mikechen
Nginx缓存实战:这4个配置让性能飙升10倍!-mikechen
Nginx缓存实战:这4个配置让性能飙升10倍!-mikechen
Nginx缓存实战:这4个配置让性能飙升10倍!-mikechen
Nginx缓存实战:这4个配置让性能飙升10倍!-mikechen
Nginx缓存实战:这4个配置让性能飙升10倍!-mikechen
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.

Nginxreverse proxyServer Configuration
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.