nginx的類squid哈希式cache功能,據(jù)張宴說(shuō)是基本穩(wěn)定可用了,昨天找個(gè)機(jī)會(huì)和時(shí)間,試著測(cè)用了一把,把要點(diǎn)記錄一下: 首先是編譯nginx,方便起見,把一些心儀的模塊統(tǒng)統(tǒng)加上了,version如下: built by gcc 4.1.2 20080704 (Red Hat 4.1.2-44)TLS SNI suppor
nginx的類squid哈希式cache功能,據(jù)張宴說(shuō)是基本穩(wěn)定可用了,昨天找個(gè)機(jī)會(huì)和時(shí)間,試著測(cè)用了一把,把要點(diǎn)記錄一下:
首先是編譯nginx,方便起見,把一些心儀的模塊統(tǒng)統(tǒng)加上了,version如下:
built by gcc 4.1.2 20080704 (Red Hat 4.1.2-44) TLS SNI support disabled configure arguments: --prefix=/home/nginx --with-pcre --add-module=../ngx_http_consistent_hash --add-module=../ngx_max_connections --add-module=../ngx_cache_purge --add-module=../ngx_mp4_streaming_public --with-cc-opt=-O3 --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --without-http_memcached_module --without-http_fastcgi_module --with-google_perftools_module
編譯過(guò)程中幾個(gè)注意事項(xiàng):
話說(shuō)我add這個(gè)max_connections模塊能怎么用自己也沒想好,反正官方有l(wèi)imit_zone和limit_req限制client,再add個(gè)限制nginx2oringin的也不在乎吧……汗~~
比較囧的一點(diǎn)是,經(jīng)過(guò)我折騰的nginx,雖然去除了debug -g模式編譯,還是有4M多大……
sina的ncache模塊,在我下載的最新的nginx0.8.34src上無(wú)法使用,而且ncache作者介紹說(shuō)ncache的緩存不用內(nèi)存,且其purge方式為標(biāo)記為過(guò)期但并不更改文件內(nèi)容直到下次訪問(wèn)請(qǐng)求以節(jié)省磁盤IO的負(fù)擔(dān);但根據(jù)我的試驗(yàn),nginx的cache_purge模塊則是采用了刪除過(guò)期文件的方式進(jìn)行(當(dāng)然,proxy_cache的過(guò)期還是標(biāo)記而不刪除的,不然太耗IO了……)。
然后貼一下,實(shí)驗(yàn)完成后的配置文件:
user nobody nobody; worker_processes 1; google_perftools_profiles /tmp/tcmalloc; worker_rlimit_nofile 65535; events { use epoll; worker_connections 65535; } 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 ; #? 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; #定義cache臨時(shí)緩存路徑,必須和哈希緩存路徑在同一個(gè)磁盤上 proxy_temp_path?? /cache/proxy_temp_dir; #定義cache哈希緩存路徑,目錄層次,緩存名稱及所允許緩存的最大文件大小,未被訪問(wèn)文件多久自動(dòng)清除,緩存最多使用多大磁盤 proxy_cache_path? /cache/proxy_cache_dir? levels=1:2?? keys_zone=cache_one:200m inactive=1d max_size=30g; #后端源站地址 upstream backend{ server 10.10.10.13:80; } server { listen?????? 80; server_name? www.test.com; #這里就是關(guān)鍵部分了,定義哈希緩存及緩存過(guò)期; #因?yàn)閚ginx提供的過(guò)期控制是針對(duì)http_status_code的,我本想通過(guò)location中限定類型的方法完成曲線救國(guó),結(jié)果發(fā)現(xiàn):一旦location中限定了文件類型,緩存過(guò)期的定義就失效!! #也就是說(shuō),限定文件類型后的哈希緩存,是絕絕對(duì)對(duì)的強(qiáng)制永久緩存——不單過(guò)期失效,下面的purge也失效——或許換一個(gè)場(chǎng)景,這個(gè)剛好有用。 #?? location ~* .*.(css|gif|jpg|png|html|swf|flv) location / { #啟用flv拖動(dòng)功能; flv; #這里定義是如果碰上502、504、timeout和invalid_header等情況,自動(dòng)調(diào)向下一個(gè)oringin繼續(xù)請(qǐng)求,這個(gè)有時(shí)候有用,有時(shí)候可能被攻擊的會(huì)很慘…… proxy_next_upstream http_502 http_504 error timeout invalid_header; #使用上面定義的具體某個(gè)緩存; proxy_cache cache_one; #200和304的狀態(tài)碼訪問(wèn)都緩存1天; proxy_cache_valid? 200 304 1d; #由主機(jī)名、唯一資源定位符、參數(shù)判斷符和請(qǐng)求參數(shù)共同生成哈希緩存的key proxy_cache_key $host$uri$is_args$args; #下面幾個(gè)是常見的nginx透明代理header proxy_pass_header User-Agent; proxy_set_header Host? $host; proxy_set_header X-Forwarded-For? $remote_addr; #下面是為了證明給大家看他確實(shí)能緩存,增加的兩句話;如果真想要看到HIT和MISS的話,可以addnginx的另一個(gè)模塊slowfs_cache,配置上和官方的proxy_cache極其相似,不過(guò)自帶有變量$slowfs_cache_status,可以顯示HIT/MISS/EXPIRED。 add_header X-Cache "HIT from cache_test"; add_header Age "1"; proxy_pass http://backend; } #下面這段就是add的purge_mod,只要在url的^/前再加上/purge,就會(huì)自動(dòng)被理解成PURGE請(qǐng)求,刷新成功返回200的特定頁(yè)面,失敗返回404的普通錯(cuò)誤頁(yè)面。 #這個(gè)proxy_cache_purge格式?jīng)]法改變,我本想改成if ($request_method = PUREG){…}試試,結(jié)果發(fā)現(xiàn)它不認(rèn)…… location ~ /purge(/.*){ allow??????????? 127.0.0.1; allow??????????? 211.151.67.0/24; deny??????????? all; proxy_cache_purge??? cache_one?? $host$1$is_args$args; } #不區(qū)分大小寫匹配~*,網(wǎng)上流傳很廣的寫法*~*是錯(cuò)滴;而且能匹配不代表保存同一份緩存; #這部分定義不緩存而是透?jìng)鞯恼?qǐng)求類型。介于無(wú)法通過(guò)類型來(lái)控制緩存,那么這里不緩存的控制就必須確保嚴(yán)格正確了…… #可是bug來(lái)了,當(dāng)我寫成swf?$的時(shí)候,swf/swf?/swf?*都不緩存;寫成swf?的時(shí)候,又變成都緩存——nginx壓根就分不清! location ~* .*.(swf|asp)?{ proxy_pass_header User-Agent; proxy_set_header Host $host; proxy_set_header X-Forwarder-For $remote_addr; add_header X-Cache "MISS from cache_test"; proxy_pass http://backend; } } }
配置完成。測(cè)試如下: wget http://www.test.com/List/j.Html -S -O /dev/null -e http_proxy=127.0.0.1 –13:17:48–? http://www.test.com/List/j.Html Connecting to 127.0.0.1:80… connected. Proxy request sent, awaiting response… HTTP/1.1 200 OK Server: squid/2.6.STABLE21 Date: Sun, 07 Mar 2010 05:17:48 GMT Content-Type: text/html; charset=utf-8 Connection: close Vary: Accept-Encoding Content-Length: 25126 Last-Modified: Wed, 10 Feb 2010 09:38:26 GMT ETag: “948acecb34aaca1:6647” X-Powered-By: ASP.NET X-Cache: HIT from cache_test Age: 1 Accept-Ranges: bytes Length: 25126 (25K) [text/html] Saving to: `/dev/null’
100%[====================================================================================================================>] 25,126????? --.-K/s?? in 0s
13:17:48 (521 MB/s) - `/dev/null’ saved [25126/25126]
[root@sdl4 ~ 13:17:48]# wget -S -O /dev/null -e http_proxy=127.0.0.1 “http://www.test.com/Search.ASP?KeyWord=整形視頻” –13:17:50–? http://www.test.com/Search.ASP?KeyWord=%D5%FB%D0%CE%CA%D3%C6%B5 Connecting to 127.0.0.1:80… connected. Proxy request sent, awaiting response… HTTP/1.1 200 OK Server: squid/2.6.STABLE21 Date: Sun, 07 Mar 2010 05:17:51 GMT Content-Type: text/html; charset=utf-8 Connection: close Vary: Accept-Encoding X-Powered-By: ASP.NET Content-Length: 24987 Set-Cookie: ASPSESSIONIDSSQQSBST=KDPDLODBNLGONONBNHCJCEGP; path=/ Cache-control: private X-Cache: MISS from cache_test Length: 24987 (24K) [text/html] Saving to: `/dev/null’
100%[====================================================================================================================>] 24,987????? 25.6K/s?? in 1.0s 13:17:52 (25.6 KB/s) - `/dev/null' saved [24987/24987] 完成。 至于?的問(wèn)題,目前針對(duì)需要,倒有另一個(gè)辦法: 在location / {}中,根據(jù)請(qǐng)求參數(shù)判斷進(jìn)行傳遞。即寫成如下:
location / { …… if ($is_args){ add_header X-Cache "MISS from cache_test"; proxy_pass http://backend; } } location ~* .*\.asp{ proxy_pass_header User-Agent; proxy_set_header Host $host; proxy_set_header X-Forwarder-For $remote_addr; add_header X-Cache "MISS from cache_test"; proxy_pass http://backend; }
不過(guò)依然有問(wèn)題:
set $yn $is_args; if ($uri ~* .(htm|jpg)){ set $yn ""; } if ($yn){ proxy_pass http://backend; }
; 2. nginx的if中不單不支持proxy_cache,居然也不支持proxy_set_header等定義,只能單純的proxy_pass。
原文地址:nginx的proxy_cache和cache_purge模塊試用記錄, 感謝原作者分享。
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com