nginx proxy_cache 无法生效问题解决

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

参考玩法

 

 

参考资料

  • 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) { 
        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

posted on   荣锋亮  阅读(1060)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 上周热点回顾(2.17-2.23)
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· spring官宣接入deepseek,真的太香了~
历史上的今天:
2021-08-22 基于cloudevents+easy-rules+centraldogma 进行基于规则的业务开发
2021-08-22 mercurius 基于fastify 的graphql server 以及gateway 服务
2021-08-22 如何测试easy-rules 的spel 表达式
2021-08-22 easy-rules-centraldogma-spring-boot-starter spel 模版格式说明
2020-08-22 odyssey prometheus 监控
2020-08-22 odyssey 试用
2019-08-22 singer tap-minio-csv 使用

导航

< 2025年2月 >
26 27 28 29 30 31 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 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示