nginx学习十 proxy_cache 代理缓存

  • 语法
    Syntax: proxy_cache_path path [levels=levels]
    	    [use_temp_path=off|on]
    		keys_zone=name:size [inactive=time]
    		[max_size=size] [manager_files=numers]
    		[manager_sleep=time]
    		[manager_threshold=time]
    		[loader_files=number]
    		[loader_sleep=time][loader_threshold=time]
    		[purger=on|off][purger_files=number]
    		[purger_sleep=time]
    		[purger_threshold=time]
    Default:--
    Context:http

     

    ##例:/etc/nginx/conf.d/default.conf
    
    
        upstream test_upstream {
            server localhost:8001;
            server localhost:8002;
            server localhost:8003;
        }
    
        proxy_cache_path /opt/app/cache levels=1:2 keys_zone=test_cache:10m max_size=10g inactive=60m use_temp_path=off;
    
    server {
        listen       80;
        server_name  localhost;
    
        #charset koi8-r;
        access_log  /var/log/nginx/test_proxy.access.log  main;
    
        location / {
            proxy_pass http://test_upstream;
            proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
    
            proxy_cache off;
            proxy_cache_valid 200 304 12h;
            proxy_cache_valid any 10m;
            proxy_cache_key $host$uri$is_args$args;
            add_header Nginx-Cache "$upstream_cache_status";
        }
    ... ...

     

  • 补充:

  1. 如何清理指定缓存?

    1. rm -rf 缓存目录内容
    2. 第三方扩展模块 ngx_cache_purge

  2. 如何让部分页面不缓存

    Syntax: proxy_no_cache string ...;
    Default: --
    Context:http,server,location

     

    
    ##例:/etc/nginx/conf.d/default.conf
        upstream test_upstream {
            server localhost:8001;
            server localhost:8002;
            server localhost:8003;
        }
    
        proxy_cache_path /opt/app/cache levels=1:2 keys_zone=test_cache:10m max_size=10g inactive=60m use_temp_path=off;
    
    server {
        listen       80;
        server_name  localhost;
    
        #charset koi8-r;
        access_log  /var/log/nginx/test_proxy.access.log  main;
    
        if ($request_uri ~ ^/index){  ##匹配以index开头的请求
            set $cookie_nocache 1;
        }
    
        location / {
            proxy_pass http://test_upstream;
            proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
    
            proxy_cache test_cache;
            proxy_cache_valid 200 304 12h;
            proxy_cache_valid any 10m;
            proxy_cache_key $host$uri$is_args$args;
    
            proxy_no_cache $cookie_nocache;
    
            add_header Nginx-Cache "$upstream_cache_status";
        }

     

  3. 大文件分片请求

    Syntax:slice size;
    Default: slice 0;
    Context:http,server,location

     

转载于:https://my.oschina.net/langgege/blog/1931243

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值