让nginx的proxy_store和proxy_cache支持ctrl+f5和PURGE二种方

主要是对我的个人博客的PHP执行代码进行Cache,对于Js,CSS直接在客户端缓存即可,这儿重点是对PHP的CGI执行结果在服务器端进行缓存,以减少服务器的DB查询压力,这样DB从21次/秒降低到13次/秒。目前让nginx的proxy_store和proxy_cache支持ctrl+f5和PURGE结合删除缓存的方法二种:
一.让ngx_cache_purge来帮忙,通过Nginx对ctrl+f5的标志来进行重写清除日志。
二.用PHP来实现清除后并再次跳转到对应的Uri模块,以实现页面缓存更新后的显示。
三.修改ngx_cache_purge源代码。。。。:(暂时忽略。
-----------------
方法一:

步骤1:需要编译安装ngx_cache_purge这个模块让它来对缓存进行清理。
  1. ./configure --user=www --group=www --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module   --add-module=/root/software/nginx_http_push_module-0.692   --add-module=/root/software/ngx_cache_purge-1.3  


步骤2:
nginx.conf中加入对Ctrl+F5刷新的标志判断并UrlRewrite:
  1.  #limit_zone  crawler  $binary_remote_addr  10m;  
  2. fastcgi_cache_path /data0/proxy_temp_dir  levels=1:2  keys_zone=cache_php:30m inactive=1d max_size=1g;  
  3. fastcgi_temp_path /data0/ngx_fcgi_tmp;  
  4. location ~ /purge(/.*)  
  5. {  
  6.   fastcgi_cache_purge cache_php $host$1$is_args$args;  
  7. }  
  8. if ( $request_method = "PURGE" ) {  
  9. rewrite ^(.*)$ /purge$1 last;  
  10. }  
  11.   
  12. #limit_conn   crawler  20;      
  13. location ~ .*\.(php|php5)?$  
  14. {        
  15.   #fastcgi_pass  unix:/tmp/php-cgi.sock;  
  16.   fastcgi_pass  127.0.0.1:9000;  
  17.   fastcgi_index index.php;  
  18.   include fcgi.conf;  
  19.   #以下是fastcgi_cache的配置  
  20.   fastcgi_cache   cache_php;  
  21.   fastcgi_cache_valid   200 302  1h;  
  22.   fastcgi_cache_min_uses  1;  
  23.   fastcgi_cache_use_stale error  timeout invalid_header http_500;  
  24.   #fastcgi_cache_key http://$host$request_uri;  
  25.   fastcgi_cache_key $host$uri$is_args$args;  
  26.   if ($http_Cache_Control ~ "no-cache")   
  27.   {  
  28.     set $http_Cache_Control 'max-age=604800';  
  29.     rewrite ^(.*)$ /purge$1 last;  
  30.   }  
  31. }  


对已经缓存的页面用Ctrl+F5后出现:
Successful purge

Key : justwinit.cn/read.php?entryid=4454&page=&part=
Path: /data0/proxy_temp_dir/c/b8/ea9939947c0cf37b9cae885987876b8c
nginx/1.0.4

清理成功!!
-----------------
方法二:
步骤1:
用PHP来实现,其Nginx配置代码修改为:
  1.  #limit_zone  crawler  $binary_remote_addr  10m;  
  2. fastcgi_cache_path /data0/proxy_temp_dir  levels=1:2  keys_zone=cache_php:30m inactive=1d max_size=1g;  
  3. fastcgi_temp_path /data0/ngx_fcgi_tmp;  
  4. location ~ /purge(/.*)  
  5. {  
  6.   fastcgi_pass   127.0.0.1:9000;  
  7.   include        fastcgi_params;  
  8.   fastcgi_param  SCRIPT_FILENAME    /data0/htdocs/blog/purge.php;  
  9.   error_page 405 =200 /purge$1;  
  10. }  
  11. if ( $request_method = "PURGE" ) {  
  12. rewrite ^(.*)$ /purge$1 last;  
  13. }  
  14. #limit_conn   crawler  20;      
  15. location ~ .*\.(php|php5)?$  
  16. {        
  17.   #fastcgi_pass  unix:/tmp/php-cgi.sock;  
  18.   fastcgi_pass  127.0.0.1:9000;  
  19.   fastcgi_index index.php;  
  20.   include fcgi.conf;  
  21.   #以下是fastcgi_cache的配置  
  22.   fastcgi_cache   cache_php;  
  23.   fastcgi_cache_valid   200 302  1h;  
  24.   fastcgi_cache_min_uses  1;  
  25.   fastcgi_cache_use_stale error  timeout invalid_header http_500;  
  26.   #fastcgi_cache_key http://$host$request_uri;  
  27.   fastcgi_cache_key $host$uri$is_args$args;  
  28.   if ($http_Cache_Control ~ "no-cache")   
  29.   {  
  30.     set $http_Cache_Control 'max-age=604800';  
  31.     rewrite ^(.*)$ /purge$1 last;  
  32.   }  
  33. }  

步骤2:
通过UrlRewrite后的结果到purge.php后进行Md5目录规则的删除,并再次刷新该页面,
purge.php代码如下:
  1. <?php  
  2. @header("Content-Type: text/html; charset=utf-8");  
  3. @header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");  
  4. @header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");  
  5. @header("Cache-Control: no-store, no-cache, must-revalidate");  
  6. @header("Pragma: no-cache");  
  7. $host = isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '');  
  8. //uri  
  9. function request_uri()  
  10. {  
  11.   if (isset($_SERVER['REQUEST_URI']))  
  12.   {  
  13.     $uri = $_SERVER['REQUEST_URI'];  
  14.   }  
  15.   else  
  16.   {  
  17.     if (isset($_SERVER['argv']))  
  18.     {  
  19.       $uri = $_SERVER['PHP_SELF'] .'?'. $_SERVER['argv'][0];  
  20.     }  
  21.     else  
  22.     {  
  23.       $uri = $_SERVER['PHP_SELF'] .'?'. $_SERVER['QUERY_STRING'];  
  24.     }  
  25.   }  
  26.   return $uri;  
  27. }  
  28. $uri = request_uri();  
  29.   
  30. $url = "http://".$host.$uri;  
  31. $md5md5 = md5($url);  
  32. $cacheRoot = "/data0/proxy_temp_dir";  
  33. $cacheFile = $cacheRoot . '/' . substr($md5, -1, 1) . '/' . substr($md5, -3, 2) . '/' . $md5;  
  34. if(is_file($cacheFile))  
  35. {  
  36.   @unlink($cacheFile);  
  37. }  
  38. echo '<script language="javascript" type="text/javascript">';  
  39. echo "window.location.href='{$url}'";  
  40. echo '</script>';  
  41. ?>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值