nginx代理的缓存

naginx的代理缓存

下面的内容接着上个反向代理和负载均衡的文章,可以去阅读我的上一篇nginx 的反向代理,比较连贯的理解

proxy_cache_path 定义代理缓存路径 这个是放在http服务下的,不要放在server下,下面是官方文档的描述

Syntax:	proxy_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [min_free=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

下面这里开始操作了

在我们的http服务里添加下面这段话,将缓存放在/tmp/proxy_cache文件里

proxy_cache_path /etc/nginx/proxy_cache levels=1:2 keys_zone=my_cache:100m max_size=10g inactive=1d use_temp_path=off;

然后配置我们代理的server服务

这里我们是缓存/images/目录的数据

location /images/ {
            proxy_cache_valid  200 304 301 302 2h;
            proxy_cache_valid  403 404 30s;
            proxy_cache_valid  any 5m;
            #缓存key的方法 以全路径md5值做做为Key, $is_args判断是否 参数,如果有参数 值为 ?,没有则为 空
            proxy_cache_key $host$uri$is_args$args;
             #增加缓存查看状态 头部
            add_header Nginx-Cache "$upstream_cache_status";
            #启用缓存my_cache
            proxy_cache my_cache;
                            proxy_pass   http://imageserver;
            proxy_set_header Host    $host;
            proxy_set_header X-Real-IP $remote_addr;           
        } 

这里是缓存/sound/目录的数据

location /sound/ {
            proxy_cache_valid  200 304 301 302 2h;
            proxy_cache_valid  403 404 30s;
            proxy_cache_valid  any 5m;
            #缓存key的方法 以全路径md5值做做为Key, $is_args判断是否 参数,如果有参数 值为 ?,没有则为 空
            proxy_cache_key $host$uri$is_args$args;
             #增加缓存查看状态 头部
            add_header Nginx-Cache "$upstream_cache_status";
            #启用缓存my_cache
            proxy_cache my_cache;

            proxy_pass   http://soundserver;
            proxy_set_header Host    $host;
            proxy_set_header X-Real-IP $remote_addr;
               }

根目录下的数据我们就不缓存了,等下测试一下缓存与不缓存的区别

配置好以后就可以重启我们的nginx测试了

[root@localhost ~]# curl 192.168.121.164:/images/
this is 8080.images
[root@localhost ~]# curl 192.168.121.164:/images/
this is 8081.images
[root@localhost ~]# curl 192.168.121.164:/images/
this is 8080.images
[root@localhost ~]# curl 192.168.121.164:/images/
this is 8081.images
[root@localhost ~]# vim /usr/local/nginx/conf/conf.d/game.conf 
[root@localhost ~]# systemctl restart nginx.service 
[root@localhost ~]# curl 192.168.121.164:/images/
this is 8080.images
[root@localhost ~]# curl 192.168.121.164:/images/
this is 8080.images
[root@localhost ~]# curl 192.168.121.164:/images/
this is 8080.images
[root@localhost ~]# curl 192.168.121.164:/images/
this is 8080.images
[root@localhost ~]# curl 192.168.121.164:/
this is 8080.com
[root@localhost ~]# curl 192.168.121.164:/
this is 8082.com
[root@localhost ~]# curl 192.168.121.164:/
this is 8081.com
[root@localhost ~]# curl 192.168.121.164:/
this is 8083.com
[root@localhost ~]# 

从上面这个测试数据可以看到,在没有修改我们的server配置代理缓存的时候

访问/imgaes/目录的时候是在8080和8081之间切换的,开启代理缓存以后

访问到8080的/images/之后都是访问的缓存,所以后面显示的都是8080的数据

访问根目录的时候可以正常切换其它节点,说明我们代理的负载均衡没有问题

访问的都是8080是因为缓存的存在

下面看看网页的缓存

这是访问根目录的抓包情况

在这里插入图片描述

这是第一次访问/sound/目录历览器的抓包情况

在这里插入图片描述

这是第二次访问/sound/目录的抓包情况

在这里插入图片描述

可以看到第一次访问/sound/目录的时候cache是MISS的说明未命中

因为第一次访问还没有缓存

而第二次访问的时候,cache就显示HIT说明缓存命中了,而且是返回304,而访问根目录的话刷新也都200.

说明访问的是缓存,而且都是8082。

说明缓存成功了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值