nginx的cache服务器支持ctrl+f5刷新的配置

转载自:linuxtone
原文地址: http://bbs.linuxtone.org/forum.php?mod=viewthread&tid=7315
nginx的cache功能可以把URL及相关组合当作Key,用md5编码哈希后保存在硬盘上,配置灵活,功能好不亚于squid。
但是在更新机制上面不如squid那么灵活,本文是通过ctrl+f5的方式来实现强制刷新nginx的cache。

原理:
[上海]暴 : Ctrl+F5无非就是请求头里面加了个 Cache-Control:no-cache 如果nginx能识别并进行相应的操作 就能解决了把
阿海 : 其实只是判断nginx的头部信息
  1. $http_Cache_Control = no-cache
复制代码


配置:
在你的nginx的cache配置里面加入
  1. if ($http_Cache_Control = "no-cache") {
  2.       rewrite ^(.*)$ /purge$1 last;
  3. }
复制代码

当然ctrl+f5的刷新用户也得你自己去控制,主要是在purge那段配置:
规定ip刷新:
  1. location ~ /purge(/.*)
  2. {
  3. #设置只允许指定的IP或IP段才可以清除URL缓存。
  4. allow 114.244.0.0/16;
  5. deny all;
  6. proxy_cache_purge cache_one $host$1$is_args$args;
  7. }
复制代码

全部用户都能刷新:
  1. location ~ /purge(/.*)
  2. {
  3. proxy_cache_purge cache_one $host$1$is_args$args;
  4. }
复制代码


处理示例:
强刷前:
1.jpg 
刷新成功界面:
2.jpg 
刷新后打开:
3.jpg 

我的详细配置:
  1. server
  2.       {
  3.       listen 80;
  4.       server_name 54yancheng.com www.54yancheng.com;
  5.       
  6.       #purge cache files
  7.       location ~ /purge(/.*)
  8.       {
  9.             #设置只允许指定的IP或IP段才可以清除URL缓存。
  10.             allow 114.244.0.0/16;
  11.             deny all;
  12.             proxy_cache_purge cache_one $host$1$is_args$args;
  13.             error_page 405 =200 /purge$1;
  14.       }
  15.       
  16.       if ( $request_method = "PURGE" ) {
  17.             rewrite ^(.*)$ /purge$1 last;
  18.       }
  19.       
  20.       #pass files
  21.       location /
  22.       {
  23.             proxy_set_header Host $host;
  24.             proxy_set_header X-Forwarded-For $remote_addr;
  25.             proxy_pass http://54yancheng;
  26.       }
  27.       
  28.       #cache files
  29.       location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$
  30.       {
  31.    
  32.             if ($http_Cache_Control = "no-cache") {
  33.                   rewrite ^(.*)$ /purge$1 last;
  34.             }
  35.       
  36.             add_header X-Cache HIT_from_www.54yancheng.com;
  37.       
  38.             proxy_cache cache_one;
  39.       
  40.             proxy_cache_valid 200 304 12h;
  41.             proxy_cache_valid 301 302 1m;
  42.             proxy_cache_valid any 1m;
  43.       
  44.             proxy_cache_key $host$uri$is_args$args;
  45.             proxy_set_header Host $host;
  46.             proxy_set_header X-Forwarded-For $remote_addr;
  47.             proxy_pass http://54yancheng;
  48.       
  49.             expires 1y;
  50.             access_log off;
  51.       
  52.       }
  53.       
  54.       #web log
  55.       #access_log off;
  56.       #access_log /data/logs/access_www.54yancheng.com.log combined;
  57.       
  58. }
复制代码


后续(只是提供方法,没在实际中实验,有错别拍砖):

中间的成功提示界面,对用户体验影响不好,询问了朋友
i am nothing :建议修改cache模块的源代码

查找ngx_cache_purge-1.1/ngx_cache_purge_module.c文件得到如下:
  1. static char ngx_http_cache_purge_success_page_top[] =
  2. "" CRLF
  3. "" CRLF
  4. "" CRLF
  5. "

    Successful purge

    " CRLF
  6. ;

  7. static char ngx_http_cache_purge_success_page_tail[] =
  8. CRLF "" CRLF
  9. " " NGINX_VER " " CRLF
  10. "" CRLF
  11. "" CRLF
  12. ;
复制代码


解决方法:
注释掉或者删除皆可!

你可能感兴趣的:(nginx,cache,服务器)