通過插件清理緩存需要手動(dòng)編譯模塊ngx_cache_purge,編譯時(shí)添加--add-module=./ngx_cache_purge-x-x
location ~ /purge(/.*) {
#設(shè)置只允許指定的IP或IP段才可以清除URL緩存。
allow 127.0.0.1;
allow 192.168.10.0/24;
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}
注意摔认,purge規(guī)則必須放于下面規(guī)則之上,否則就會(huì)優(yōu)先匹配到下面規(guī)則,會(huì)在清除緩存的時(shí)候報(bào)404錯(cuò)誤。
location ~ .*\.(gif|jpg|png|htm|html|css|js|flv|ico|swf)(.*) {
proxy_pass http://appserver;
proxy_redirect off;
#以域名分歇、URI、參數(shù)組合成Web緩存的Key值欧漱,Nginx根據(jù)Key值哈希职抡,存儲(chǔ)緩存內(nèi)容到二級緩存目錄內(nèi)
proxy_cache_key $host$uri$is_args$args;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_cache cache_one;
#對不同的HTTP狀態(tài)碼設(shè)置不同的緩存時(shí)間
proxy_cache_valid 200 302 1h;
proxy_cache_valid 301 1d;
proxy_cache_valid any 1m;
expires 30d;
}
設(shè)置cache日志的格式,可以在日志中查看“MISS”和“HIT”狀態(tài)
log_format cache '***$time_local ' '***$upstream_cache_status ' '***Cache-Control: $upstream_http_cache_control ' '***Expires: $upstream_http_expires ' '***"$request" ($status) ' '***"$http_user_agent" ';