nignx + ngx_cache_purge 配备

nignx + ngx_cache_purge 配置

1、下载ngx_cache_purge: http://labs.frickle.com/nginx_ngx_cache_purge/

2、下载nginx :http://nginx.org/download/nginx-1.4.1.tar.gz

3、下载zlib :http://prdownloads.sourceforge.net/libpng/zlib-1.2.8.tar.gz?download

4、下载pcre :http://sourceforge.net/projects/pcre/files/pcre/8.33/pcre-8.33.tar.gz/download

5、解压
     将上面所有包解压到同一目录。
6、安装
     进入解压后的nginx-1.4.1目录,执行。
  ./configure --prefix=/works/nginx --with-pcre=/works/pcre-8.33 --with-zlib=/works/zlib-1.2.7 --add-module=/works/ngx_cache_purge-2.1

 make && make install



7、配置 nginx.conf

   

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    use epoll;
    worker_connections  51200;
}


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

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

   proxy_temp_path /data/proxy_temp_path;
   
   proxy_cache_path /data/proxy_cache_path levels=1:2 keys_zone=ngx_cache:200m inactive=1d max_size=30g;

    #gzip  on;
    upstream my_server_pool{
      server  100.22.160.67:8080;

    }

    server {
        listen       80;
        server_name  xxx.test.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

                
        location ~ /purge(/.*){
               proxy_cache_purge ngx_cache $host$1$is_args$args;
               error_page 405 =200 /purge$1;
         }
         if ( $request_method = "PURGE" ) {
 		   rewrite ^(.*)$ /purge$1 last;
	 }


        location / {
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $remote_addr;
              proxy_pass http://my_server_pool;
        }
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$ {
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_cache ngx_cache;
            proxy_cache_valid 200 304 12h;
            proxy_cache_valid 301 302 1m;    
            proxy_cache_valid any 1m;
            add_header NGX_CACHE $upstream_cache_status; 
            proxy_cache_key $host$uri$is_args$args;     
            proxy_pass http://my_server_pool;
 
         }
         

    }


}

 


8、启动nginx sbin/nginx

#用于清除缓存,假设一个URL为http://xxx.test.com/test.gif,通过访问
    #http://xxx.test.com/purge/test.gif可以清楚该url的缓存