0
点赞
收藏
分享

微信扫一扫

nginx proxy_cache 无法生效问题解决

alonwang 2022-10-07 阅读 70

nginx proxy_cache 是一个比较有用的东西,可以对于系统请求的资源进行cache,可以提升系统的性能

参考玩法

nginx proxy_cache 无法生效问题解决_ide

 

 

参考资料

  • cache zong

proxy_cache_path  /data/nginx_caches2  levels=1:2   keys_zone=static_cache1:256m max_size=10g;

  • proxy cache 配置

location ~ .*\.(gif|jpg|jpeg|png|css|js|ico|mp4) {

几个参数说明

location ~ .*\.(gif|jpg|jpeg|png|css|js|ico|mp4) { 
proxy_cache static_cache1;
proxy_cache_key $uri$is_args$args;
expires 30d;
client_body_buffer_size 10M;
client_max_body_size 10G;
proxy_buffering on;
proxy_buffers 1024 4k;
proxy_cache_valid any 48h;
proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504 http_429;
proxy_read_timeout 300;
add_header X-Proxy-Cache $upstream_cache_status;
proxy_set_header Host $host;
set $cip $remote_addr;
if ($http_x_forwarded_for != "") {
set $cip $http_x_forwarded_for;
}
proxy_set_header X-Forwarded-For $cip;
proxy_pass http://proxy_resource;
}

很多时候我们需要配置 proxy_cache_valid 以及proxy_buffering on ,同时还需要注意cache zone 的目录是否有权限,比如nginx 使用了默认的nobody 自己手工创建
了文件夹也会出现问题,如果为了简单可以直接proxy_cache_valid 配置any,对于我碰到的问题包含了上边说的几点,但是也会有其他问题,类似的一些,提供开源
项目nginx-proxy 一些东西是很值得参考学习的, 为了方便测试推荐添加下​​​add_header X-Proxy-Cache $upstream_cache_status;​​​ 包含一个cache 的请求头,方便分析
问题

说明

多学习官方文档,阅读源码还是比较重要的

参考资料

​​https://nginx.org/en/docs/http/ngx_http_proxy_module.html​​​
​​​https://www.nginx.com/blog/nginx-caching-guide/​​​
​​​https://stackoverflow.com/questions/9230812/nginx-as-cache-proxy-not-caching-anything​​​
​​​https://www.sheshbabu.com/posts/nginx-caching-proxy/​​​
​​​https://docs.nginx.com/nginx/admin-guide/content-cache/content-caching/​​​
​​​https://github.com/nginx/nginx/blob/master/src/http/modules/ngx_http_proxy_module.c​​​
​​​https://github.com/nginx-proxy/nginx-proxy/issues/241​​

举报

相关推荐

0 条评论