

下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、Nginx模塊fastcgi_cache的幾個注意點在 web 項目中,大家都已經非常熟悉其架構流程了。都說 Cache 是萬金油,哪里不舒服抹哪里。這些流程中,幾乎每個環節都會進行cache。從瀏覽器到 webserver,到 cgi 程序,到DB 數據庫,會進行瀏覽器cache,數據 cache,SQL 查詢的 cache 等等。對于 fastcgi 這里的 cache,很少被使用。去年年底,我對nginx 的 fastcgi_cache進行摸索使用。在我的 測試過程中,發現一些 WIKI 以及網絡上沒被提到的注意點,這里分享一下。從瀏覽器到數據庫的流程圖這里是我的 NGinx 配置信息#
2、增加調試信息add_header X-Cache-CFC $upstream_cache_status - $upstream_resp on se_time;fastcgi_temp_path /dev/shm/ngin x_tmp;#cache 設置fastcgi_cache_path /dev/shm/ngin x_cache levels=1:2 keys_z on e=cfcache:10min active=50m;fastcgi_cache_key $request_method:/$host$request_uri;fastcgi_cache_methods GET HEAD;
3、fastcgi_cache cfcache;fastcgi_cache_valid any 1d;fastcgi_cache_ min _uses 1;fastcgi_cache_use_stale error timeout in valid_header http_500;fastcgi_ig no re_clie nt_abort on;配置這些參數時,注意每個參數的作用域,像fastcgi cache path 參數,只能在 http酉己置項里配置,而 fastcgi_cache_min_uses這個參數,可以在 http、server、location三個配置項里配置。這樣更靈活的會
4、每個域名、每個匹配的 location 進行選擇性 cache 了。 具體的參數作用域,參考FASTCGI 模塊的官方 WIKI。我為了調試方便,添加了一個X-Cache-CFC的 http 響應頭,$upstream_cache_status變量表示此請求響應來自cache的狀態,分別為:MISS 未命中EXPIRED - expired, request was passed to backend Cache已過期UPDATING- expired, stale response was used due to proxy/fastcgi_cache_use_stale updatingC
5、ache已過期,(被其他 nginx 子進程)更新中STALE - expired, stale response was used due to proxy/fastcgi_cache_use_staleCache 已過期,響應數據不合法,被污染HIT 命中 cacheFASTCGI CACHE $upstream cache status 結果為 miss,次也沒命中程序代碼是 Discuz!論壇,隨便開啟測試了幾下,發現/dev/shm/nginx_cache/ 下沒有任何目錄建立,也沒有文件創建。調試的http header響應頭里的 X-Cache-CFC 結果一直是 MISSo 從
6、服務器 進程上來看, Nginx cache manager process跟 Nginx cache loaderprocess 進程也正常運行:root 3100 1 0 14:52 ? 00:00:00 ngi nx: master process /usr/sb in/ngi nxwww-data 3101 3100 0 14:52 ? 00:00:00 ngi nx: worker processwww-data 3102 3100 0 14:52 ? 00:00:00 ngi nx: cache ma nager processwww-data 3103 3100 0 14:52
7、? 00:00:00 ngi nx: cache loader process不知道為何會這樣,為何沒有 cache 成功,我以為我配置參數有問題,只好閱讀 WIKI。4r一二說w-IL-.FI-二蘭=匯壬-:尸汩.二配置這些參數時,注意每個參數的作用域,像fastcgi cache path 參數,只能在 http發現 fastcgi_ignore_headers參數下解釋有這么一段fastcgi ignore headersSyn tax: fastcgi_ig nore_headers fieldDefault:Con text: httpserverlocatio nRefere ne
8、e: fastcgi_ig nore_headersThis directive forbidsprocessing of the named headers from the FastCGI-serverreply. It is possible to specify headers like“ X-Accel-Redirect ” ,“X-Accel-Expires ” ,“ Expires ” or“ Cache-Control ” .也就是說這個參數的值,將會被忽略掉,同樣被忽略掉的響應頭比如”X-Accel-Redirect ” , “X-Accel-Expires ” , “ Ex
9、pires ” or “Cache-Control ”,而 nginx 配置中并沒有 fastcgi_ignore_headers參數的設定,那么問題會不會出現在FASTCGI 響應結果里包含了類似” X-Accel-Redirect ” , “X-Accel-Expires ” , “ Expires ” or“Cache-Control ”這幾個響應頭呢?用 strace 抓包,看了下 nginx 與 fpm 進程通訊的數據#為了確保準確抓到處理該http 請求的進程,我把nginx、fpm 都只開啟了一個進程處理。/strace -ff -tt -s 1000 -o xxx.log -p
10、 PHPFPM-PID14:52:07.837334 write(3, 1601034350X-Powered-By:PHP/5.3.10-1ubu ntu3.5r nExpires:Thu, 19 Nov 1981 08:52:00 GMTrnCache-Co ntrol:no-store, no-cache, must-revalidate, post-check=0, pre-check=0r nPragma: no-cacher nContent-type: text/htmlr nrnH ellocfc4n 13620343270000013010100000000000, 256)
11、 = 256/strace -ff -tt -s 1000 -o xxx.log -p Ngi nx-PID15:05:13.265663 recvfrom(12, 1601034350X-Powered-By:PHP/5.3.10-1ubu ntu3.5r nExpires:Thu, 19 Nov 1981 08:52:00 GMTrnCache-Co ntrol:no-store, no-cache, must-revalidate, post-check=0, pre-check=0r nPragma: no-cacher nContent-type: text/htmlr nrnH e
12、llocfc4n 13620351130000013010100000000000,4023, 0, NULL, NULL)=256從抓取的數據包里可以看到,fpm 確實返回了包含Expires 、“ Cache-Control ”頭的 http 響應頭信息。那么疑問來了:nginx 的 fastcgi cache沒緩存這條 http 響應,是因為響應頭里包含Expires ”、Cache-Co ntrol ”的原因嗎程序里并沒有輸出Expires ”、“ Cache-Control ” http header的代碼,這是誰輸出的呢?既然是 fpm 響應的時候,就已經有了,那么是 php 的
13、core 模塊,還是其他拓展模塊輸 出的?“Expires: ” 時間為何是“ Thu, 19 Nov 1981 08:52:00 GMT ” ?疑問比較多,一個一個查起,先從 Nginx 的 fastcgi_cache 沒緩存這條 http 響應查起。 我根據測試環境 nginx 版本 1.1.9(ubuntu 12.04 默認的),到 nginx 官方下了對應版本的源 碼,搜索了 fastcgi參數使用的地方,在 httpngx_http_upstream.c 找到了。雖然不能很 流程的讀懂 nginx 的代碼,但粗略的了解,根據了解的情況加以猜測,再動手測試實驗,也得出了結論,確定了 n
14、ginx 的 fastcgi_cache 的規則。n gx_http_upstream.c/line 3136 當 fastcgi響應包含 set-cookie 時,不緩存static ngx_int_tn gx_http_upstream_process_set_cookie( ngx_http_request_t *r, n gx_table_elt_t*h,ngx_uint_t offset)#if (NGX_HTTP_CACHE)n gx_http_upstream_t *u;u = r-upstream;if (!(u-co nf-ig no re_headers & NGX
15、_HTTP_UPSTREAM_IGN_SET_COOKIE) u-cacheable = 0;#en difreturn NGX_OK;/line 3242當響應頭包含 Expires 時,如果過期時間大于當前服務器時間,則nginx_cache 會緩存該響應,否則,則不緩存static ngx_int_tn gx_http_upstream_process_expires (n gx_http_request_tngx_uint_t offset)n gx_http_upstream_t *u;u = r-upstream;u-headers_i n.expires = h;#if (NGX
16、_HTTP_CACHE)time_t expires;if (u-co nf-ig no re_headers & NGX_HTTP_UPSTREAM_IGN_EXPIRES) return NGX_OK;if (r-cache = NULL) return NGX_OK;if (r-cache-valid_sec != 0) return NGX_OK;expires = n gx_http_parse_time(h-value.data, h-value .len);if (expires = NGX_ERROR | expires cacheable = 0;return NGX
17、_OK;r-cache-valid_sec = expires;#en difreturn NGX_OK;*h,line 3199當響應頭包含 Cache-Control 時,#如果#這里有如果啊。/【注意】如果 Cache-Control 參數值為 no-cache、no-store、private 中任意一個時,則不緩存不緩存II【注意】如果 Cache-Control 參數值為 max-age 時,會被緩存,且 nginx 設置的 cache 的過期時間,就是系統當前時間+ mag-age 的值if (n gx_strlcasestrn(p, last, (u_char *) n o-c
18、ache, 8 - 1) != NULL| ngx_strlcasestrn(p, last, (u_char *) n o-store, 8 - 1) != NULL| ngx_strlcasestrn(p, last, (u_char *) private, 7 - 1) != NULL)u-cacheable = 0;return NGX_OK;p = n gx_strlcasestr n( p, last, (u_char *) max-age=, 8 - 1);if (p = NULL) return NGX_OK;r-cache-valid_sec = n gx_time() +
19、n;也就是說,fastcgi 響應 http 請求的結果中,響應頭包括Expires、Cache-Control、Set-Cookie 三個,都會可能不被cache,但不只有這些,別忘了 nginx 配置中fastcgi_ignore_headers參數設定的部分。以及ngxin 的 X-ACCEL X-Accel-Redirect 、X-Accel-Expires 、X-Accel-Charset 、X-Accel-Buffering等 nginx 自定義的響應頭。由于這幾個不常用,我也沒深入研究。通過對nginx 的 ngx_http_upstream 模塊代碼模糊理解,加猜測,以及寫了腳
20、本測試驗證,可以得到結論是正確的。即Ngi nx fastcgi_cache 在緩存后端 fastcgi 響應時,當響應里包含“ set-cookie ”時,不緩存;當響應頭包含 Expires 時, 如果過期時間大于當前服務器時間,則nginx_cache 會緩存該響應,否則,則不緩存;當響應頭包含 Cache-Control 時,如果 Cache-Control 參數值為 no-cache、no-store、private 中任意一個時,則不緩存,如果Cache-Control 參數值為 max-age 時,會被緩存,且 nginx設置的 cache 的過期時間,就是系統當前時間+ mag
21、-age 的值。FASTCGI_CACHE $upstream_cache_status 結果為 miss,次也沒命中。/逐個測試,測試時,注釋其他的header(Expires: .gmdate(D, d M Y H:i:s, time()+10000). GMT);header(Expires: .gmdate(D, d M Y H:i:s, time()-99999). GMT); header(X-Accel-Expires:30);header(Cache-C on trol: no-cache);header(Cache-C on trol: no-store);header(Ca
22、che-C on trol: private);header(Cache-C on trol: max-age=10);setcookie(cfc4 n,testaaaa);echo Hello cfc4n ,time();到了這里,疑問 1 解決了。那么疑問 2、3 呢?程序里并沒有輸出“ Expires“Cache-Control ” http header的代碼,這是誰輸出的呢?既然是 fpm 響應的時候,就已nginx fastcgi_cache響應 expirednginx fastcgi_cache hit 命中經有了,那么是 php 的 core 模塊,還是其他拓展模塊輸出的?我
23、精簡了代碼,只輸出一個“hello world,發現也確實被緩存了。顯然, php 腳本程序中并沒輸出http header 的“ Expires ”、“ Cache-Control ”,多次測試,最終定位到session_start函數,翻閱源碼找到了這些代碼:/ext/sessio n/sessio n.c lin e:1190左右/ .CACHE_LIMITER_FUNC(private) /* */ADD_HEADER(Expires: Thu, 19 Nov 1981 08:52:00 GMT);CACHE_LIMITER(private_ no_expire)(TSRMLS_C);
24、/* */再到這里 3 或者上面幾個 #默認是 nocacheCACHE_LIMITER_FUNC( no cache) /* */ADD_HEADER(Expires: Thu, 19 Nov 1981 08:52:00 GMT);/* For HTTP/1.1 con formi ng clie nts and the rest (MSIE 5) */ADD_HEADER(Cache-Control: no-store, no-cache, must-revalidate, post-check=0,pre-check=0);/* For HTTP/1.0 con formi ng cli
25、e nts */ADD_HEADER(Pragma: n o-cache);/* */這里 2static php_sessi on _cache_limiter_t php_sessi on _cache_limiters = CACHE_LIMITER_ENTRY(public)CACHE_LIMITER_ENTRY(private)CACHE_LIMITER_ENTRY(private_ no_expire)CACHE_LIMITER_ENTRY( nocache)0;static int php_session_cacheimiter(TSRMLS_D) /* */php_sessi
26、on _cache_limiter_t *lim;if (PS(cache_limiter)0 = 0) return 0;if (SG(headers_sent) const char *output_start_filename= php_output_get_start_filename(TSRMLS_C);int output_start_li neno = php_output_get_start_li nen o(TSRMLS_C);if (output_start_file name) php_error_docref(NULLTSRMLS_CCE_WARNING:Cannot
27、send session cache limiter-headers already sen t (output started at %s:%d), output_start_file name, output_start_li neno); else php_error_docref(NULLTSRMLS_CCE_WARNING:Cannot send session cache limiter-headers already sen t);return -2;for (lim = php_sessi on _cache_limiters; lim-n ame; lim+) if (!strcasecmp(lim-n ame, PS(cache_limiter) lim-fu nc(TSRMLS_C); /這
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 家禽孵化廠管理制度
- 弱電井施工管理制度
- 律師所案件管理制度
- 德克士庫存管理制度
- 快遞pda管理制度
- 快餐分餐間管理制度
- 總決算編制管理制度
- 總經辦規章管理制度
- 慈善會印章管理制度
- 成品庫盤點管理制度
- 2025至2030年中國高鎳三元材料產業發展動態及投資方向分析報告
- 2025年畢節市大方富民村鎮銀行招聘題庫帶答案分析
- (2025)國家公務員考試時事政治必考試題庫與答案
- 2025影視拍攝場地布置合同協議書
- 2017司考題目及答案
- 2025年D-對羥基苯甘氨酸項目市場調查研究報告
- 國泰君安補簽風險協議書
- 防排煙系統設計畢業答辯
- 2025年人工智能應用技術職業資格考試試卷及答案
- 預防強對流天氣安全教育
- 2025年一級建造師《市政實務》考點精粹
評論
0/150
提交評論