Nginx缓存服务 通常情况下缓存是⽤来减少后端压⼒ , 将压⼒尽可能的往前推 , 减少后端压⼒,提⾼⽹站并发延时
缓存常⻅类型
缓存配置语法
缓存配置实践
缓存清理实践
部分⻚⾯不缓存
缓存⽇志记录统计
总结
缓存的分类
代理缓存原理
Nginx使用proxy_cache配置缓存
Nginx如果清理缓存
直接删除缓存目录
ngx_cache_purge 三方模块(必须编译Nginx)
Nginx如果指定页面不缓存
缓存常⻅类型 服务端缓存
代理缓存 获取服务端内容进⾏缓存
客户端浏览器缓存
Nginx 代理缓存原理
缓存配置语法 proxy_cache 配置 1 2 3 Syntax: proxy_cache zone | off; Default: proxy_cache off; Context: http, server, location
缓存路径 1 2 3 4 5 6 7 Syntax: proxy_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time][manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time]; Default: — Context: http
缓存过期周期 1 2 3 Syntax: proxy_cache_valid [code ...] time; Default: — Context: http, server, location
示例
1 2 3 proxy_cache_valid 200 302 10m; proxy_cache_valid 404 1m;
缓存的维度 就是要缓存的内容
1 2 3 Syntax: proxy_cache_key string; Default: proxy_cache_key $scheme$proxy_host$request_uri ; Context: http, server, location
示例
1 2 proxy_cache_key "$host$request_uri $cookie_user " ; proxy_cache_key $scheme$proxy_host$uri$is_args$args ;
缓存配置实践 配置Nginx :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 server { listen 8081; root /soft/code1; index index.html; } server { listen 8082; root /soft/code2; index index.html; } server { listen 8083; root /soft/code3; index index.html; }
代理配置缓存 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 proxy_cache_path /soft/cache levels=1:2 keys_zone=my_zone:10m max_size=10g inactive=60m use_temp_path=off; upstream my_cache { server 192.168.69.113:8081; server 192.168.69.113:8082; server 192.168.69.113:8083; } server { listen 80; server_name 192.168.69.12; location / { proxy_pass http://my_cache; proxy_cache my_zone; proxy_cache_valid 200 304 12h; proxy_cache_valid any 10m; 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; } }
测试:
1 2 3 4 5 curl -s -I http://192.168.56.11/url3.html|grep "Nginx-Cache" curl -s -I http://192.168.56.11/url3.html|grep "Nginx-Cache"
$upstream_cache_status
包含以下几种状态:
MISS
未命中,请求被传送到后端
HIT
缓存命中
EXPIRED
缓存已经过期请求被传送到后端
UPDATING
正在更新缓存,将使用旧的应答
STALE
后端将得到过期的应答
缓存清理实践 如何清理 proxy_cache 代理缓存
方法一 : 直接删除缓存文件 前面设置了proxy_cache_path
1 proxy_cache_path /soft/cache
之后只要删除这个目录下所有文件即可
方法二 : 第三方ngx_cache_purge 扩展 下载并编译 :
1 2 3 4 5 6 7 8 9 wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz tar xf ngx_cache_purge-2.3.tar.gz cd nginx-1.12.2/ && ./configure \--prefix=/server/nginx --add-module=../ngx_cache_purge-2.3 \ --with-http_stub_status_module --with-http_ssl_module make && make install
将上⽂的缓存proxy_cache.conf⽂件拷⻉⾄源码包中,并增加如下内容
1 2 3 4 5 6 location ~ /purge(/.*) { allow 127.0.0.1; allow 192.168.69.0/24; deny all; proxy_cache_purge code_cache $host$1$is_args$args ; }
访问182.168.69.112/purge/index.html
即可清除index.html
对应的缓存
部分⻚⾯不缓存 指定部分⻚⾯不进⾏ proxy_Cache 缓存
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 upstream cache{ server 192.168.69.113:8081; server 192.168.69.113:8082; server 192.168.69.113:8083; } proxy_cache_path /soft/cache levels=1:2 keys_zone=code_cache:10m max_size=10g inactive=60m use_temp_path=off; server { listen 80; server_name 192.168.69.112; if ($request_uri ~ ^/(login|register|password)) { set $cookie_nocache 1; } location / { proxy_pass http://cache; proxy_cache code_cache; proxy_cache_valid 200 304 12h; proxy_cache_valid any 10m; proxy_cache_key $host$uri$is_args$args ; proxy_no_cache $cookie_nocache $arg_nocache $arg_comment ; proxy_no_cache $http_pargma $http_authorization ; 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; } }
缓存⽇志记录统计 通过⽇志记录 proxy_cache 命中情况与对应 url
1 2 log_format main '$http_user_agent' '$request_uri' '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"' '"$upstream_cache_status"' ;
修改proxy_cache.conf, 在server标签新增access⽇志
1 access_log /var/log /nginx/proxy_cache.log main;