最近配置另外一套刚做的系统时遇到了些问题?采用nginx_cache缓存,使用ngx_cache_purge来清除缓存。由于之前的cms系统url全部是小写字母或数字,所以就没有这样的问题,现在的问题是url包含大小写字母,不管用户输入的是大小写字母都可以访问到指定内容,由于ngx_cache缓存是$host$uri$is_args$args做key进行md5后生成的缓存文件名,这样一来大写小写会各缓存一次,而清除缓存的时候是根据读取页面中的链接来清除缓存的,就会遇到某些缓存是清除不掉的。下面举几个例子。比如:
访问地址:http://www.dyx.com/go/XianXia和http://www.dyx.com/go/xianxia访问结果是一样的
清除缓存:http://www.dyx.com/purge/go/XianXia而http://www.dyx.com/purge/go/xianxia则清除不掉
我采用的方法是使用nginx的lua模块(参考:FreeBSD下nginx添加lua-nginx-module模块,使nginx支持lua强大的语法),用lua来把请求的url都给转换成小写,这样缓存下来的只有小写的,清除缓存也是一样,统一了大小写后,问题就解决了
nginx配置如下:
- location /
- {
- content_by_lua 'local res = ngx.location.capture("/lua"..string.lower(ngx.var.uri))
- if res.status == 200 then
- ngx.print(res.body)
- end';
- }
- location /lua
- {
- rewrite ^/lua/$ /index.php?mod=index break;
- rewrite ^/lua/ssi_([a-zA-Z]+)_(\d+)_(latest|libao|yygs|all)$ /index.php?mod=ssi&action=$1&gameid=$2&by=$3? break;
- rewrite ^/lua/(index|test)\.html$ /index.php?mod=$1 break;
- rewrite ^/lua/game/([a-zA-Z\d]+)/?$ /index.php?mod=game&action=game&english=$1? break;
- rewrite ^/lua/game/([a-zA-Z\d]+)/([a-zA-Z]+)-list(?:_(?!0)(\d+))?\.html$ /index.php?mod=game&action=list&english=$1&type=$2&pageno=$3? break;
- rewrite ^/lua/game/([a-zA-Z\d]+)/([a-zA-Z]+)-(\d+)(?:_(?!0\d|1\.)(\d+))?\.html$ /index.php?mod=game&action=content&english=$1&type=$2&id=$3&pageno=$4? break;
- rewrite ^/lua/news/(\d+)(?:_(?!0\d|1\.)(\d+))?\.html$ /index.php?mod=public&action=news&id=$1&pageno=$2? break;
- proxy_set_header DIANHOST www.dyx.com;
- proxy_pass http://admin.dyx.com;
- proxy_cache cache_tmp;
- proxy_cache_valid 200 304 12h;
- proxy_cache_valid 301 302 1m;
- proxy_cache_key $host$uri$is_args$args;
- }
- #清除缓存
- location ~* ^/purge/
- {
- if ($uri ~ ^/purge/$)
- {
- set $cachekey /index.php?mod=index;
- }
- if ($uri ~ ^/purge/(index|test).html$)
- {
- set $cachekey /index.php?mod=$1;
- }
- if ($uri ~ ^/purge/game/([a-zA-Z\d]+)/?$)
- {
- set $cachekey /index.php?mod=game&action=game&english=$1;
- }
- if ($uri ~ ^/purge/game/([a-zA-Z\d]+)/([a-zA-Z]+)-list(?:_(?!0)(\d+))?\.html$)
- {
- set $cachekey /index.php?mod=game&action=list&english=$1&type=$2&pageno=$3;
- }
- if ($uri ~ ^/purge/game/([a-zA-Z\d]+)/([a-zA-Z]+)-(\d+)(?:_(?!0\d|1\.)(\d+))?\.html$)
- {
- set $cachekey /index.php?mod=game&action=content&english=$1&type=$2&id=$3&pageno=$4;
- }
- if ($uri ~ ^/purge/news/(\d+)(?:_(?!0\d|1\.)(\d+))?\.html$)
- {
- set $cachekey /index.php?mod=public&action=news&id=$1&pageno=$2;
- }
- if ($uri ~ ^/purge/ssi_([a-zA-Z]+)_(\d+)_(latest|libao|yygs|all)$)
- {
- set $cachekey /index.php?mod=ssi&action=$1&gameid=$2&by=$3;
- }
- if ($cachekey = "")
- {
- return 405;
- }
- set_by_lua $cachekey 'return string.lower(ngx.var.cachekey)';
- proxy_cache_purge cache_tmp $host$cachekey;
- }
如非注明则为本站原创文章,欢迎转载。转载请注明转载自:moon's blog