首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

深入 Nginx + PHP 快取機制詳解:fastcgi_cache、proxy_cache 與 Memcache

     編輯:PHP 相關知識

Nginx 提供兩種核心快取機制,搭配 PHP-FPM 與 Memcache/Redis,可構建多層次高效能快取架構。本文詳細比較 fastcgi_cache、proxy_cache 差異,提供生產級配置範例,並延伸說明 MySQL 記憶體表與 Memcache 特性。

Nginx 兩大快取機制核心差異

特性proxy_cachefastcgi_cache
快取對象後端伺服器回應(靜態/動態皆可)FastCGI 應用產生的內容(通常為 PHP 動態頁)
運作層級HTTP 反向代理層FastCGI 協定層
減少通訊Nginx ↔ 後端伺服器Nginx ↔ PHP-FPM
降低壓力後端伺服器、網路傳輸PHP 進程、資料庫
適用場景微服務代理、靜態資源代理、跨語言後端PHP 應用頁面快取、API 回應快取

proxy_cache 生產配置範例

# 必須同分區
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;
    }
}

fastcgi_cache 生產配置範例

# 快取路徑:三級目錄、區塊 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 會被快取吞噬

MySQL 記憶體表 vs Memcache 比較

MySQL MEMORY 引擎表

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_size32位 4GB、64位無實際上限
TTL 過期不支援支援(最長 30 天)

Memcache 核心用途

  1. 提升併發能力: 將熱點資料(Session、熱門商品、計數器)放入記憶體,減輕 DB 壓力
  2. 減輕資料庫負擔: 非關鍵、可重算、頻繁讀取的資料優先走快取

多層快取架構建議

請求 → Nginx fastcgi_cache (頁面級、秒~小時)
    → PHP 邏輯層 → Redis/Memcache (物件級、資料結構、TTL 精確控制)
    → MySQL (持久化、關聯查詢、交易)

合理配置 fastcgi_cache 可讓大多數匿名訪問繞過 PHP 直達 Nginx,配合 Redis 做細粒度資料快取,是高流量 PHP 應用的標準架構模式。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved