`
student_lp
  • 浏览: 428707 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论
阅读更多

    不管是前面介绍的Apache缓存还是现在要介绍的Nginx缓存,其实他们都借鉴了squid。随着web服务器的发展,web服务器的缓存功能已经越来越强大,通过配置使用web服务器的缓存功能,我们在很多时候已经没有必要去实用squid了。下面是Nginx的proxy_cache模块的介绍。

二、Nginx缓存

1、依赖组件

    从Nginx-0.7.44版本开始,Nginx开始支持类似Squid的较正规的缓存功能,这个缓存是把链接用md5编码经过哈希后保存,支持任意链接。同时支持404/301/302这样的非200状态。在Nginx是基于proxy_cache实现的缓存功能。

2、Nginx安装ngx_cache扩展

    首先下载Nginx缓存模块,ngx_cache_purge相应版本,这里下载nginx-1.4版本,不同版本对应不同的Nginx版本,安装的时候要留心。

ulimit -SHn 65535  
yum install pcre pcre-devel -y
wget http://nginx.org/download/nginx-1.0.11.tar.gz
     http://labs.frickle.com/files/ngx_cache_purge-1.4.tar.gz
tar zxvf ngx_cache_purge-1.4.tar.gz
tar zxvf nginx-1.0.11.tar.gz
useradd www
cd nginx-1.0.11/ 
./configure --user=www--group=www--add-module=../ngx_cache_purge-1.4 --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install

注意:这个是在安装nginx服务器的时候,在编译过程中就需要添加进去。如果后期需要安装,那么就需要重新编译nginx服务器,将插件编译进去。

3、nginx cache配置

user nobody nobody;
worker_processes 8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
pid /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 102400;
events
{
use epoll;
worker_connections 102400;
}
 
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;
 
 log_format main '$http_x_forwarded_for - $remote_user [$time_local] "$request"'
              '$status $body_bytes_sent "$http_referer" '
              '"$http_user_agent"  $request_time $remote_addr';
 
    proxy_temp_path /data/proxy_temp_dir;
    proxy_cache_path /data/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=2m clean_time=1m;
     
upstream backend_server {
    server 127.0.0.1:8800 weight=1 max_fails=2 fail_timeout=30s;
    server 127.0.0.1:8801 weight=1 max_fails=2 fail_timeout=30s;
}
server
{
    listen 80;
    server_name localhost; 
    index index.html index.htm;
    root /data/webapps/www;
    location /
    {
        #如果后端服务器返回502、504、执行超时等错误,自动将请求发送到upstream负载均衡池
        #中的另一台服务器,实现故障转移。
        proxy_next_upstream http_502 http_504 error timeout invalid_header;
        #使用cache_one这个keys_zone
        proxy_cache cache_one; 
        #200和302状态码保存1小时
        proxy_cache_valid 200 302 1h; 
        #以域名、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;
        add_header X-Cache '$upstream_cache_status from $host';
        expires 1d;
    }
    location ~ /purge(/.*)
    {
        #设置允许指定的IP或IP段输入正确的密码才可以清楚URL缓存
        auth_basic "Please Insert User And Password";
        auth_basic_user_file /usr/local/nginx/conf/htpasswd;
        #设置允许指定的IP或者IP段才可以清除URL缓存
        allow 127.0.0.1;
        allow 192.168.5.0/24;
        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  /usr/local/nginx/logs/access.log main;
  }
}

   有关缓存部分配置详解:

  • proxy_temp_path和proxy_cache_path指定的路径必须在同一分区。
  • proxy_cache_path:这个配置在Server标签外,levels指定该缓存空间有两层HASH目录,第一层1个字母,第二层2个字母,保存的文件名类似于/data/proxy_cache_dir/c/29/asdf777asdf7as6;key_zone为这个空间起个名字,200m指空间大小为200M;inactive的1d指缓存默认时长为1天;max_size的2m指单个文件超过2m就不缓存;clean_time指定1分钟清理一次缓存。

4、清除缓存

    清除缓存有两种方法,第一种是直接通过nginx.conf配置文件定义的/purge虚拟目录去清除,第二种方法可以通过shell脚本去批量清楚。下面是shell脚本清空缓存的内容:

 

#! /bin/sh
#Auto Clean Nginx Cache Shell Scripts
#2013-06-12  wugk
#Define Path
CACHE_DIR=/data/www/proxy_cache_dir/
FILE="$*"
 
#To determine whether the input script,If not, then exit 判断脚本是否有输入,没有输入然后退出
if
  [  "$#" -eq "0" ];then
  echo "Please Insert clean Nginx cache File, Example: $0 index.html index.js"
  sleep 2 && exit
fi
  echo "The file : $FILE to be clean nginx Cache ,please waiting ....."
 
#Wrap processing for the input file, for grep lookup,对输入的文件进行换行处理,利于grep查找匹配相关内容
for i in `echo $FILE |sed 's//\n/g'`
do
   grep -ra  $i  ${CACHE_DIR}| awk -F':' '{print $1}'  > /tmp/cache_list.txt
    for j in `cat/tmp/cache_list.txt`
  do
    rm  -rf  $j
    echo "$i  $j  is  Deleted Success !"
  done
done

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics