借鉴文章 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重启即可!