#对不同的 HTTP 状态码设置不同的缓存时间
proxy_cache_valid 200 304 12h;
#以域名、URI、参数组合成 Web 缓存的 Key 值,Nginx 根据 Key 值哈希,存储缓存内容到
二级缓存目录内
proxy_cache_key $host$uri$is_args$args;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://backend_server;
expires 1d;
}
# 用 于 清 除 缓 存 , 假 设 一 个 URL 为 http://192.168.8.42/test.txt , 通 过 访 问
http://192.168.8.42/purge/test.txt 就可以清除该 URL 的缓存。
location ~ /purge(/.*)
{
#设置只允许指定的 IP 或 IP 段才可以清除 URL 缓存。
allow 127.0.0.1;
allow 192.168.0.0/16;
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}
#扩展名以.php、.jsp、.cgi 结尾的动态应用程序不缓存。
location ~ .*\.(php|jsp|cgi)?$
{
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://backend_server;
}
access_log off;
}
}