如何啟用CDN快取
瞭解如何在AEM as a Cloud Service的CDN中啟用HTTP回應的快取。 回應快取是由Cache-Control
、Surrogate-Control
或Expires
HTTP回應快取標頭所控制。
這些快取標頭通常在使用mod_headers
的AEM Dispatcher vhost設定中設定,但也可以在AEM Publish本身中執行的自訂Java™程式碼中設定。
預設快取行為
當自訂設定不存在時,將使用預設值。 在以下熒幕擷圖中,您可以看到部署AEM Project Archetype型mynewsite
AEM專案時AEM Publish和Author的預設快取行為。
檢閱AEM Publish — 預設快取存留期和AEM Author — 預設快取存留期以取得詳細資訊。
總而言之,AEM as a Cloud Service會在AEM Publish中快取大部分的內容型別(HTML、JSON、JS、CSS和Assets),並在AEM Author中快取少數內容型別(JS、CSS)。
啟用快取
若要變更預設快取行為,您可以透過兩種方式更新快取標頭。
- 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 web browser and CDN to cache the response for 'max-age' value (XXX) seconds. The 'stale-while-revalidate' and 'stale-if-error' attributes controls the stale state treatment at CDN layer.
Header set Cache-Control "max-age=XXX,stale-while-revalidate=XXX,stale-if-error=XXX"
# Instructs the CDN to cache the response for 'max-age' value (XXX) seconds. The 'stale-while-revalidate' and 'stale-if-error' attributes controls the stale state treatment at CDN layer.
Header set Surrogate-Control "max-age=XXX,stale-while-revalidate=XXX,stale-if-error=XXX"
# Instructs the web browser and CDN to cache the response until the specified date and time.
Header set Expires "Sun, 31 Dec 2023 23:59:59 GMT"
</LocationMatch>
以下摘要說明每個 標頭 的用途以及適用於標頭的 屬性。
- max-age:此屬性會控制回應內容的TTL或「存留時間」(以秒為單位)。
- stale-while-revalidate:當收到的要求位於指定的期間(以秒為單位)內時,此屬性會控制CDN層回應內容的 過時狀態 處理。 過時狀態 是TTL過期之後與重新驗證回應之前的時間段。
- stale-if-error:當原始伺服器無法使用,且收到的請求在指定的期間(以秒為單位)內時,此屬性會控制CDN層回應內容的 過時狀態 處理。
如需詳細資訊,請檢閱失效與重新驗證詳細資料。
範例
若要將 HTML內容型別 的網頁瀏覽器和CDN快取存留期增加到 10分鐘,而不處理過時的狀態,請遵循下列步驟:
-
在您的AEM專案中,從
dispatcher/src/conf.d/available_vhosts
目錄找出所需的vhsot檔案。 -
更新vhost (例如
wknd.vhost
)檔案,如下所示:code language-none <LocationMatch "^/content/.*\.(html)$"> # Removes the response header if present Header unset Cache-Control # Instructs the web browser and CDN to cache the response for max-age value (600) seconds. Header set Cache-Control "max-age=600" </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環境。
不過,若要讓網頁瀏覽器和CDN快取存留期的值不同,您可以使用上述範例中的Surrogate-Control
標頭。 同樣地,若要在特定日期和時間讓快取到期,您可以使用Expires
標頭。 此外,使用stale-while-revalidate
和stale-if-error
屬性,您可以控制回應內容的過時狀態處理。 AEM WKND專案有參考過時狀態處理 CDN快取設定。
同樣地,您也可以更新其他內容型別(JSON、JS、CSS和Assets)的快取標頭。
自訂Java™程式碼
此選項同時適用於AEM Publish和Author。 但是,不建議在AEM Author中啟用快取並保留預設快取行為。
若要更新快取標頭,請在自訂Java™程式碼(Sling servlet、Sling servlet篩選器)中使用HttpServletResponse
物件。 一般語法如下:
// Instructs the web browser and CDN to cache the response for 'max-age' value (XXX) seconds. The 'stale-while-revalidate' and 'stale-if-error' attributes controls the stale state treatment at CDN layer.
response.setHeader("Cache-Control", "max-age=XXX,stale-while-revalidate=XXX,stale-if-error=XXX");
// Instructs the CDN to cache the response for 'max-age' value (XXX) seconds. The 'stale-while-revalidate' and 'stale-if-error' attributes controls the stale state treatment at CDN layer.
response.setHeader("Surrogate-Control", "max-age=XXX,stale-while-revalidate=XXX,stale-if-error=XXX");
// Instructs the web browser and CDN to cache the response until the specified date and time.
response.setHeader("Expires", "Sun, 31 Dec 2023 23:59:59 GMT");