Nginx有很强的代理功能,作为proxy很有优势,另外还可以把文件缓存到本地,实在是很不错的。
但可惜的是,默认没有文件过期控制,文件作为长期镜像是很不错的。

Nginx代理不需要下载到服务器端再发送,有进程下载完整内容了估计就可以保存到本地了,需要清理的话,下面有介绍简单的方法。Nginx的文件发送性能就不用担心了,很稳定高效。

详情参考:NginxHttpProxyModule

引用
proxy_store
syntax: proxy_store [on | off | path]

default: proxy_store off

context: http, server, location

This directive sets the path in which upstream files are stored. The parameter "on" preserves files in accordance with path specified in directives alias or root. The parameter "off" forbids storing. Furthermore, the name of the path can be clearly assigned with the aid of the line with the variables:

proxy_store   /data/www$original_uri;

The time of modification for the file will be set to the date of "Last-Modified" header in the response. To be able to safe files in this directory it is necessary that the path is under the directory with temporary files, given by directive proxy_temp_path for the data location.


也就是说的是可以缓存文件了,怎么做呢?
可以参考下面的代码片段:

upstream aslibra {
  server 123.45.67.89 ;
  server 123.45.67.8 backup;
}

proxy_store             on;
proxy_store_access      user:rw  group:rw  all:r;
proxy_temp_path /Data/webapps/proxy_tmp;
proxy_redirect off;

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

server {
  expires       240h;
  server_name  www.aslibra.com;
  root    /Data/webapps/www.aslibra.com;
  location / {
    add_header      X-Cache   HIT;
    error_page           404 = @fetch;
  }

  location @fetch {
    internal;
    proxy_pass                      http://aslibra;
    proxy_next_upstream http_500;
    add_header      X-Cache   MISS;
    root                    /Data/webapps/www.aslibra.com;
  }
}


另外,文件要更新怎么办?那其实有两种方式解决文件过期的情况:
1 定时删除修改时间太旧的文件
2 PURGE方式直接删除

定时删除文件
下面是每2小时删除修改时间超过2天的文件

#clear cache pic
01 */2 * * * root find /Data/webapps/www.aslibra.com/ -type f -mtime +2 -print0 | xargs -0 -r rm >/dev/null 2>&1


PURGE方式删除

如果不是要求实时删除的话,可以有一种思路:
PURGE请求的(或者其它标识)记录日志,然后每10分钟分析日志,删除日志里面的文件,这样效率应该也是不错的~我没有做这个,我还是希望兼容PURGE,那怎么办呢?

那就交给fastcgi处理好了(注意,Nginx不支持cgi),比如:

location / {
  if ($request_method ~ "PURGE"){
    rewrite (.*) /purge.php last;
  }
  add_header      X-Cache   HIT;
  error_page           404 = @fetch;
}
location /purge.php
{
  internal;
  fastcgi_pass   127.0.0.1:9000;
  include        fastcgi_params;
  fastcgi_param  SCRIPT_FILENAME    /Data/www.aslibra.com/fastcgi$fastcgi_script_name;
}


这个就是把请求交给PHP了啦,PHP怎么写呢?
下面有个简单的例子:

<?
header('Cache-Control: max-age=0');
$uri = $_SERVER['REQUEST_URI'];
$doc_root = $_SERVER['DOCUMENT_ROOT'];

$file = $doc_root.$uri;
echo $file."\n";
if(is_file($file)){
  echo "File Clear...";
  @unlink($file);
}else{
  echo "File Not Found...";
}
?>


以上方式就兼容squid的PURGE方式了,很方便的~

另外说一下Ncache:

Ncache也是不错的项目,是新浪的公司项目。
测试过32位的版本,有不满意的地方:
1 文件缓存有不完整的情况
2 proxy_pass就触发此功能,不能使用proxy_store了,以上说的功能不兼容
3 发现多次 kill -HUP 导致死掉服务

与开发的负责人闲聊一些,听闻64位现在是主要维护对象,文件不完整情况应该做了改善,有兴趣的朋友可以试试
不过阿权没有64位的机器来测试了,我的都是32位的老机器啊,可怜~~


原创内容如转载请注明:来自 阿权的书房
收藏本文到网摘
Tags: , , ,
&FROST
2009/12/16 22:39
QQ:273585099
你用的 什么版本的 nginx  还是 用了 第三方。。。
我这无法识别 PURGE 指令。。
&FROST
2009/12/16 10:57
请问为啥我telnet 到我的nginx Server
怎么就不能发送  PURGE 指令呢。。
我的nginx 不支持 request_method  PURGE 还是配置错了?
hqlulu 回复于 2009/12/16 13:10
这个你碰到什么反馈信息,可以把配置发gmail我看看,hqlulu at gmail
无风的飘逸 Homepage
2009/12/05 12:16
无意间来到贵博客,希望交个朋友

我的QQ:294229820 有兴趣可以一起交流
nginx+linux+php 专业博客
清水的博客http://www.zouqingshui.net
pr3寻求友情链接。
hqlulu 回复于 2009/12/10 08:56
恩。。
分页: 1/1 第一页 1 最后页
发表评论
表情
emotemotemotemotemotemotemotemotemotemotemotemotemot
emotemotemotemotemotemotemotemotemotemotemotemot
打开HTML 打开UBB 打开表情 隐藏
昵称   密码   游客无需密码
网址   电邮   [注册]
               

验证码 不区分大小写
 

阅读推荐

服务器相关推荐

开发相关推荐

应用软件推荐