1.缓存配置语法
1)proxy_cache配置语法
Syntax: proxy_cache zone | off; Default: proxy_cache off; Context: http, server, location
2)缓存路径
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
3)缓存过期周期
Syntax: proxy_cache_valid [code ...] time; Default: — Context: http, server, location #示例 proxy_cache_valid 200 302 10m; proxy_cache_valid 404 1m;
4)缓存的维度
Syntax: proxy_cache_key string; Default: proxy_cache_key $scheme$proxy_host$request_uri; Context: http, server, location #示例 proxy_cache_key "$host$request_uri $cookie_user"; proxy_cache_key $scheme$proxy_host$uri$is_args$args;
2.代理配置缓存
[root@localhost ~]# mkdir /var/cache/nginx [root@localhost ~]# vim /etc/nginx/conf.d/proxy_cache.conf upstream cache { server 10.0.0.12:80; server 10.0.0.13:80; server 10.0.0.14:80; } #proxy_cache_path 存放缓存临时文件 #levels 按照两层目录分级 #keys_zone 开辟空间名, 10m:开辟空间大小, 1m可存放8000key #max_size 控制最大大小, 超过后Nginx会启用淘汰规则 #inactive 60分钟没有被访问缓存会被清理 #use_temp_path 临时文件, 会影响性能, 建议关闭 proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=code_cache:10m max_size=10g inactive=60m use_temp_path=off; server { listen 80; server_name 127.0.0.1; #proxy_cache 开启缓存 #proxy_cache_valid 状态码200|304的过期为12h, 其余状态码10分钟过期 #proxy_cache_key 缓存key #add_header 增加头信息, 观察客户端respoce是否命中 #proxy_next_upstream 出现502-504或错误, 会跳过此台服务器访问下台 location / { proxy_pass http://cache; proxy_cache code_cache; 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; } }
3.客户端测试
[root@localhost ~]# curl -s -I http://127.0.0.1/index.html | grep "Nginx-Cache" Nginx-Cache: MISS [root@localhost ~]# curl -s -I http://127.0.0.1/index.html | grep "Nginx-Cache" Nginx-Cache: HIT
4.缓存清理
1)删除已缓存数据
[root@proxy ~]# rm -rf /var/cache/nginx/* [root@localhost ~]# curl -s -I http://127.0.0.1/index.html | grep "Nginx-Cache" Nginx-Cache: MISS
2)使用ngx_cache_purge扩展模块清理,需要编译安装nginx(安装过程略)
修改配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf ... upstream cache { server 10.0.0.12:80; server 10.0.0.13:80; server 10.0.0.14:80; } proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=code_cache:10m max_size=10g inactive=60m use_temp_path=off; server { listen 80; server_name 127.0.0.1; location / { proxy_pass http://cache; proxy_cache code_cache; 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; } location ~ /purge(/.*) { allow 127.0.0.1; deny all; proxy_cache_purge code_cache $host$1$is_args$args; } }
...
清理缓存
[root@localhost ~]# curl http://127.0.0.1/purge/index.html
5.部分页面不缓存
1)指定部分页面不进行proxy_Cache缓存
[root@localhost conf.d]# vim proxy_cache.conf upstream cache{ server 10.0.0.12:80; server 10.0.0.13:80; server 10.0.0.14:80; } 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 127.0.0.1; 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; } }
2)清理原先的缓存
[root@localhost ~]# rm -rf /var/cache/nginx/*
3)请求测试
[root@localhost ~]# curl -s -I http://127.0.0.1/login | grep "Nginx-Cache" Nginx-Cache: MISS [root@localhost ~]# curl -s -I http://127.0.0.1/login | grep "Nginx-Cache" Nginx-Cache: MISS [root@localhost ~]# curl -s -I http://127.0.0.1/login | grep "Nginx-Cache" Nginx-Cache: MISS
6.缓存日志记录统计
通过日志记录proxy_cache命中情况与对应url
1)修改/etc/nginx/nginx.conf中log_format格式
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"';
2)查看访问日志(略)