主要是对我的个人博客的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这个模块让它来对缓存进行清理。
./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:
#limit_zone crawler $binary_remote_addr 10m; fastcgi_cache_path /data0/proxy_temp_dir levels=1:2 keys_zone=cache_php:30m inactive=1d max_size=1g; fastcgi_temp_path /data0/ngx_fcgi_tmp; location ~ /purge(/.*) { fastcgi_cache_purge cache_php $host$1$is_args$args; } if ( $request_method = "PURGE" ) { rewrite ^(.*)$ /purge$1 last; } #limit_conn crawler 20; location ~ .*.(php|php5)?$ { #fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fcgi.conf; #以下是fastcgi_cache的配置 fastcgi_cache cache_php; fastcgi_cache_valid 200 302 1h; fastcgi_cache_min_uses 1; fastcgi_cache_use_stale error timeout invalid_header http_500; #fastcgi_cache_key http://$host$request_uri; fastcgi_cache_key $host$uri$is_args$args; if ($http_Cache_Control ~ "no-cache") { set $http_Cache_Control 'max-age=604800'; rewrite ^(.*)$ /purge$1 last; } }
对已经缓存的页面用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配置代码修改为:
#limit_zone crawler $binary_remote_addr 10m; fastcgi_cache_path /data0/proxy_temp_dir levels=1:2 keys_zone=cache_php:30m inactive=1d max_size=1g; fastcgi_temp_path /data0/ngx_fcgi_tmp; location ~ /purge(/.*) { fastcgi_pass 127.0.0.1:9000; include fastcgi_params; fastcgi_param SCRIPT_FILENAME /data0/htdocs/blog/purge.php; error_page 405 =200 /purge$1; } if ( $request_method = "PURGE" ) { rewrite ^(.*)$ /purge$1 last; } #limit_conn crawler 20; location ~ .*.(php|php5)?$ { #fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fcgi.conf; #以下是fastcgi_cache的配置 fastcgi_cache cache_php; fastcgi_cache_valid 200 302 1h; fastcgi_cache_min_uses 1; fastcgi_cache_use_stale error timeout invalid_header http_500; #fastcgi_cache_key http://$host$request_uri; fastcgi_cache_key $host$uri$is_args$args; if ($http_Cache_Control ~ "no-cache") { set $http_Cache_Control 'max-age=604800'; rewrite ^(.*)$ /purge$1 last; } }
步骤2:
通过UrlRewrite后的结果到purge.php后进行Md5目录规则的删除,并再次刷新该页面,
purge.php代码如下:
<?php @header("Content-Type: text/html; charset=utf-8"); @header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); @header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); @header("Cache-Control: no-store, no-cache, must-revalidate"); @header("Pragma: no-cache"); $host = isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : ''); //uri function request_uri() { if (isset($_SERVER['REQUEST_URI'])) { $uri = $_SERVER['REQUEST_URI']; } else { if (isset($_SERVER['argv'])) { $uri = $_SERVER['PHP_SELF'] .'?'. $_SERVER['argv'][0]; } else { $uri = $_SERVER['PHP_SELF'] .'?'. $_SERVER['QUERY_STRING']; } } return $uri; } $uri = request_uri(); $url = "http://".$host.$uri; $md5 = md5($url); $cacheRoot = "/data0/proxy_temp_dir"; $cacheFile = $cacheRoot . '/' . substr($md5, -1, 1) . '/' . substr($md5, -3, 2) . '/' . $md5; if(is_file($cacheFile)) { @unlink($cacheFile); } echo '<script language="javascript" type="text/javascript">'; echo "window.location.href='{$url}'"; echo '</script>'; ?>
转载请注明:爱开源 » 让nginx的proxy_store和proxy_cache支持ctrl+f5和PURGE二种方法