`
jsczxy2
  • 浏览: 1251720 次
  • 性别: Icon_minigender_1
  • 来自: 常州
文章分类
社区版块
存档分类
最新评论

nginx缓存动态页面功能的设置

阅读更多

网上查了一下相关资料,主要是在需要缓存的路径加proxy_cache,而后端动态页面也需要做相应的处理,否则无法被nginx缓存。(或者nginx配置处加上proxy_ignore_headers)

 

检测到是代理服务器访问的时候,给动态页面加上和静态页面一致的头部信息,例如:

  
  1. ETag:"1febdd4429cfcd1:0"
  2. Last-Modified:Fri,30Nov201218:34:01 GMT
 

让代理服务器感觉不到是动态的内容。
这种方式最好缓存的是没有访问权限控制的动态页面。

 

下面记录几篇文章:

 1.

http://my.oschina.net/foxidea/blog/97511

 

------------------------------------------分割线-----------------------------------

2.

首先用的缓存是proxy_cache.

在http段里加入下列几句:

 

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. proxy_connect_timeout 5;  
  2.   proxy_read_timeout 60;  
  3.   proxy_send_timeout 5;  
  4.   proxy_buffer_size 16k;  
  5.   proxy_buffers 4 64k;  
  6.   proxy_busy_buffers_size 128k;  
  7.   proxy_temp_file_write_size 128k;  
  8.   proxy_temp_path /home/temp_dir;  
  9.   proxy_cache_path /home/cache levels=1:2 keys_zone=cache_one:50m inactive=20m max_size=30g;  


上面的各行的参数我再时不解释,网上有很多。

 

 

接下来在要缓存的service里加入:

 

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. location /gou/detail-id-116  {  
  2.            ##缓存  
  3.           index  index.html index.htm index.php;  
  4.           proxy_cache cache_one;  
  5.           proxy_cache_valid 200 302 1h;  
  6.             
  7.           proxy_cache_key $host$uri$is_args$args;  
  8.           proxy_pass   http://contactpool;  
  9.           proxy_ignore_headers "Cache-Control" "Expires" "Set-Cookie";#不处理后端服务器返回的指定响应头  
  10.           expires 30d;  
  11.           proxy_set_header Host $host;  
  12.           proxy_set_header X-Real-IP $remote_addr;  
  13.           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
  14.        }  

 

当然可以用正则缓存更多的页面如:~.*\.(php|jsp|cgi)?$

上面的我只解释一下地方:

 

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. proxy_ignore_headers  

这个表示不处理后端服务器返回的指定响应头,作用就是能够缓存动态页面,比如.php的页面,如果不加这一行就只能缓存静态的页面内容了。

 

现在:nginx -s reload 后缓存就有了,

接下来如何在需要的时候清理缓存呢,

网上一位大牛分析了nginx是如何存的缓存文件:

计算方法:
1) nginx先把请求地址/1.png用md5进行哈希,得到e0bd86606797639426a92306b1b98ad9
md5的参数就是上面的配置中:

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. proxy_cache_key  

值,如md5("www.xxx.com/gou/detail-id-116");

2) level=1:2就是把最后一位数9拿出来建一个目录,然后再把9前面的2位建一个目录,最后把刚才得到的这个缓存文件放到9/ad目录中。
同样的方法推理,如果level=1:1,那么缓存文件的路径就是/usr/local/nginx/cache/9/d/e0bd86606797639426a92306b1b98ad9

 

那么我们就可以写一个脚本来清理特定的缓存了:

 

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. #!/usr/bin/env php  
  2. <?php  
  3.   
  4. $cache_dir = '/usr/local/nginx/cache/';  
  5. $request_uri = $argv[1];  
  6. $url_hash = md5($request_uri);  
  7. $dir1 = substr($url_hash,-1,1) . '/';  
  8. $dir2 = substr($url_hash,-3,2) . '/';  
  9. $cached_file = $cache_dir . $dir1 . $dir2 . $url_hash;  
  10. if (is_file($cached_file)) {  
  11.     if (unlink($cache_dir . $dir1 . $dir2 . $url_hash)) {  
  12.         echo $request_uri . " 缓存清除成功\n";  
  13.     } else {  
  14.         echo $request_uri . " 缓存清除失败\n";  
  15.     }  
  16. } else {  
  17.     echo $request_uri . " 未被缓存\n";  
  18. }  

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics