web緩存位於內容源Web服務器和客戶端之間,當用戶訪問一個URL時,Web緩存服務器會去後端Web源服務器取回要輸出的內容,而後,當下一個請求到來時,若是訪問的是相同的URL,Web緩存服務器直接輸出內容給客戶端,而不是向源服務器再次發送請求.Web緩存下降了內容源Web服務器,數據庫的負載,減小了網絡延遲,提升了用戶訪問的響應速度,加強了用戶體驗.javascript
web緩存服務器中,最著名的要數Squid Cache(簡稱爲Squid),Squid是一個流浪的自由軟件的代理服務器和Web緩存服務器。
----------------------------------------------------------------------------------------------------------------------------
Squid能夠做爲網頁服務器的前置cache服務器緩存相關請求來提升Web服務器的速度;
Squid能夠爲一組人共享網絡資源而緩存萬維網,域名系統和其餘網絡搜索;
Squid能夠經過過濾流量幫助網絡安全,到局域網經過代理上網.
----------------------------------------------------------------------------------------------------------------------------
然而,當下多數公司網站的圖片,js,css等文件的緩存會選擇Nginx的web緩存服務。php
以下將對nginx的web緩存功能的總體配置進行梳理性記錄:
Nginx的Web緩存服務主要由proxy_cache相關指令集和fastcgi_cache相關指令集構成。
1)proxy_cache相關指令集用於反向代理時,對後端內容源服務器進行緩存.Nginx的proxy_cache緩存功能,十分穩定,速度不遜於Squid!css
2)fastcgi相關指令集主要用於對FastCGI的動態程序進行緩存.二者功能基本同樣.在功能上,Nginx已經具有Squid所擁有的Web緩存加速功能,清除指定URL緩存功能.而在性能上,Nginx對多核CPU的利用,賽過Squid很多.另外,在反向代理,負載均衡,健康檢查,後端服務器故障轉移,重寫,易用性上,Nginx也比Squid強大不少.這使得一臺Nginx能夠同時做爲"負載均衡服務器"與"Web緩存服務器"來使用.html
proxy_cache相關指令集
(1)proxy_cache指令
語法: proxy_cache zone_name ;
該指令用於設置哪一個緩存區將被使用,zone_name的值爲proxy_cache_path指令建立的緩存區的名稱.java
(2)proxy_cache_path指令
語法 proxy_cache_path path [levels=number]node
keys_zone=zone_name:zone_size[inactive=time] [max_size=size];
該指令用於設置緩存文件的存放路徑.nginx
例如:
proxy_cache_path /usr/local/nginx/proxy_cache_dir levels=1:2 keys_zone=cache_one:500m inactive=1d max_size=30g ;
解釋:
path 表示存放目錄
levels 表示指定該緩存空間有兩層hash目錄,第一層目錄爲1個字母,第二層目錄爲2個字母,
保存的文件名會相似/usr/local/nginx/proxy_cache_dir/c/29/XXXXXX ;
keys_zone參數用來爲這個緩存區起名.
500m 指內存緩存空間大小爲500MB
inactive的1d指若是緩存數據在1天內沒有被訪問,將被刪除。至關於expires過時時間的配置;
max_size的30g是指硬盤緩存空間爲30Gweb
(3)proxy_cache_methods指令
語法:proxy_cache_methods[GET HEAD POST];
該指令用於設置緩存哪些HTTP方法,默認緩存HTTP GET/HEAD方法,不緩存HTTP POST 方法數據庫
(4)proxy_cache_min_uses指令
語法:proxy_cache_min_uses the_number
該指令用於設置緩存的最小使用次數,默認值爲1vim
(5)proxy_cache_valid指令
語法: proxy_cache_valid reply_code [reply_code...] time ;
該指令用於對不一樣返回狀態碼的URL設置不一樣的緩存時間.
例如:
proxy_cache_valid 200 302 10m ;
proxy_cache_valid 404 1m ;
設置200,302狀態的URL緩存10分鐘,404狀態的URL緩存1分鐘.
(6)proxy_cache_key指令
語法: proxy_cache_key line ;
該指令用來設置Web緩存的Key值,Nginx根據Key值md5哈希存儲緩存.通常根據$host(域名),$request_uri(請求的路徑)等變量組合成proxy_cache_key .
proxy_cache緩存配置的完整示例(多數nginx緩存的配置):
1)下載nginx和第三方的ngx_cache_purge模塊的編譯安裝包(官網:http://labs.frickle.com/nginx_ngx_cache_purge/),將ngx_cache_purge編譯到到Nginx中,用來清除指定URL的緩存
[root@test-huanqiu ~]# yum install -y pcre pcre-devel openssl openssl-devel gcc //首先安裝依賴
[root@test-huanqiu ~]# cd /usr/local/src
[root@test-huanqiu src]# wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
[root@test-huanqiu src]# wget http://nginx.org/download/nginx-1.8.0.tar.gz
[root@test-huanqiu src]# tar -zxvf ngx_cache_purge-2.3.tar.gz
[root@test-huanqiu src]# tar zxvf nginx-1.8.0.tar.gz
[root@test-huanqiu src]# cd nginx-1.8.0.tar.gz
[root@test-huanqiu nginx-1.8.0]# ./configure --user=www --group=www --add-module=../ngx_cache_purge-2.3 --prefix=/usr/local/nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre
[root@test-huanqiu src]# make && make install
此時啓動nginx的話會看到新增了兩個進程:
2)接着,在同一分區下建立兩個緩存目錄,分別供proxy_temp_path , proxy_cache_path指令設置緩存路徑.
注意:兩個指定設置的緩存路徑必須爲同一磁盤分區,不能跨分區.
[root@test-huanqiu src]# mkdir -p /usr/local/nginx/proxy_temp_path
[root@test-huanqiu src]# mkdir -p /usr/local/nginx/proxy_cache_path
3)在配置文件nginx.conf中對擴展名爲gif,jpg,jpeg,png,bmp,swf,js,css的圖片,flash,javascript , css文件開啓Web緩存,其餘文件不緩存。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
[root@
test
-huanqiu src]
# vim /usr/local/nginx/conf/nginx.conf
user www;
worker_processes 8;
events {
worker_connections 65535;
}
http {
include mime.types;
default_type application
/octet-stream
;
charset utf-8;
log_format main
'$http_x_forwarded_for $remote_addr $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_cookie" $host $request_time'
;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
#要想開啓nginx的緩存功能,須要添加此處的兩行內容!
#設置Web緩存區名稱爲cache_one,內存緩存空間大小爲500M,緩存的數據超過1天沒有被訪問就自動清除;訪問的緩存數據,硬盤緩存空間大小爲30G
proxy_cache_path
/usr/local/nginx/proxy_cache_path
levels=1:2 keys_zone=cache_one:500m inactive=1d max_size=30g;
#建立緩存的時候可能生成一些臨時文件存放的位置
proxy_temp_path
/usr/local/nginx/proxy_temp_path
;
fastcgi_connect_timeout 3000;
fastcgi_send_timeout 3000;
fastcgi_read_timeout 3000;
fastcgi_buffer_size 256k;
fastcgi_buffers 8 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
client_header_timeout 600s;
client_body_timeout 600s;
client_max_body_size 100m;
client_body_buffer_size 256k;
gzip
on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 9;
gzip_types text
/plain
application
/x-javascript
text
/css
application
/xml
text
/javascript
application
/x-httpd-php
;
gzip_vary on;
include vhosts/*.conf;
}
|
[root@test-huanqiu src]# ulimit -n 65535
[root@test-huanqiu src]# mkdir /usr/local/nginx/conf/vhosts
[root@test-huanqiu src]# vim /usr/local/nginx/conf/vhosts/wang.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
upstream LB-WWW {
ip_hash;
server 192.168.1.101:80 max_fails=3 fail_timeout=30s;
#max_fails = 3 爲容許失敗的次數,默認值爲1
server 192.168.1.102:80 max_fails=3 fail_timeout=30s;
#fail_timeout = 30s 當max_fails次失敗後,暫停將請求分發到該後端服務器的時間
server 192.168.1.118:80 max_fails=3 fail_timeout=30s;
}
server {
listen 80;
server_name www.wangshibo.com;
index index.html index.php index.htm;
root
/var/www/html
;
access_log
/usr/local/nginx/logs/www-access
.log main;
error_log
/usr/local/nginx/logs/www-error
.log;
location / {
proxy_pass http:
//LB-WWW
;
proxy_redirect off ;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 300;
#跟後端服務器鏈接超時時間,發起握手等候響應時間
proxy_send_timeout 300;
#後端服務器回傳時間,就是在規定時間內後端服務器必須傳完全部數據
proxy_read_timeout 600;
#鏈接成功後等待後端服務器的響應時間,已經進入後端的排隊之中等候處理
proxy_buffer_size 256k;
#代理請求緩衝區,會保存用戶的頭信息以供nginx進行處理
proxy_buffers 4 256k;
#同上,告訴nginx保存單個用幾個buffer最大用多少空間
proxy_busy_buffers_size 256k;
#若是系統很忙時候能夠申請最大的proxy_buffers
proxy_temp_file_write_size 256k;
#proxy緩存臨時文件的大小
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css)$ {
#使用Web緩存區cache_one,已在nginx.conf的緩存配置中命名的。
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設置
#假設一個URL爲http://www.wangshibo.com/test.gif,那麼就能夠經過訪問http://www.wangshibo.com/purge/test.gif清除該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 ;
}
}
|
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 /usr/local/nginx/fastcgi_cache_dir levels=1:2 keys_zone=cache_one:500m inactive=1d max_size=30g ;
注意這個指令只能在http標籤內配置,
levels指定該緩存空間有兩層hash目錄,第一層目錄爲1個字母,第二層爲2個字母,保存的
文件名會相似/usr/local/nginx/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。
fastcgi_cache緩存配置的完整示例
1)首先,在同一分區下建立兩個緩存目錄,分別供fastcgi_temp_path,fastcgi_cache_path指令設置緩存路徑.
注意:兩個指定設置的緩存路徑必須爲同一磁盤分區,不能跨分區.
[root@test-huanqiu src]# mkdir -p /usr/local/nginx/fastcgi_temp_path
[root@test-huanqiu src]# mkdir -p /usr/local/nginx/fastcgi_cache_path
2)配置文件nginx.conf對擴展名爲gif,jpg,jpeg,png,bmp,swf,js,css的圖片,Flash,JavaScript,CSS文件開啓Web緩存,其餘文件不緩存.
1
2
3
4
5
6
7
8
9
10
11
|
[root@
test
-huanqiu src]
# vim /usr/local/nginx/conf/nginx.conf
........
http{
#fastcgi_temp_path和fastcgi_cache_path指定的路徑必須在同一分區
fastcgi_temp_path
/usr/local/nginx/fastcgi_temp_path
;
#設置Web緩存區名稱爲cache_one,內存緩存空間大小爲500MB,自動清除超過1天沒有被
#訪問的緩存數據,硬盤緩存空間大小爲30G
fastcgi_cache_path
/usr/local/nginx/fastcgi_cache_path
levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g ;
........
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
[root@
test
-huanqiu src]
# vim /usr/local/nginx/conf/vhosts/wang.conf
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 index.php ;
include fcgi.conf ;
}
}
|
-------------------------------------------------------------------------------------------------------------------------------------------------------------
個人我的示例主配置文件: 下面 $upstream_cache_status表示資源緩存的狀態,有HIT MISS EXPIRED三種狀態
log_format access '$remote_addr,$http_x_forwarded_for,$remote_user,$time_local,$host,$request,$status,$request_uri,$http_referer,$reques t_time,$http_user_agent,$upstream_cache_status'; proxy_temp_path /usr/local/nginx110/proxy_temp_dir 1 2; proxy_cache_path /data/ifengsite/proxy_cache_dir/cache1 levels=1:2 keys_zone=cache1:200m inactive=1d max_size=100g;
upstream houseservers {
server 10.0.10.31:80 max_fails=3 fail_timeout=60 weight=1;
server 10.0.10.32:80 max_fails=3 fail_timeout=60 weight=1;
server 10.0.10.33:80 max_fails=3 fail_timeout=60 weight=1;
}
server配置文件:注意purge的location須要處於被緩存的內容的location的前面,不然會被匹配攔截,沒法準確匹配到purge!
location / { proxy_pass http://houseservers; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } location ~ /purge(/.*) { allow 127.0.0.1; allow 10.0.0.0/16; deny all; proxy_cache_purge cache1 $1$is_args$args; } location ~* \.(jpg|html|shtml)$ { proxy_pass http://houseservers;
proxy_set_header Host $host;
proxy_set_header X-Real_IP $remote_add; proxy_cache cache1; proxy_cache_key $uri$is_args$args; add_header X-Cache $upstream_cache_status; proxy_ignore_headers "Cache-Control" "Expires" "Set-Cookie";
expires 15d;
}
這個緩存是把連接用md5編碼hash後保存,因此它能夠支持任意連接,同時也支持404/301/302這樣的非200狀態
查看你的nginx是根據什麼做爲key來hash的,個人設置是 proxy_cache_key $uri$is_args$args; 所以nginx會根據$uri$is_args$args做爲key進行hash,所以能夠模擬nginx對一個key進行再hash找到相應的文件路徑,刪除(具體可隨意找個緩存文件 more 一下看看)
相關參考資料:http://www.cnblogs.com/Eivll0m/p/4921829.html
http://blog.csdn.net/czp11210/article/details/28596649