Nginx缓存服务器配置

时间:2025-03-11 19:15:01

5.proxy_cache完整示例
(1)首先,把第三方的ngx_cache_purge模块编译安装到Nginx中,用来清除指定URL的缓存
wgetftp:///pub/software/programming/pcre/pcre-8.
tar zxvf pcre-8.
cd pcre-8.00/
./configure
make && make install
cd ../

wget /files/ngx_cache_purge-1.
tar zxvf ngx_cache_purge-1.

wget /download/nginx-0.8.
tar zxvf nginx-0.8.
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)然后,在同一分区下创建两个缓存目录,分别供proxy_temp_path , proxy_cache_path

指令设置缓存路径.注:两个指定设置的缓存路径必须为同一磁盘分区,不能跨分区.
mkdir -p /data0/proxy_temp_path
mkdir -p /data0/proxy_cache_path
(3)Nginx配置文件()对扩展名为gif,jpg,jpeg,png,bmp,swf,js,css的图

片,flash,javascript , css文件开启Web缓存,其他文件不缓存.
示例代码如下:
http{
  proxy_temp_path /data0/proxy_temp_path ;
  #设置Web缓存区名称为cache_one,内存缓存空间大小为500M,自动清除超过1天没有被 

#访问的缓存数据,硬盘缓存空间大小为30G
  proxy_cache_path /data0/proxy_cache_pathlevels=1:2

keys_zone=cache_one:200m inactive=1d max_size=30g ;
     
  server{
      location ~.*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$
      {
          #使用Web缓存区cache_one
          proxy_cache cache_one ;
          #对不同HTTP状态码缓存设置不同的缓存时间
          proxy_cache_valid 200 304 12h ;
          proxy_cache_valid 301 302 1m ;
          proxy_cache_valid any 1m ;
          #设置Web缓存的Key值,Nginx根据Key值md5哈希存储缓存,这里根据"域名,URI,
          #参数"组合成Key
          proxy_cache_key $host$uri$is_args$args;
      }

      #用于清除缓存,假设一个URL为/,通过访问
      #/purge/可以清除该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 ;
      }
  }
}

6.fastcgi_cache相关指令集
(1)fastcgi_cache指令
语法:fastcgi_cache zone_name;
该指令用于设置哪个缓存区将被使用,zone_name的值为fastcgi_cache_path指令创建的

缓存区名称.
(2)fastcgi_cache_path指令
语法:fastcgi_cache_path path [levels=number]keys_zone=zone_name:zone_size

[inactive=time] [max_size=size];
该指令用于设置缓存文件的存放路径,
例:
fastcgi_cache_path /data0/fastcgi_cache_dir levels=1:2

keys_zone=cache_one:500m inactive=1d max_size=30g ;
注意这个指令只能在http标签内配置,
levels指定该缓存空间有两层hash目录,第一层目录为1个字母,第二层为2个字母,保存的

文件名会类似/data0/fastcgi_cache_dir/c/29/XXXX;
keys_zone参数用来为这个缓存区起名,
500m指内存缓存空间大小为500MB;
inactive的1d指如果缓存数据在1天内没有被访问,将被删除;
max_size的30g是指硬盘缓存空间为30GB
(3)fastcgi_cache_methods指令
语法:fastcgi_cache_methods [GET HEAD POST] ;
该指令用于设置缓存哪些HTTP方法,默认缓存HTTP GET/HEAD 方法,不缓存HTTP POST方法

(4)fastcgi_cache_min_uses指令
语法:fastcgi_cache_min_uses the_number;
该指令用于设置缓存的最小使用次数,默认值为1.
(5)fastcgi_cache_valid指令
fastcgi_cache_valid reply_code [reply_code...] time;
该‎指令用于对不同返回状态码的URL设置不同的缓存时间.
fastcgi_cache_valid 200 302 10m ;
fastcgi_cache_valid 404 1m ;
设置200,302状态的URL缓存10分钟,404状态的URL缓存1分钟.
如果不指定状态码,直接指定缓存时间,则只有200,301,302状态的URL缓存5分钟.
(6)fastcgi_cache_key指令
语法:fastcgi_cache_key line ;
该指令用来设置Web缓存的Key值,Nginx根据Key值md5哈希存储缓存.一般根据FastCGI服

务器的地址和端口,$request_uri(请求的路径)等变量组合成fastcgi_cache_key。


7.fastcgi_cache完整示例
(1)首先,在同一分区下创建两个缓存目录,分别供

fastcgi_temp_path,fastcgi_cache_path指令设置缓存路径.注:两个指定设置的缓存路

径必须为同一磁盘分区,不能跨分区.
mkdir -p /data0/fastcgi_temp_path
mkdir -p /data0/fastcgi_cache_path
(2)Nginx配置文件()对扩展名为gif,jpg,jpeg,png,bmp,swf,js,css的图

片,Flash,JavaScript,CSS文件开启Web缓存,其他文件不缓存.
http{
  #fastcgi_temp_path和fastcgi_cache_path指定的路径必须在同一分区
  fastcgi_temp_path /data0/fastcgi_temp_path;
  #设置Web缓存区名称为cache_one,内存缓存空间大小为500MB,自动清除超过1天没有被

  #访问的缓存数据,硬盘缓存空间大小为30G
  fastcgi_cache_path /data0/fastcgi_cache_pathlevels=1:2

keys_zone=cache_one:200m inactive=1d max_size=30g ;

  server{
      location ~.*\.(php|php5)$
      {
          #使用Web缓存区cache_one
          fastcgi_cache cache_one ;
          #对不同的HTTP状态码缓存设置不同的缓存时间
          fastcgi_cache_valid 200 10m ;
          fastcgi_cache_valid 301 302 1h ;
          fastcgi_cache_valid an 1m ;
          #设置Web缓存的key值,Nginx根据key值md5哈希存储缓存,这里根据"FastCGI服务 

      #器的IP,端口,请求的URI"组合成Key。
          fastcgi_cache_key 127.0.0.1:9000$requet_uri ;
          #FastCGI服务器
          fastcgi_pass 127.0.0.1:9000 ;
          fastcgi_index ;
          include ;
      }
  }
}