naginx的代理缓存
ngx_http_proxy_module nginx的代理缓存需要这个模块
下面的内容接着上个反向代理和负载均衡的文章,可以去阅读我的上一篇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。
说明缓存成功了
到此这篇关于nginx代理的缓存配置的文章就介绍到这了,更多相关nginx代理缓存内容请搜索晓枫资讯以前的文章或继续浏览下面的相关文章希望大家以后多多支持晓枫资讯!
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |