合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
nginx的缓存模式: 缓存是通过键值存储的,键是存放在内存中,数据是放到磁盘上。在内存中找一块空间,每个键以哈希编码的方式放到内存,这个键所对应的值指向的是磁盘中缓存空间的内容的路径。磁盘上存放缓存的文件系统,层级不会太深。 1)命令 proxy_cache_path 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 loader_threshold----指定每次加载执行的时间 loader_files---每次加载的数量 loader_sleeps---每次加载的延时 inactive 非活动期限 max_size:设置磁盘中缓存数据的大小限制 案例: proxy_cache_path /data/nginx/cache keys_zone=one:10m loader_threshold=300 loader_files=200; (在内存中找10M的空间,并取名为one) file names in a cache will look like this: /data/nginx/cache/c/29/b7f54b2df7773722d382f4809d65029c 指定缓存哪些请求 nginx默认会缓存所有 get 和 head 方法的请求结果,缓存的key默认使用请求字符串 2)proxy_cache_min_uses Syntax: proxy_cache_min_uses number; Default: proxy_cache_min_uses 1; Context: http, server, location Sets the number of requests after which the response will be cached. 该指令用于设置客户端请求发送的次数,当客户端向被代理服务器发送相同请求达到指令设置的次数后,nginx服务器才会对请求的响应数据做缓存 默认为1次 proxy_cache_min_uses 2; 3)proxy_cache_key 该指令用于设置nginx服务器在内存中为缓存数据建立索引时使用的关键字 例如 proxy_cache_key "$host$request_uri$cookie_user"; proxy_cache_key “$scheme$proxy_host$uri$is_args$args” 4)缓存有效期 该指令可以针对不同的http响应状态设置不同的缓存时间 可以指定缓存有效时间,例如 proxy_cache_valid 200 302 10m; //响应状态码为200 302时,10分钟有效 proxy_cache_valid any 5m; //对应任何状态码,5分钟有效 5)是否使用过期的内容响应 proxy_cache_use_stale Syntax: proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | http_429 | off ...; Default: proxy_cache_use_stale off; Context: http, server, location 如果nginx在访问被代理服务器过程中出现,被代理服务器无法访问或者访问错误等现象,nginx服务器可以使用历史缓存响应用户端的请求,但这些数据不一定和被代理服务器上的最新数据相一致。 案例: proxy_cache_use_stale error invalid_header http_500 http_502 http_503 http_504 ; 6)缓存修剪(proxy_cache_purge) proxy_cache_path /data/nginx/cache keys_zone=cache_zone:10m; map $request_method $purge_method { PURGE 1; default 0; } server { ... location / { proxy_pass http://backend; proxy_cache cache_zone; proxy_cache_key $uri; proxy_cache_purge $purge_method; } } 7) proxy_cache_bypass 绕开缓存 用于设置nginx将不会从缓存存取数据 例如 proxy_cache_bypass $cookie_nocache $arg_nocache$arg_comment; proxy_cache_bypass $cookie_nocache $arg_nocache $arg_comment; proxy_cache_bypass $http_pragma $http_authorization; 案例: ![](https://box.kancloud.cn/76a7b2be587a8221c4812b6b5e85e432_1052x530.png) 第二部分:案例 在http段定义 proxy_cache_path /cache/nginx levels=1:2 keys_zone=mycache:32m; [root@proxy01 nginx]# mkdir -p /cache/nginx && chown nginx.nginx /cache/nginx [root@proxy01 nginx]# ll /cache/nginx/ -d drwxr-xr-x 2 nginx nginx 4096 Dec 20 16:18 /cache/nginx/ 在server段配置 location /bbs/ { proxy_cache mycache; proxy_cache_valid 200 1d; proxy_cache_valid 301 302 10m; proxy_cache_valid any 1m; proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504 http_429; proxy_cache_min_uses 2; proxy_cache_key "$host$request_uri"; proxy_pass http://10.100.100.106:8082/bbs/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } 第三部分:配置缓存修剪 1、下载ngx_cache_purge wget https://github.com/FRiCKLE/ngx_cache_purge/archive/2.3.zip https://codeload.github.com/FRiCKLE/ngx_cache_purge/zip/master 2、配置nginx location ~ /purge(/.*) { auth_basic "allow purge cache data"; auth_basic_user_file /usr/local/nginx/conf/conf.d/.htpasswd; allow 127.0.0.1; allow 10.100.100.0/24; deny all; proxy_cache_purge mycache $host$1$is_args$args; } location /bbs/ { proxy_cache mycache; proxy_cache_valid 200 1d; proxy_cache_valid 301 302 10m; proxy_cache_valid any 1m; proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504; proxy_cache_min_uses 2; proxy_cache_key $host$uri$is_args$args; proxy_pass http://10.100.100.106:8082/bbs/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } 3、安装httpd,并通过htpasswd设置密码 htpasswd -c -m /usr/local/nginx/conf/conf.d/.htpasswd tom 4、重启nginx服务 测试 [root@proxy01 nginx]# pwd /cache/nginx [root@proxy01 nginx]# ll total 0 (刚开始缓存没有内容) 此时访问https://test.51yuki.cn/bbs/a.html 然后在查看 [root@proxy01 nginx]# ll total 4 drwx------ 3 nginx nginx 4096 Dec 20 17:23 c [root@proxy01 nginx]# cd c/9a/ [root@proxy01 9a]# ll total 4 -rw------- 1 nginx nginx 456 Dec 20 17:23 36e85e58edcd96369da181c0eab5e9ac (发现有缓存咯,此时我们模拟修改后台服务器的内容) [root@node1 bbs]# echo "this a test file" >> a.html 然后在测试访问,发现还是原来的内容 我们就可以把缓存的内容修剪掉,访问如下 [root@node1 bbs]# echo "this a test file" >> a.html ![](https://box.kancloud.cn/a1cdb88dfe8f4951bbce63e3a949ac17_1100x310.png) 在访问,发现就是新的内容咯 ![](https://box.kancloud.cn/1ccd75a3a61cb9fa1e025740da88a7e3_567x134.png) 五、自定义响应报文的首部 通过add_header添加头部 语法: Syntax: add_header name value [always]; Default: — Context: http, server, location, if in location 案例: server { listen 443 ssl; server_name www.test.com; add_header X-Via $server_addr; add_header X-Cache $upstream_cache_status; ![](https://box.kancloud.cn/8ead34b364d4f6a6b79001ce8691185f_537x218.png)