不管是前面介紹的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