4.1 proxy_cache_path
proxy_cache_path /data/cache levels=1:2 keys_zone=my_cache:100m max_size=500m inactive=3h;
server {
location /pp {
#在客户端收到的响应报头中验证缓存功能
add_header X-Cache $upstream_cache_status via $server_addr;
#代理至后端时,使用keep-alive
proxy_http_version 1.1;
proxy_set_header Connection "keep-alive";
#同时在后端服务器中LogFormat中启用%{X-Forwarded-For}i
#来记录真实client地址
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://websrv/remote;
proxy_cache my_cache;
#没有这行时,X-Cache一直是MISS
proxy_cache_valid 200 10s;
#为哪些请求方法启用缓存功能
proxy_cache_methods GET | HEAD | POST ...;
#设置缓存key,将用户私有信息用于键可以
#避免将用户的私有信息返回给其它用户
proxy_cache_key $remote_addr$request_uri;
}
}
4.2 proxy_cache_key
proxy_cache_key "$host$request_uri $cookie_user";
The default proxy_cache_key, which will fit most use cases, is
"\$scheme\$proxy_host\$request_uri"
4.3 proxy_cache_bypass
set $arg_cache 1;
proxy_cache_bypass $arg_nocache;
proxy_cache_bypass $cookie_nocache $arg_nocache $arg_comment;
proxy_cache_bypass $http_pragma $http_authorization;
按书上说的
proxy_cache_bypass $http_cache_bypass;
The configuration tells NGINX to bypass the cache if the HTTP
request header named cache_bypass is set to any value that is not 0.
没有用
4.4
location ~* \.(css|js)$ {
expires 1y;
add_header Cache-Control "public";
}