11. Nginx緩存

緩存類型

  1. 瀏覽器緩存
  2. Nginx代理服務器緩存
  3. 服務器緩存,例如Redis、Memcache等

緩存配置

http://nginx.org/en/docs/http...
upstream backend {
    server x.x.x.x:1023;
    server x.x.x.x:1024;
    server x.x.x.x:1025;
}

#緩存存放的位置是 /data/nginx/cache,目錄層級爲兩層,最多存放10G緩存
#緩存key存放的內存空間是 default_cache ,單個緩存最大爲 10m
#自動移除60分鐘內沒有人訪問的緩存
#在將緩存放置到 proxy_cache_path 以前,不使用 use_temp_path

proxy_cache_path /data/nginx/cache levels=1:2 max_size=10g keys_zone=default_cache:10m inactive=60m use_temp_path=off;

server {
    ...
    location / {
        #使用backend負載均衡組
        proxy_pass http://backend;
        #使用 default_cache 緩存
        proxy_cache default_cache;
        #200、304的響應緩存時間爲12h
        proxy_cache_valid 200 304 12h;
        #非200、304的響應緩存時間爲10m
        proxy_cache_valid any 10m;
        #使用$uri做爲緩存key
        proxy_cache_key $uri;
        #對於如下特定狀況,繼續請求負載均衡組裏面的其餘服務器
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        #若是請求中含有參數或者爲受權請求,則不緩存 
        proxy_no_cache $http_pragma    $http_authorization;
        #向客戶端返回一個是否擊中緩存的頭信息
        add_header Nginx-Cache "$upstream_cache_status";
    }
}

緩存清理

  • rm -rf 緩存目錄
  • 使用 ngx_cache_purge 模塊清理特定緩存

專題閱讀

相關文章
相關標籤/搜索