Nginx生效接口缓存

借鉴文章 Nginx POST 请求缓存的使用 - 简书 (jianshu.com) Nginx配置Proxy Cache - 掘金

demo开始:

一、http配置添加

在http配置中添加行

proxy_cache_path /usr/share/nginx/cache levels=1:2 keys_zone=cache_app:512m inactive=5m max_size=2g;

其中/usr/share/nginx/cache代表缓存路径,缓存生效后可在对应子路径下,查看到缓存数据;keys_zone 代表缓存命名空间;

二、server配置添加

2.1 GET请求

   location /customers/list {
        include /etc/nginx/conf.d/proxy.temple;
        proxy_cache cache_app;
        proxy_cache_key $uri$is_args$args;
        proxy_cache_valid 200 1m;
        proxy_ignore_headers Cache-Control Expires;
        proxy_pass http://xxx:8080/customers/list;
    }

其中proxy_ignore_headers 主要规避接口返回的Cache-control设置;

2.2 POST请求

    location /report/wechatFansReport {
        include /etc/nginx/conf.d/proxy.temple;
        proxy_cache cache_app;
        proxy_cache_methods POST;
        proxy_cache_use_stale updating;
        proxy_cache_key "$request_uri|$request_body";
        proxy_ignore_headers Cache-Control Expires;
        proxy_cache_valid 200 2m;
        proxy_pass http://xxx:8080/report/wechatFansReport;
    }

其中proxy_ignore_headers 主要规避接口返回的Cache-control设置;proxy_cache_methods  声明使用的请求方式为POST;

3.生效配置

nginx重启即可!

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值