http { ... map $request_method $purge_method { PURGE 1; default 0; } }
server { listen 80; server_name www.example.com; location / { proxy_pass https://localhost:8002; proxy_cache mycache; proxy_cache_purge $purge_method; } }
$ curl-X PURGE-D-"https://www.example.com/*" HTTP/1.1 204 No Content Server: Nginx/1.15.0 Date: Sat, 19 May 2018 16:33:04 GMT Connection: keep-alive
geo $purge_allowed { default 0; # deny from other 10.0.0.1 1; # allow from localhost 192.168.0.0/24 1; # allow from 10.0.0.0/24 } map $request_method $purge_method { PURGE $purge_allowed; default 0; }
proxy_cache_path /data/Nginx/cache levels=1:2 keys_zone=mycache:10m purger=on;
http { ... proxy_cache_path /data/Nginx/cache levels=1:2 keys_zone=mycache:10m purger=on; map $request_method $purge_method { PURGE 1; default 0; } server { listen 80; server_name www.example.com; location / { proxy_pass https://localhost:8002; proxy_cache mycache; proxy_cache_purge $purge_method; } } geo $purge_allowed { default 0; 10.0.0.1 1; 192.168.0.0/24 1; } map $request_method $purge_method { PURGE $purge_allowed; default 0; } }
首先,确保 Nginx 是使用 Cache Slice 模块编译的。
使用 slice 指令定义切片的大小:
location / { slice 1m; }
选择使切片下载速度更快的切片大小。如果大小太小,则内存使用可能过多,并且打开了大量文件描述符。如果大小很大,则处理请求可能会导致延迟。将 $slice_range 变量添加到缓存键:
proxy_cache_key $uri$is_args$args$slice_range;
使用 206 状态代码启用响应缓存:
proxy_cache_valid 200 206 1h;
通过在 Range 标头字段中设置变量(即 $slice_range)来启用将范围请求传递到代理服务器:
proxy_set_header Range $slice_range;
location / { slice 1m; proxy_cache cache; proxy_cache_key $uri$is_args$args$slice_range; proxy_set_header Range $slice_range; proxy_cache_valid 200 206 1h; proxy_pass http://localhost:8000; }
http { ... proxy_cache_path /data/Nginx/cache keys_zone=one:10m loader_threshold=300 loader_files=200 max_size=200m; server { listen 8080; proxy_cache mycache; location / { proxy_pass http://backend1; } location /some/path { proxy_pass http://backend2; proxy_cache_valid any 1m; proxy_cache_min_uses 3; proxy_cache_bypass $cookie_nocache $arg_nocache$arg_comment; } } }