admin avatar

nginx 反向代理后端并缓存数据的方法

🕞 by admin

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
proxy_cache_path  /data/cache/nginx levels=1:2 keys_zone=cache1:100m inactive=1d max_size=2g;//最大占用2G空间

  location ^~ / {
        //代理所有的域名根目录下的请求,包括静态资源
    	proxy_pass http://127.0.0.1:3100/;

    	proxy_cache_key $host$uri$is_args$args;
    	proxy_redirect off;
    	proxy_set_header Host $host;
    	proxy_cache cache1;

    	#状态为200、302的缓存1天
    	proxy_cache_valid 200 302 1d;//200 302状态码缓存1天
    	#状态为301的缓存2天
    	proxy_cache_valid 301 2d;//301状态码缓存2天
    	proxy_cache_valid any 1m;//1分钟
    	#浏览器过期时间设置8小时
    	expires 8h;
    	#忽略头部禁止缓存申明,类似与CDN的强制缓存功能
    	proxy_ignore_headers "Cache-Control" "Expires" "Set-Cookie";
    	#在header中插入缓存状态,命中缓存为HIT,没命中则为MISS
    	add_header Nginx-Cache "$upstream_cache_status";
    }

💘 相关文章

写一条评论