Nginx 提供兩種核心快取機制,搭配 PHP-FPM 與 Memcache/Redis,可構建多層次高效能快取架構。本文詳細比較 fastcgi_cache、proxy_cache 差異,提供生產級配置範例,並延伸說明 MySQL 記憶體表與 Memcache 特性。
| 特性 | proxy_cache | fastcgi_cache |
|---|---|---|
| 快取對象 | 後端伺服器回應(靜態/動態皆可) | FastCGI 應用產生的內容(通常為 PHP 動態頁) |
| 運作層級 | HTTP 反向代理層 | FastCGI 協定層 |
| 減少通訊 | Nginx ↔ 後端伺服器 | Nginx ↔ PHP-FPM |
| 降低壓力 | 後端伺服器、網路傳輸 | PHP 進程、資料庫 |
| 適用場景 | 微服務代理、靜態資源代理、跨語言後端 | PHP 應用頁面快取、API 回應快取 |
# 必須同分區
proxy_temp_path /data0/proxy_temp_dir;
# 快取區設定:名稱 cache_one、記憶體 200MB、1天未存取清除、硬碟 30GB
proxy_cache_path /data0/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
server {
listen 80;
server_name www.example.com;
root /data0/htdocs/www;
location / {
# 故障轉移
proxy_next_upstream http_502 http_504 error timeout invalid_header;
# 啟用快取
proxy_cache cache_one;
# 不同狀態碼快取時間
proxy_cache_valid 200 304 12h;
proxy_cache_valid 301 302 1h;
proxy_cache_valid any 1m;
# 快取 Key:域名 + URI + 參數
proxy_cache_key $host$uri$is_args$args;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass https://backend_server;
expires 1d;
}
# 手動清除快取:訪問 /purge/實際路徑
location ~ /purge(/.*) {
allow 127.0.0.1;
allow 192.168.0.0/16;
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}
# 動態副檔名不快取
location ~ \.(php|jsp|cgi)$ {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass https://backend_server;
}
}
# 快取路徑:三級目錄、區塊 NAME 2880MB、2天未存取清除、硬碟 10GB
fastcgi_cache_path /tt/cache levels=1:2 keys_zone=NAME:2880m inactive=2d max_size=10G;
# 快取 Key 組合
fastcgi_cache_key "$scheme$request_method$host$uri$arg_filename$arg_x$arg_y";
server {
listen 8080;
server_name www.example.com;
root /www;
location / {
index index.html index.htm index.php;
}
location ~ \.php$ {
root /www;
fastcgi_pass 127.0.0.1:9000;
# 啟用快取
fastcgi_cache NAME;
fastcgi_cache_valid 200 48h;
fastcgi_cache_min_uses 1;
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi.conf;
# 關鍵:允許傳遞 Set-Cookie(預設快取會隱藏)
fastcgi_pass_header Set-Cookie;
}
}
levels=1:2:二級子目錄結構,避免單目錄檔案過多影響效能keys_zone=NAME:200m:共享記憶體區大小,存放快取索引(Key + 中繼資料)inactive=1d:未被存取多久移除(非過期時間)max_size=30g:硬碟快取上限,超過時依 LRU 淘汰fastcgi_cache_min_uses 1:請求幾次才快取(1=首次即快取)fastcgi_cache_use_stale:上游錯誤/逾時時提供舊快取,提升可用性fastcgi_pass_header Set-Cookie:關鍵,否則登入狀態 Cookie 會被快取吞噬CREATE TABLE test (
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
state CHAR(10),
type CHAR(20),
date CHAR(30)
) ENGINE=MEMORY DEFAULT CHARSET=utf8;
| 特性 | MEMORY 表 | Memcache |
|---|---|---|
| 資料持久性 | 重啟遺失,結構保留 | 重啟遺失 |
| 資料類型支援 | 不支援 BLOB/TEXT/VARCHAR(長) | 支援任意二進位資料 |
| 索引 | 支援 Hash/B-tree | 無索引,單純 Key-Value |
| 交易 | 不支援 | 不支援 |
| 鎖粒度 | 表鎖(高併發寫入效能差) | 無鎖(單執行緒、極高併發讀寫) |
| 容量限制 | max_heap_table_size | 32位 4GB、64位無實際上限 |
| TTL 過期 | 不支援 | 支援(最長 30 天) |
請求 → Nginx fastcgi_cache (頁面級、秒~小時)
→ PHP 邏輯層 → Redis/Memcache (物件級、資料結構、TTL 精確控制)
→ MySQL (持久化、關聯查詢、交易)
合理配置 fastcgi_cache 可讓大多數匿名訪問繞過 PHP 直達 Nginx,配合 Redis 做細粒度資料快取,是高流量 PHP 應用的標準架構模式。