Nginx缓存服务

通常情况下缓存是⽤来减少后端压⼒, 将压⼒尽可能的往前推, 减少后端压⼒,提⾼⽹站并发延时

  1. 缓存常⻅类型
  2. 缓存配置语法
  3. 缓存配置实践
  4. 缓存清理实践
  5. 部分⻚⾯不缓存
  6. 缓存⽇志记录统计

总结

  • 缓存的分类
    • 客户端缓存
    • 代理缓存
    • 服务端缓存
  • 代理缓存原理
  • Nginx使用proxy_cache配置缓存
  • Nginx如果清理缓存
    • 直接删除缓存目录
    • ngx_cache_purge 三方模块(必须编译Nginx)
  • Nginx如果指定页面不缓存

缓存常⻅类型

服务端缓存

1575730058647

代理缓存

获取服务端内容进⾏缓存

1575730081046

客户端浏览器缓存

1575730103900

Nginx 代理缓存原理

1575730176741

缓存配置语法

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
# 将200,302的状态码缓存10分钟 , 404缓存一分钟
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存放缓存临时⽂件
#levels 按照两层⽬录分级
#keys_zone 开辟空间名, 10m:开辟空间⼤⼩, 1m可存放8000key
#max_size 控制最⼤⼤⼩, 超过后Nginx会启⽤淘汰规则
#inactive 60分钟没有被访问缓存会被清理
#use_temp_path 临时⽂件, 会影响性能, 建议关闭
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;
#proxy_cache 开启缓存
#proxy_cache_valid 状态码200|304的过期为12h, 其余状态码10分钟过期
#proxy_cache_key 缓存key
#add_header 增加头信息, 观察客户端response是否命中(第一次访问,没有缓存,所以是miss,之后都是hit)
#proxy_next_upstream 出现502-504或错误, 会跳过此台服务器访问下一台
#proxy_params 代理的基本配置
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"
# Nginx-Cache: MISS

curl -s -I http://192.168.56.11/url3.html|grep "Nginx-Cache"
# Nginx-Cache: HIT

$upstream_cache_status包含以下几种状态:

  • MISS 未命中,请求被传送到后端
  • HIT 缓存命中
  • EXPIRED 缓存已经过期请求被传送到后端
  • UPDATING 正在更新缓存,将使用旧的应答
  • STALE 后端将得到过期的应答

缓存清理实践

如何清理 proxy_cache 代理缓存

方法一 : 直接删除缓存文件

前面设置了proxy_cache_path

1
proxy_cache_path /soft/cache

之后只要删除这个目录下所有文件即可

1
rm -rf /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;
# 如果uri匹配 , 设置$cookie_nocache为1(如果$cookie_nocache非0,代表不缓存)
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
# 修改/etc/nginx/nginx.conf中log_format格式 , 添加$upstream_cache_status
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;