[文章作者:张宴 本文版本:v1.2 最后修改:2009.01.12 转载请注明原文链接:http://blog.zyan.cc/nginx_cache/]

  Nginx从0.7.48版本开始,支持了类似Squid的缓存功能。这个缓存是把URL及相关组合当作Key,用md5编码哈希后保存在硬盘上,所以它可以支持任意URL链接,同时也支持404/301/302这样的非200状态码。虽然目前官方的Nginx Web缓存服务只能为指定URL或状态码设置过期时间,不支持类似Squid的PURGE指令,手动清除指定缓存页面,但是,通过一个第三方的Nginx模块,可以清除指定URL的缓存。

  Nginx的Web缓存服务主要由proxy_cache相关指令集和fastcgi_cache相关指令集构成,前者用于反向代理时,对后端内容源服务器进行缓存,后者主要用于对FastCGI的动态程序进行缓存。两者的功能基本上一样。

  最新的Nginx 0.8.32版本,proxy_cache和fastcgi_cache已经比较完善,加上第三方的ngx_cache_purge模块(用于清除指定URL的缓存),已经可以完全取代Squid。我们已经在生产环境使用了 Nginx 的 proxy_cache 缓存功能超过两个月,十分稳定,速度不逊于 Squid。

  在功能上,Nginx已经具备Squid所拥有的Web缓存加速功能、清除指定URL缓存的功能。而在性能上,Nginx对多核CPU的利用,胜过Squid不少。另外,在反向代理、负载均衡、健康检查、后端服务器故障转移、Rewrite重写、易用性上,Nginx也比Squid强大得多。这使得一台Nginx可以同时作为“负载均衡服务器”与“Web缓存服务器”来使用。
  


  1、Nginx 负载均衡与缓存服务器在 Linux 下的编译安装:
ulimit -SHn 65535
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.00.tar.gz
tar zxvf pcre-8.00.tar.gz
cd pcre-8.00/
./configure
make && make install
cd ../

wget http://labs.frickle.com/files/ngx_cache_purge-1.0.tar.gz
tar zxvf ngx_cache_purge-1.0.tar.gz

wget http://nginx.org/download/nginx-0.8.32.tar.gz
tar zxvf nginx-0.8.32.tar.gz
cd nginx-0.8.32/
./configure --user=www --group=www --add-module=../ngx_cache_purge-1.0 --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
cd ../



  2、/usr/local/webserver/nginx/conf/nginx.conf 配置文件内容如下:
user  www www;

worker_processes 8;

error_log  /usr/local/webserver/nginx/logs/nginx_error.log  crit;

pid        /usr/local/webserver/nginx/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;

events
{
  use epoll;
  worker_connections 65535;
}

http
{
  include       mime.types;
  default_type  application/octet-stream;

  charset  utf-8;
      
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 300m;
      
  sendfile on;
  tcp_nopush     on;

  keepalive_timeout 60;

  tcp_nodelay on;

  client_body_buffer_size  512k;
  proxy_connect_timeout    5;
  proxy_read_timeout       60;
  proxy_send_timeout       5;
  proxy_buffer_size        16k;
  proxy_buffers            4 64k;
  proxy_busy_buffers_size 128k;
  proxy_temp_file_write_size 128k;

  gzip on;
  gzip_min_length  1k;
  gzip_buffers     4 16k;
  gzip_http_version 1.1;
  gzip_comp_level 2;
  gzip_types       text/plain application/x-javascript text/css application/xml;
  gzip_vary on;

  #注:proxy_temp_path和proxy_cache_path指定的路径必须在同一分区
  proxy_temp_path   /data0/proxy_temp_dir;
  #设置Web缓存区名称为cache_one,内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。
  proxy_cache_path  /data0/proxy_cache_dir  levels=1:2   keys_zone=cache_one:200m inactive=1d max_size=30g;
  
  upstream backend_server {
    server   192.168.8.43:80 weight=1 max_fails=2 fail_timeout=30s;
    server   192.168.8.44:80 weight=1 max_fails=2 fail_timeout=30s;
    server   192.168.8.45:80 weight=1 max_fails=2 fail_timeout=30s;
  }

  server
  {
    listen       80;
    server_name  www.yourdomain.com 192.168.8.42;
    index index.html index.htm;
    root  /data0/htdocs/www;  

    location /
    {
         #如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。
         proxy_next_upstream http_502 http_504 error timeout invalid_header;
         proxy_cache cache_one;
         #对不同的HTTP状态码设置不同的缓存时间
         proxy_cache_valid  200 304 12h;
         #以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
         proxy_cache_key $host$uri$is_args$args;
         proxy_set_header Host  $host;
         proxy_set_header X-Forwarded-For  $remote_addr;
         proxy_pass http://backend_server;
         expires      1d;
    }
    
    #用于清除缓存,假设一个URL为http://192.168.8.42/test.txt,通过访问http://192.168.8.42/purge/test.txt就可以清除该URL的缓存。
    location ~ /purge(/.*)
    {
     #设置只允许指定的IP或IP段才可以清除URL缓存。
     allow            127.0.0.1;
     allow            192.168.0.0/16;
     deny            all;
     proxy_cache_purge    cache_one   $host$1$is_args$args;
    }    

    #扩展名以.php、.jsp、.cgi结尾的动态应用程序不缓存。
    location ~ .*\.(php|jsp|cgi)?$
    {
         proxy_set_header Host  $host;
         proxy_set_header X-Forwarded-For  $remote_addr;
         proxy_pass http://backend_server;
    }

    access_log  off;
  }
}



  3、启动 Nginx:
/usr/local/webserver/nginx/sbin/nginx




  4、清除指定的URL缓存示例:

  点击在新窗口中浏览此图片





技术大类 » Cache与存储 | 评论(99) | 引用(0) | 阅读(202853)
emrys
2013-3-13 14:58
张老师,问问,我在测试后的时候步骤:1、搭建了两个lnmp服务器(192.168.20.1和192.168.20.2)2、对192.168.20.1的nginx配置文件添加了 proxy_cacheproxy_connect_timeout    5;  proxy_read_timeout       60;  proxy_send_timeout       5;  proxy_buffer_size        16k;  proxy_buffers            4 64k;  proxy_busy_buffers_size 128k;  proxy_temp_file_write_size 128k;  proxy_temp_path   /data0/proxy_temp_dir;  proxy_cache_path  /data0/proxy_cache_dir  levels=1:2   keys_zone=cache_one:200m inactive=1d max_size=30g;    location /        {                proxy_cache cache_one;                proxy_cache_key $host$uri$is_args$args;                proxy_set_header Host $host;                proxy_set_header X-Forwarded-For $remote_addr;                expires 1d;        }3、重启nginx服务,结果是相应的目录里没有产生缓存我的这个操作目的就是在web服务器上对自身的web程序进行缓存,但是不生效第二个测试:把192.168.20.1  只配置了proxy_cache做缓存,192.168.20.2来做web应用,结果访问20.1能够访问,相应的目录也产生了缓存问题是proxy_cache不能对自身的web进行缓存吗,自己测试结果是这样的问题二,如果不能最自身做缓存,那么设置root有什么用呢
merys Email
2013-3-13 15:00
张老师,问问,我在测试后的时候步骤:1、搭建了两个lnmp服务器(192.168.20.1和192.168.20.2)2、对192.168.20.1的nginx配置文件添加了 proxy_cacheproxy_connect_timeout    5;  proxy_read_timeout       60;  proxy_send_timeout       5;  proxy_buffer_size        16k;  proxy_buffers            4 64k;  proxy_busy_buffers_size 128k;  proxy_temp_file_write_size 128k;  proxy_temp_path   /data0/proxy_temp_dir;  proxy_cache_path  /data0/proxy_cache_dir  levels=1:2   keys_zone=cache_one:200m inactive=1d max_size=30g;    location /        {                proxy_cache cache_one;                proxy_cache_key $host$uri$is_args$args;                proxy_set_header Host $host;                proxy_set_header X-Forwarded-For $remote_addr;                expires 1d;        }3、重启nginx服务,结果是相应的目录里没有产生缓存我的这个操作目的就是在web服务器上对自身的web程序进行缓存,但是不生效第二个测试:把192.168.20.1  只配置了proxy_cache做缓存,192.168.20.2来做web应用,结果访问20.1能够访问,相应的目录也产生了缓存问题是proxy_cache不能对自身的web进行缓存吗,自己测试结果是这样的问题二,如果不能最自身做缓存,那么设置root有什么用呢
beihan Email
2013-5-7 09:38
内存缓存空间大小为200MB上面这句话是不是写错了proxy_cache_path不是 指 数据缓存 在 硬盘文件上 的吗?
odayou Email Homepage
2013-5-21 18:15
张,那,单服的话,直接用squid如何.
Skin Care Counsel Email Homepage
2013-6-20 11:38
我發現這個職位非常有用。感謝您分享。 cg4d78j
Skin Care Counsel
sonla
2013-6-21 21:22
在网上看到过不下10次这样的内容,到底谁才是原创。
磨延城 Email Homepage
2013-10-21 20:50
磨途歌学习了
django Email Homepage
2013-11-30 19:46
Uggs Sale the ideal idea Coach Outlet Online  enough time to search for many of reduction Ugg boot. Like big portions? If you began ready your apartment, Coach Outlet  it's always best to consider the most effective way direct to the stage it could be to utilise your inventory throughout Coach Factory Outlet  concern, or maybe you basically won't destination stuff not enough.we're anti- whitening tray because Coach Outlet Online  i will be enjoy just get germ dog breeders
lzh
2014-12-2 14:45
我这里生成哈希文件了,但是感觉还是没有命中缓存,感觉还是从源服务器获取的。
Steven Email Homepage
2015-3-19 02:43
Thank you for this post Weight Loss Tea
steven Email Homepage
2015-3-20 22:31
Thank you asphalt 8 cheats
111
2019-10-18 16:23
grincoolsmilekillkillkillgringrin
Paulojimmathew Email
2021-7-29 12:35
Much obliged. Thanks! , dude. As usual, you come through like a champ! I like your work.  บาคาร่ามือถือ
Ieuan Ventura Email
2021-10-20 18:01
Strategic goals, Team work and Communication is the key with our industry. Following tracks may also lead us to success. Its always been grateful for the opportunity you gave us all. Have a nice day.  สล็อตคาสิโนออนไลน์
Lucky cola
2024-2-7 15:04
Unlock new levels of excitement with every game.  Lucky cola
分页: 5/5 第一页 上页 1 2 3 4 5 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]