具體實施步驟如下所示:
第一步:獲取清除清除指定URL的緩存安裝介質
wget http://labs.frickle.com/files/ngx_cache_purge-1.6.tar.gz
第二步:編譯並安裝該插件(由於我在安裝該插件之前我已部署了nginx所以此次安裝我只需加載清除緩存模塊的插件就可以了)
./configure --add-module=../ngx_cache_purge-1.6 --with-http_stub_status_module --with-http_ssl_module --add-module=/soft/nginx_mod_h264_streaming-2.2.7 --with-pcre=/soft/pcre-7.9 --with-zlib=/soft/zlib/1.2.3 --prefix=/usr/local/nginx --with-http_flv_module --with-http_stub_status_module --with-openssl-opt=enable --with-http_mp4_module --with-cc-opt=-O3
make && make install
第三步:驗證nginx已經安裝了緩存清除插件
[root@localhost conf]# ../sbin/nginx -V
nginx version: nginx/1.3.3
configure arguments: --add-module=../ngx_cache_purge-1.6 --with-http_stub_status_module --with-http_ssl_module --add-module=/soft/nginx_mod_h264_streaming-2.2.7 --with-pcre=/soft/pcre-7.9 --with-zlib=/soft/zlib/1.2.3 --prefix=/usr/local/nginx --with-http_flv_module --with-http_stub_status_module --with-openssl-opt=enable --with-http_mp4_module --with-cc-opt=-O3根據以上提示信息證明ngx_cache_purge插件已經安裝好了
第三步:配置nginx的緩存功能
cd /usr/local/nginx/conf
配置nginx.conf文件
配置緩存模塊分兩部分:
一、http模塊設置如下所示
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;
##cache##
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;
proxy_temp_path /usr/local/nginx/temp;
proxy_cache_path /usr/local/nginx/cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
##end##
#注:proxy_temp_path和proxy_cache_path指定的路徑須在同一分區
#proxy_cache_path /data0/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
##設置Web緩存區名稱爲cache_one,內存緩存空間大小爲200MB,1天沒有被訪問的內容自動清除,硬盤緩存空間大小爲30GB。
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;
二、server模塊
server
{
listen 80;
server_name www.yourdomain.com 192.168.8.42;
index index.html index.htm;
root /data0/htdocs/www;
location ~ /purge(/.*)
{
#設置只允許指定的IP或IP段纔可以清除URL緩存。
allow 127.0.0.1;
allow 192.168.0.0/24
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}
#用於清除緩存,假設一個URL爲http://192.168.8.42/222.gif,通過訪問http://192.168.8.42/purge/222.gif就可以清除該URL的緩存。
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;
}
location ~ \.flv$ {
proxy_cache_valid 200 302 1h;
proxy_cache_valid 301 1d;
proxy_cache_valid any 10m;
expires 30d;
proxy_cache_key $host$uri$is_args$args;
flv;
}
#對flv視頻的緩存方案設置
location ~ \.mp4$ {
proxy_cache_valid 200 302 1h;
proxy_cache_valid 301 1d;
proxy_cache_valid any 10m;
expires 30d;
proxy_cache_key $host$uri$is_args$args;
mp4;
}
#對mp4視頻文件的緩存方案設置
location ~* \.(txt|doc)$ {
proxy_cache_valid 200 302 1h;
proxy_cache_valid 301 1d;
proxy_cache_valid any 10m;
expires 30d;
proxy_cache_key $host$uri$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;
}