语法配置
Syntax: proxy_cache_path path;
Default: —
Context: http;
Syntax: proxy_cache zone | off;
Default: proxy_cache off
Context: http,server,location;
缓存过期周期
Syntax: proxy_cache_valid [code...] time;
Default: proxy_cache off
Context: http,server,location;
缓存的维度
Syntax: proxy_cache_key string;
Default: proxy_cache_key $scheme$proxy_host$request_uri;
Context: http,server,location;
示例
upstream imooc {
server 116.62.103.228:8001;
server 116.62.103.228:8002;
server 116.62.103.228:8003;
}
proxy_cache_path /opt/app/cache levels=1:2 keys_zone=imooc_cache:10m max_size=10g inactive=60m use_temp_path=off; # 定义缓存路径,分缓存目录,开辟缓存空间及大小,缓存最大容量,超过多少分钟没被使用则清理缓存
server {
listen 80;
server_name localhost jeson.t.imooc.io;
#charset koi8-r;
access_log /var/log/nginx/test_proxy.access.log main;
location / {
proxy_cache imooc_cache; #缓存空间
proxy_pass http://imooc; #反向代理上游几个服务器
proxy_cache_valid 200 304 12h; # 返回状态码200,304时缓存12个小时
proxy_cache_valid any 10m; # 其它缓存10分钟
proxy_cache_key $host$uri$is_args$args; # key的定义
add_header Nginx-Cache "$upstream_cache_status";
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; # 防止其中一台服务错误影响到整个资源获取流程
include proxy_params;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}