![(完整版)Nginx模塊fastcgi_cache的幾個注意點_第1頁](http://file2.renrendoc.com/fileroot_temp3/2021-5/16/78dc7d8b-8cc9-4d53-9a29-59f828017e14/78dc7d8b-8cc9-4d53-9a29-59f828017e141.gif)
![(完整版)Nginx模塊fastcgi_cache的幾個注意點_第2頁](http://file2.renrendoc.com/fileroot_temp3/2021-5/16/78dc7d8b-8cc9-4d53-9a29-59f828017e14/78dc7d8b-8cc9-4d53-9a29-59f828017e142.gif)
![(完整版)Nginx模塊fastcgi_cache的幾個注意點_第3頁](http://file2.renrendoc.com/fileroot_temp3/2021-5/16/78dc7d8b-8cc9-4d53-9a29-59f828017e14/78dc7d8b-8cc9-4d53-9a29-59f828017e143.gif)
![(完整版)Nginx模塊fastcgi_cache的幾個注意點_第4頁](http://file2.renrendoc.com/fileroot_temp3/2021-5/16/78dc7d8b-8cc9-4d53-9a29-59f828017e14/78dc7d8b-8cc9-4d53-9a29-59f828017e144.gif)
![(完整版)Nginx模塊fastcgi_cache的幾個注意點_第5頁](http://file2.renrendoc.com/fileroot_temp3/2021-5/16/78dc7d8b-8cc9-4d53-9a29-59f828017e14/78dc7d8b-8cc9-4d53-9a29-59f828017e145.gif)
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、配置這些參數時,注意每個參數的作用域,像fastcgi cache path 參數,只能在httpNginx 模塊fastcgi_cache的幾個注意點在web項目中,大家都已經非常熟悉其架構流程了。都說Cache是萬金油,哪里不舒服抹哪里。這些流程中,幾乎每個環(huán)節(jié)都會進行cache。從瀏覽器到 webserver,到cgi程序,到DB數據庫,會進行瀏覽器cache,數據cache,SQL查詢的cache等等。對于fastcgi這里的cache,很少被使用。去年年底,我對nginx的fastcgi_cache進行摸索使用。在我的 測試過程中,發(fā)現一些 WIKI以及網絡上沒被提到的注意點,這里分
2、享一下。從瀏覽器到數據庫的流程圖這里是我的NGinx配置信息#增加調試信息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;f
3、astcgi_cache_methods GET HEAD;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_min_uses這個參數,可以在 http、server、location三個配置項里配置。這樣更靈活的會每個域名、每個匹配的location 進
4、行選擇性cache 了。 具體的參數作用域,參考FASTCGI模塊的官方 WIKI。我為了調試方便,添加了一個X-Cache-CFC的http 響應頭,$upstream_cache_status變量表示此請求響應來自cache的狀態(tài),分別為:MISS未命中EXPIRED - expired, request was passed to backend Cache已過期UPDATING- expired, stale response was used due to proxy/fastcgi_cache_use_stale updating Cache已過期,(被其他 nginx 子進程)更
5、新中STALE - expired, stale response was used due to proxy/fastcgi_cache_use_staleCache已過期,響應數據不合法,被污染HIT 命中 cache4r一二說 w- -IL-.FI- 二蘭=匯壬-:尸汩.二FASTCGI CACHE $upstream cache status 結果為 miss,次也沒命中程序代碼是 Discuz!論壇,隨便開啟測試了幾下,發(fā)現/dev/shm/nginx_cache/ 下沒有任何目錄建立,也沒有文件創(chuàng)建。調試的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。發(fā)現fastcgi_ignore_headers參數下解釋有這么一段fastcgi ignore headersSyn tax: fastcgi_ig nore_headers fieldDefault:Con text: httpserverlocatio nRefere nee: fastcgi_ig nore_headersThis directive forbidsprocessing of the named heade
8、rs 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 ” , “ Expires ” or “Cache-Control ”,而 nginx 配置中并沒有fastcgi_ignore_headers參數的設定,那么問題會不會出
9、現在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 PHPFPM-PID14:52:07.837334 write(3, 1601034350X-Powered-By:PHP/5.3.10-1ubu ntu3.5r nExp
10、ires: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 nConten t-type: text/htmlr nrnH ellocfc4n 13620343270000013010100000000000, 256) = 256/strace -ff -tt -s 1000 -o xxx.log -p Ngi nx-PID15:05:13.265663 recvfrom(12, 160
11、1034350X-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 nConten t-type: text/htmlr nrnH ellocfc4n 13620351130000013010100000000000,4023, 0, NULL, NULL)=256從抓取的數據包里可以看到,fpm確實返
12、回了包含Expires 、“ Cache-Control ”頭的http響應頭信息。那么疑問來了:nginx的fastcgi cache沒緩存這條 http 響應,是因為響應頭里包含Expires ”、Cache-Co ntrol ”的原因嗎程序里并沒有輸出Expires ”、“ Cache-Control ” http header的代碼,這是誰輸出的呢?既然是fpm響應的時候,就已經有了,那么是 php的core模塊,還是其他拓展模塊輸 出的?“Expires: ” 時間為何是“ Thu, 19 Nov 1981 08:52:00 GMT ” ?疑問比較多,一個一個查起,先從Nginx的f
13、astcgi_cache 沒緩存這條http響應查起。 我根據測試環(huán)境 nginx版本1.1.9(ubuntu 12.04默認的),到nginx官方下了對應版本的源 碼,搜索了 fastcgi參數使用的地方,在 httpngx_http_upstream.c 找到了。雖然不能很 流程的讀懂nginx的代碼,但粗略的了解,根據了解的情況加以猜測,再動手測試實驗,也得出了結論,確定了 nginx的fastcgi_cache 的規(guī)則。n gx_http_upstream.c/line 3136 當 fastcgi響應包含 set-cookie 時,不緩存static ngx_int_tn gx_ht
14、tp_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_HTTP_UPSTREAM_IGN_SET_COOKIE) u-cacheable = 0;#en difreturn NGX_OK;/line 3242當響應頭包含Expires時,如果過期時間大于當前服務器時間,則ngi
15、nx_cache會緩存該響應,否則,則不緩存static ngx_int_t*h,n 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_HTTP_CACHE)time_t expires;if (u-co nf-ig no re_headers & NGX_HTTP_UPSTREAM_IGN_EXPIRES) return NGX_OK;if (r-cach
16、e = 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_OK;r-cache-valid_sec = expires;#en difreturn NGX_OK;line 3199當響應頭包含 Cache-Control 時,#如果#這里有如果啊。/【注意】如果Cache-Control參
17、數值為no-cache、no-store、private 中任意一個時,則不緩存不緩存II【注意】如果Cache-Control參數值為 max-age時,會被緩存,且nginx設置的cache 的過期時間,就是系統(tǒng)當前時間+ mag-age的值if (n gx_strlcasestrn(p, last, (u_char *) n o-cache, 8 - 1) != NULL| ngx_strlcasestrn(p, last, (u_char *) n o-store, 8 - 1) != NULL| ngx_strlcasestrn(p, last, (u_char *) private
18、, 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() + n;也就是說,fastcgi 響應http 請求的結果中,響應頭包括Expires、Cache-Control、Set-Cookie三個,都會可能不被cache,但不只有這些,別忘了 nginx配置中fastcgi_ignore_headers參數設定的部分。
19、以及ngxin 的 X-ACCEL X-Accel-Redirect 、X-Accel-Expires 、X-Accel-Charset 、X-Accel-Buffering等 nginx 自定義的響應頭。由于這幾個不常用,我也沒深入研究。通過對nginx的ngx_http_upstream 模塊代碼模糊理解,加猜測,以及寫了腳本測試驗證,可以得到結論是正確的。即Ngi nx fastcgi_cache 在緩存后端fastcgi響應時,當響應里包含“ set-cookie ”時,不緩存;當響應頭包含 Expires時, 如果過期時間大于當前服務器時間,則nginx_cache會緩存該響應,否則
20、,則不緩存;當響應頭包含 Cache-Control 時,如果 Cache-Control 參數值為 no-cache、no-store、private 中任意一個時,則不緩存,如果Cache-Control參數值為max-age時,會被緩存,且 nginx設置的cache的過期時間,就是系統(tǒng)當前時間+ mag-age的值。nginx fastcgi_cache響應 expirednginx fastcgi_cache hit 命中FASTCGI_CACHE $upstream_cache_status 結果為 miss,次也沒命中。/逐個測試,測試時,注釋其他的header(Expires:
21、 .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(Cache-C on trol: private);header(Cache-C on trol: max-age=10);setcookie(cfc4 n,testaaaa);ech
22、o Hello cfc4n ,time();到了這里,疑問1解決了。那么疑問2、3呢?程序里并沒有輸出“ Expires“Cache-Control ” http header的代碼,這是誰輸出的呢 ?既然是fpm響應的時候,就已經有了,那么是php的core模塊,還是其他拓展模塊輸出的?我精簡了代碼,只輸出一個“hello world,發(fā)現也確實被緩存了。顯然, php腳本程序中并沒輸出http header 的“ Expires ”、“ Cache-Control ”,多次測試,最終定位到session_start函數,翻閱源碼找到了這些代碼:/ext/sessio n/sessio n.
23、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);/* */再到這里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
24、 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 clie 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_EN
25、TRY(private)CACHE_LIMITER_ENTRY(private_ no_expire)CACHE_LIMITER_ENTRY( nocache)0;static int php_session_cacheimiter(TSRMLS_D) /* */php_sessi 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);
26、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 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_
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 四年級數學三位數除以兩位數競賽自測模擬題大全附答案
- 100以內加減法豎式計算綜合監(jiān)控例題大全附答案
- 2025年度水資源調配與利用合同
- 2025年度護坡工程地質災害預警系統(tǒng)施工合同
- 2025年度京牌租賃合同附加環(huán)保責任條款
- 2025年度跨境電商公證借款合同范本
- 2025年度豪華SUV購買合同模板
- 2025年二手房交易合同協(xié)議(三篇)
- 2025年健身房與保險公司合作意外傷害保險合同
- 2025年主題道德實踐活動方案與總結標準范文(2篇)
- GA 2139-2024警用防暴臂盾
- DL∕T 5810-2020 電化學儲能電站接入電網設計規(guī)范
- 北京三甲中醫(yī)疼痛科合作方案
- QCT957-2023洗掃車技術規(guī)范
- 新外研版高中英語選擇性必修1單詞正序英漢互譯默寫本
- 自愿斷絕父子關系協(xié)議書電子版
- 2023年4月自考00504藝術概論試題及答案含解析
- 美麗的大自然(教案)2023-2024學年美術一年級下冊
- 成都特色民俗課件
- 花城版音樂四下-第四課-認知音樂節(jié)奏(教案)
- 統(tǒng)編版語文五年級下冊 《古詩三首》公開課一等獎創(chuàng)新教學設計及反思
評論
0/150
提交評論