【調教WordPress】套Cloudflare後如何避免快取admin bar

比如說Wordpress,在登入的情況下,一般會在網站頂部顯示一條admin bar。如果登入的用家瀏覽網頁時觸發了Cloudflare的快取機制,那它就會把帶有admin bar的頁面快取進進去,使下一位瀏覽者也看到這條admin bar,儘管他們無法真的登入,對網站安全造成影響。

作為世界上最大的CDN服務商,Cloudflare很強大也很佛心,對於一個小小的blog,免費版就夠用,但有時設定不好,也可能出問題。很多人會教你,要盡用Cloudflare的cache功能,為你的網站提速,那就必須在Cloudflare設立一條cache everything的page rule,但是他們沒有告訴你,cache everything非常進取,有些不改cache的內容也可能cache到Cloudflare上,然後呈現給網站的所有訪問者。

比如說Wordpress,在登入的情況下,一般會在網站頂部顯示一條admin bar。如果登入的用家瀏覽網頁時觸發了Cloudflare的快取機制,那它就會把帶有admin bar的頁面快取進進去,使下一位瀏覽者也看到這條admin bar,儘管他們無法真的登入,對網站安全造成影響。

一,在Wordpress關閉admin bar

既然不想admin bar被Cloudflare快取,那麼最直接的方法不就是關掉admin bar嗎?Wordpress確實提供了關閉選項,你需要進入 Users 選單,再進入每一位user的編輯頁面,將「Show Toolbar when viewing site」前面的勾勾點除。當然,admin bar提供了一定的便利,在瀏覽頁面時可以通過admin bar作出一些操作,而無需打開管理頁面,所以有些人可能不希望停用admin bar,那麼下面還有其他的方法。

二,在Cloudflare設置Bypass Cache on Cookie

Cloudflare也想到了用家希望避免某些頁面被快取,因此提供了一個方法,就是設定一條基於cookies去判斷是否需要快取的page rule,這條規則可以直接加到cache everything的那條page rule裏面,後面填上:

wordpress_logged_in_|wp-|comment_|woocommerce_|wordpressuser_|wordpresspass_*|wordpress_sec_

因為Wordpress的登入user都會產生這些cookies,Cloudflare就能夠判斷瀏覽者是否為登入user,再決定是否對他的瀏覽頁面作出快取。

cloudflare page rules bypass on cookie

不過Bypass Cache on Cookie是收費功能,只有Cloudflare的商業用家(Business Subscriber) 和企業用家(Enterprise Subscriber)才能用到。如果你像我一樣,不想為了自己的一個小破站而購買Cloudflare付費功能,那麼請繼續瀏覽下面的方法。

也有人提出過可以設立一條指定不快取/wp-admin的page rule,但我十分懷疑這條規則是否能避免快取admin bar,它應該只是不快取admin管理頁面,而admin bar是在前端顯示的。

三,用Super Page Cache for Cloudflare Plugin

非Cloudflare官方出品的Super Page Cache for Cloudflare,是一款很不錯的Wordpress plugin,雖然有點複雜,但提供了許多的定製功能,使得Cloudflare能更好地配合Wordpress,其中一個功能就是針對登入user不作頁面快取。

它過去的做法是,登入user在瀏覽頁面時會自動在連結後面加上「?swcfpc=1」,只要後面有這串字的,Cloudflare就會判斷為不作快取。在Cloudflare加入了cache rules功能後,等於將Bypass Cache on Cookie免費了,而SPCFC也提供了新的方法。

四,在Cloudflare設立cache rules

cloudflare-cache-rules

進入Cloudflare的cache rules頁面,設立一條「SPCFC Cache Rule ➜ Cache Eligible Requests」(名字可以隨便取),點擊「Edit expession」,複製以下code加進去即可(example.com須修改為自己的domain):

(http.host eq “example.com” and not starts_with(http.request.uri.path, “/wp-admin”) and not starts_with(http.request.uri.path, “/wp-login”) and not starts_with(http.request.uri.path, “/wp-json/”) and not starts_with(http.request.uri.path, “/wc-api/”) and not starts_with(http.request.uri.path, “/edd-api/”) and not starts_with(http.request.uri.path, “/mepr/”) and not http.request.uri.path contains “/register/” and not http.request.uri.path contains “/dashboard/” and not http.request.uri.path contains “/members-area/” and not http.request.uri.path contains “/wishlist-member/” and not http.request.uri.path contains “phs_downloads-mbr” and not http.request.uri.path contains “/checkout/” and not http.request.uri.path contains “.xsl” and not http.request.uri.path contains “.xml” and not http.request.uri.path contains “.php” and not starts_with(http.request.uri.query, “s=”) and not starts_with(http.request.uri.query, “p=”) and not http.request.uri.query contains “nocache” and not http.request.uri.query contains “nowprocket” and not http.cookie contains “wordpress_logged_in_” and http.cookie ne “comment_” and not http.cookie contains “woocommerce_” and not http.cookie contains “wordpressuser_” and not http.cookie contains “wordpresspass_” and not http.cookie contains “wordpress_sec_” and not http.cookie contains “yith_wcwl_products” and not http.cookie contains “edd_items_in_cart” and not http.cookie contains “it_exchange_session_” and not http.cookie contains “comment_author” and not http.cookie contains “dshack_level” and not http.cookie contains “auth_” and not http.cookie contains “noaffiliate_” and not http.cookie contains “mp_session” and not http.cookie contains “xf_” and not http.cookie contains “mp_globalcart_” and not http.cookie contains “wp-resetpass-” and not http.cookie contains “upsell_customer” and not http.cookie contains “wlmapi” and not http.cookie contains “wishlist_reg”)

compressedcloudflare cache rules 2 【調教Wordpress】套Cloudflare後如何避免快取admin bar

之後可點擊「Use expression builder」,預覽這些規則或作出修改,下面的「Cache eligibility」點選「Eligible for cache」,最後點擊右下角的「Deploy」就可以應用這些快取規則。然後回到SPCFC的設定頁面,在「other」選項頁裏找到「Remove Cache Buster Query Parameter」,點擊「Yes」打開這個功能,從此Cloudflare就不會再快取不該快取的東西,SPCFC也不會在連結後面加上尾巴。事實上,如果你不需要Super Page Cache for Cloudflare plugin,也可以將它停用,上面的cache rule並不需要依賴它來運作。

Cloudflare已經預告了將會停用 page rules 功能,取而代之的正是 cache rules ,而免費版用家可設立10條 cache rules ,比現在的 page rules 的三條限額要多。

詳細教學請參考:Implementation Guide for using “Remove Cache Buster Query Parameter” feature

陳牛
陳牛

陳牛,曾先後於《明報》、《端傳媒》、《香港01》任職,為《號外》、《就係香港》等媒體擔任特約記者、撰稿人。

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
Shares
0
Would love your thoughts, please comment.x
()
x