【问题标题】:how to purge nginx cache which vary by user agent如何清除因用户代理而异的 nginx 缓存
【发布时间】:2013-05-07 15:58:36
【问题描述】:

一个页面有两个缓存。一个用于 PC 网络浏览器,另一个用于移动浏览器。

nginx 可以完美地做到这一点。以下是nginx conf内容的一部分:

map $http_user_agent $device_type {
    default 'pc';
    ~(iPhone|Android|IEMobile|Balckberry) 'mobile';
}

#fast cgi cache def
fastcgi_cache_path  /data0/nginx-1.2.6/cache levels=1:2 keys_zone=nginx_webpy_cache:30m inactive=1d;

fastcgi_temp_path /data0/nginx-1.2.6/cache/temp;
fastcgi_cache_key "$request_method$scheme$host$request_uri$device_type$is_args$args";
#end

如您所见,我将 $device_type 放入缓存键中以用于两个缓存版本。

但是我发现有了这个配置,我无法清除 nginx 缓存。如何清除这些缓存版本?

非常感谢。

【问题讨论】:

    标签: caching nginx fastcgi


    【解决方案1】:

    我已经解决了这个问题。

    遵循是我的解决方案。

    首先在 nginx.conf 的 http 作用域中添加 map:

    map $http_user_agent $device_type {
        default 'pc';
        ~(iPhone|Android|IEMobile|Balckberry) 'mobile';
    }
    

    你需要编辑 fastcgi 缓存键,如下:

      #fast cgi cache def
      fastcgi_cache_path  /data0/nginx-1.2.6/cache levels=1:2 keys_zone=nginx_webpy_cache:30m inactive=1d;
    
      fastcgi_temp_path /data0/nginx-1.2.6/cache/temp;
    
      fastcgi_cache_key "$scheme$request_method$host/$device_type$request_uri$is_args$args";
      #end
    

    我将变量 $device_type 放入缓存键中。现在 pc 和 mobile 有不同的缓存版本。

    注意:如果你需要清除缓存,你应该清除拖曳版本。

    如果你是中国人,请看下面的文章:

    nginx cache with different user agent

    【讨论】:

      猜你喜欢
      • 2017-02-26
      • 2011-09-08
      • 1970-01-01
      • 2015-11-12
      • 1970-01-01
      • 1970-01-01
      • 2014-11-25
      • 2023-03-22
      • 2014-10-23
      相关资源
      最近更新 更多