如何停用CDN快取
- 適用對象:
- Experience Manager as a Cloud Service
- 主題:
- CDN 快取
建立對象:
- 初學者
- 管理員
- 開發人員
瞭解如何在AEM as a Cloud Service的CDN中停用HTTP回應的快取。 回應快取是由Cache-Control
、Surrogate-Control
或Expires
HTTP回應快取標頭所控制。
這些快取標頭通常在使用mod_headers
的AEM Dispatcher vhost設定中設定,但也可以在AEM Publish本身中執行的自訂Java™程式碼中設定。
預設快取行為
AEM as a Cloud Service CDN中HTTP回應的快取是由以下來自來源Cache-Control
、Surrogate-Control
或Expires
的HTTP回應標頭所控制。 不快取Cache-Control
中包含private
、no-cache
或no-store
的原始回應。
部署以AEM專案原型為基礎的AEM專案時,檢閱AEM Publish和Author的預設快取行為。
停用快取
關閉快取可能會對AEM as a Cloud Service執行個體的效能產生負面影響,因此在關閉預設快取行為時請務必謹慎。
不過,在某些情況下,您可能會想要停用快取,例如:
- 正在開發新功能,並且想要立即看到變更。
- 內容安全(僅供驗證的使用者使用)或動態(購物車、訂單詳細資料)且不應快取。
若要停用快取,您可以使用兩種方式更新快取標頭。
- Dispatcher vhost設定: 僅適用於AEM Publish。
- 自訂Java™程式碼: 可同時用於AEM Publish和Author。
讓我們檢閱這些選項中的每一個。
Dispatcher vhost設定
此選項是停用快取的建議方法,但此選項僅適用於AEM Publish。 若要更新快取標頭,請使用Apache HTTP伺服器的vhost檔案中的mod_headers
模組和<LocationMatch>
指示詞。 一般語法如下:
<LocationMatch "$URL$ || $URL_REGEX$">
# Removes the response header of this name, if it exists. If there are multiple headers of the same name, all will be removed.
Header unset Cache-Control
Header unset Surrogate-Control
Header unset Expires
# Instructs the Browser and the CDN to not cache the response.
Header always set Cache-Control "private"
# Instructs only the CDN to not cache the response.
Header always set Surrogate-Control "private"
</LocationMatch>
範例
若要停用 CSS內容型別 的CDN快取,以進行部分疑難排解,請按照下列步驟進行。
請注意,若要略過現有的CSS快取,必須變更CSS檔案,才能為CSS檔案產生新的快取索引鍵。
-
在您的AEM專案中,從
dispatcher/src/conf.d/available_vhosts
目錄找出所需的vhsot檔案。 -
更新vhost (例如
wknd.vhost
)檔案,如下所示:<LocationMatch "^/etc.clientlibs/.*\.(css)$"> # Removes the response header of this name, if it exists. If there are multiple headers of the same name, all will be removed. Header unset Cache-Control Header unset Expires # Instructs the Browser and the CDN to not cache the response. Header always set Cache-Control "private" </LocationMatch>
dispatcher/src/conf.d/enabled_vhosts
目錄中的vhost檔案是dispatcher/src/conf.d/available_vhosts
目錄中檔案的 symlink,因此請務必建立symlink (若不存在)。 -
使用Cloud Manager - Web層設定管道或RDE命令,將vhost變更部署到所需的AEM as a Cloud Service環境。
自訂Java™程式碼
此選項同時適用於AEM Publish和Author。 若要更新快取標頭,請在自訂Java™程式碼(Sling servlet、Sling servlet篩選器)中使用SlingHttpServletResponse
物件。 一般語法如下:
response.setHeader("Cache-Control", "private");