06 Nginx Proxy_cache 缓存服务


Nginx 缓存服务

常见缓存类型

缓存配置语法

缓存配置实践

1
2
CentOS7.4 Nginx Proxy 172.17.70.227
CentOS7.4 Nginx Web 172.17.70.226

web 节点准备

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 目录建立
[root@nginx soft]# mkdir -p /soft/code{1..3}

# html⽂件

[root@nginx code]# for i in {1..3};do echo Code1-Url$i > /soft/code1/url$i.html;done
[root@nginx code]# for i in {1..3};do echo Code2-Url$i > /soft/code2/url$i.html;done
[root@nginx code]# for i in {1..3};do echo Code3-Url$i > /soft/code3/url$i.html;done

[root@nginx code1]# ls -l
total 12
-rw-r--r-- 1 root root 11 Oct 22 16:47 url1.html
-rw-r--r-- 1 root root 11 Oct 22 16:47 url2.html
-rw-r--r-- 1 root root 11 Oct 22 16:47 url3.html
[root@nginx code1]# cat url1.html
Code1-Url1

启动三个端口监听

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
[root@nginx conf.d]# vim web_node.conf 

server {
listen 8081;
server_name 172.17.70.226;
index index.html;
root /soft/code1;
}

server {
listen 8082;
server_name 172.17.70.226;
index index.html;
root /soft/code2;
}


server {
listen 8083;
server_name 172.17.70.226;
index index.html;
root /soft/code3;
}

[root@nginx conf.d]# nginx -t
[root@nginx conf.d]# nginx -s reload
1
2
3
4
5
6
[root@nginx code1]# curl 172.17.70.226:8081/url1.html
Code1-Url1
[root@nginx code1]# curl 172.17.70.226:8081/url2.html
Code1-Url2
[root@nginx code1]# curl 172.17.70.226:8081/url3.html
Code1-Url3

代理配置缓存

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
34
35
36
37
38
39
40
41
42
# 创建缓存目录
[root@proxy ~]# mkdir -p /soft/cache

# 配置文件
[root@proxy conf.d]# vim proxy_cache.conf

proxy_cache_path /soft/cache levels=1:2 keys_zone=code_cache:10m max_size=10g inactive=60m use_temp_path=off;

# 缓存路径 proxy_cache_path /soft/cache
# 目录结构级别 levels=1:2 一般都是2层 多了效率会底下 按照两层⽬录分级
# 空间名称+10M keys_zone=code_cache:10m
# 最大缓存文件只能是10G 超过就把最弱剔除 max_size=10g
# 60分钟没有被访问缓存会被清理 inactive=60m
# 临时⽂件,会影响性能,建议关闭 use_temp_path=off

upstream cache {
server 172.17.70.226:8081;
server 172.17.70.226:8082;
server 172.17.70.226:8083;
}

server {
listen 80;
server_name 60.205.217.112;
index index.html;

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;
}
}


# proxy_cache # 开启缓存
# proxy_cache_valid # 状态码200|304的过期为12h,其余状态码10分钟过期
# add_header # 可以看response头部信息 看是否命中
# proxy_next_upstream # 超时和500+请求 就会被直接跳转到第二台请求
1
2
[root@proxy conf.d]# nginx -t
[root@proxy conf.d]# nginx -s reload

客户端测试

1
2


1
2
3
4
5
6
7
8
9
10
11
12
# 查看缓存内容:
[root@proxy conf.d]# cd /soft/cache/
[root@proxy cache]# ls
7 8 d

# 关闭缓存 回归正常轮询
proxy_cache off;
nginx -s reload

# 清理缓存
[root@proxy cache]# rm -rf *
# 页面会重新开始缓存,第一次都是MISS

Nginx 清理缓存方式

1
2
[root@proxy /]# cd /soft/cache/
[root@proxy cache]# rm -rf *
1
2
3
# curl 测试缓存
[root@proxy conf.d]# curl -I http://60.205.217.112/url3.html | grep "Nginx-Cache"
Nginx-Cache: HIT

ngx_cache_purge 扩展模块清理

部分页面 不缓存

指定部分页面 不参加 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
32
33
[root@proxy conf.d]# vim proxy_cache.conf 

proxy_cache_path /soft/cache levels=1:2 keys_zone=code_cache:10m max_size=10g inactive=60m use_temp_path=off;
upstream cache {
server 172.17.70.226:8081;
server 172.17.70.226:8082;
server 172.17.70.226:8083;
}

server {
listen 80;
server_name 60.205.217.112;
index index.html;

location / {

if ($request_uri ~ ^/(url3|login|register|password)) {
set $cookie_nocache 1;
}

proxy_pass http://cache;
proxy_cache code_cache;
proxy_cache_valid 200 304 12h;
proxy_cache_valid any 10m;
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;
}
}

[root@proxy conf.d]# nginx -s reload
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 清理缓存
rm -rf /soft/cache/*

[root@proxy soft]# curl -I http://60.205.217.112/url1.html | grep "Nginx-Cache"
Nginx-Cache: MISS

[root@proxy soft]# curl -I http://60.205.217.112/url1.html | grep "Nginx-Cache"
Nginx-Cache: HIT

[root@proxy soft]# curl -I http://60.205.217.112/url3.html | grep "Nginx-Cache"
Nginx-Cache: MISS

[root@proxy soft]# curl -I http://60.205.217.112/url3.html | grep "Nginx-Cache"
Nginx-Cache: MISS

缓存日志 记录统计

1
2
3
4
5
# 修改/etc/nginx/nginx.conf中log_format格式
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"' '"$upstream_cache_status"';
1
2
3
4
# 修改proxy_cache.conf,在server标签新增access⽇志
# 只记录该sever的访问日志

access_log /var/log/nginx/proxy_cache.log main;

1
2
3
4
5
[root@proxy soft]# nginx -s reload

[root@proxy soft]# curl -I http://60.205.217.112/url1.html | grep "Nginx-Cache"
[root@proxy soft]# curl -I http://60.205.217.112/url2.html | grep "Nginx-Cache"
[root@proxy soft]# curl -I http://60.205.217.112/url3.html | grep "Nginx-Cache"

nginx cache 查看命中率

1
http://www.361way.com/nginx-cache/2665.html

Nginx 缓存总结

1
2
3
4
5
6
7
8
9
10
11
1. 缓存的分类:
1. 客户端缓存
2. 代理缓存
3. 服务端缓存

2. Nginx 配置缓存
1. proxy_cache
3. Nginx 如何清理缓存
1. 直接删除缓存目录
2. 使用ngx_cache_purge 编译后使用第三方模块
4. Nginx 可以指定页面不参与缓存