使用 Azure Front Door Service 進行快取Caching with Azure Front Door Service

下列文件會利用已啟用快取的路由規則來指定 Front Door 的行為。The following document specifies behavior for Front Door with routing rules that have enabled caching.

傳遞大型檔案Delivery of large files

Azure Front Door Service 可傳遞大型檔案且檔案大小沒有上限。Azure Front Door Service delivers large files without a cap on file size. Front Door 會使用稱為物件區塊化的技術。Front Door uses a technique called object chunking. 要求大型檔案時,Front Door 會從後端擷取較小的檔案片段。When a large file is requested, Front Door retrieves smaller pieces of the file from the backend. 接收到完整或位元組範圍檔案要求之後,Front Door 環境就會向後端要求檔案 (以 8 MB 的區塊為單位)。After receiving a full or byte-range file request, a Front Door environment requests the file from the backend in chunks of 8 MB.

當區塊抵達 Front Door 環境之後,就會被快取並立即提供給使用者。After the chunk arrives at the Front Door environment, it is cached and immediately served to the user. 然後 Front Door 會以平行方式預先提取下一個區塊。Front Door then pre-fetches the next chunk in parallel. 此預先擷取可確保內容會領先使用者一個區塊,以降低延遲。This pre-fetch ensures that the content stays one chunk ahead of the user, which reduces latency. 此程序一直持續到整個檔案下載完畢 (如有要求)、取得所有位元組範圍 (如有要求),或用戶端終止連線。This process continues until the entire file is downloaded (if requested), all byte ranges are available (if requested), or the client terminates the connection.

如需位元組範圍要求的詳細資訊,請參閱 [RFC 7233](https://web.archive.org/web/20171009165003/ http://www.rfc-base.org/rfc-7233.html)。For more information on the byte-range request, read RFC 7233. Front Door 會在接收到任何區塊之後進行快取,因此不需要在 Front Door 快取上快取整個檔案。Front Door caches any chunks as they're received and so the entire file doesn't need to be cached on the Front Door cache. 快取會提供後續的檔案或位元組範圍要求。Subsequent requests for the file or byte ranges are served from the cache. 如果不會快取所有區塊,就會使用預先擷取來向後端要求區塊。If not all the chunks are cached, pre-fetching is used to request chunks from the backend. 此最佳化依賴後端的功能來支援位元組範圍要求;如果後端不支援位元組範圍要求,則此最佳化無效。This optimization relies on the ability of the backend to support byte-range requests; if the backend doesn't support byte-range requests, this optimization isn't effective.

檔案壓縮File compression

Front Door 可在邊緣動態壓縮內容,因而對用戶端產生較小且更快速的回應。Front Door can dynamically compress content on the edge, resulting in a smaller and faster response to your clients. 所有檔案都符合壓縮資格。All files are eligible for compression. 但是,檔案必須是符合壓縮清單資格的 MIME 類型。However, a file must be of a MIME type that eligible for compression list. 目前,Front Door 不允許變更此清單。Currently, Front Door does not allow this list to be changed. 目前的清單為:The current list is:

  • "application/eot""application/eot"
  • "application/font""application/font"
  • "application/font-sfnt""application/font-sfnt"
  • "application/javascript""application/javascript"
  • "application/json""application/json"
  • "application/opentype""application/opentype"
  • "application/otf""application/otf"
  • "application/pkcs7-mime""application/pkcs7-mime"
  • "application/truetype""application/truetype"
  • "application/ttf","application/ttf",
  • "application/vnd.ms-fontobject""application/vnd.ms-fontobject"
  • "application/xhtml+xml""application/xhtml+xml"
  • "application/xml""application/xml"
  • "application/xml+rss""application/xml+rss"
  • "application/x-font-opentype""application/x-font-opentype"
  • "application/x-font-truetype""application/x-font-truetype"
  • "application/x-font-ttf""application/x-font-ttf"
  • "application/x-httpd-cgi""application/x-httpd-cgi"
  • "application/x-mpegurl""application/x-mpegurl"
  • "application/x-opentype""application/x-opentype"
  • "application/x-otf""application/x-otf"
  • "application/x-perl""application/x-perl"
  • "application/x-ttf""application/x-ttf"
  • "application/x-javascript""application/x-javascript"
  • "font/eot""font/eot"
  • "font/ttf""font/ttf"
  • "font/otf""font/otf"
  • "font/opentype""font/opentype"
  • "image/svg+xml""image/svg+xml"
  • "text/css""text/css"
  • "text/csv""text/csv"
  • "text/html""text/html"
  • "text/javascript""text/javascript"
  • "text/js"、"text/plain""text/js", "text/plain"
  • "text/richtext""text/richtext"
  • "text/tab-separated-values""text/tab-separated-values"
  • "text/xml""text/xml"
  • "text/x-script""text/x-script"
  • "text/x-component""text/x-component"
  • "text/x-java-source""text/x-java-source"

此外,檔案大小也必須介於 1 KB 到 8 MB (含) 之間。Additionally, the file must also be between 1 KB and 8 MB in size, inclusive.

這些設定檔支援下列壓縮編碼:These profiles support the following compression encodings:

如果要求支援 gzip 和 Brotli 壓縮,Brotli 壓縮的優先順序最高。If a request supports gzip and Brotli compression, Brotli compression takes precedence.
當資產的要求指定壓縮且快取中的要求結果遺失時,Front Door 會在 POP 伺服器上直接對資產執行壓縮。When a request for an asset specifies compression and the request results in a cache miss, Front Door performs compression of the asset directly on the POP server. 之後會從快取提供壓縮的檔案。Afterward, the compressed file is served from the cache. 產生的項目會使用 transfer-encoding: chunked 來傳回。The resulting item is returned with a transfer-encoding: chunked.

查詢字串行為Query string behavior

使用 Front Door,可以控制 Web 要求內含查詢字串時的檔案快取方式。With Front Door, you can control how files are cached for a web request that contains a query string. 在包含查詢字串的 Web 要求中,查詢字串是要求中問號 (?) 之後的部分。In a web request with a query string, the query string is that portion of the request that occurs after a question mark (?). 查詢字串可以包含一或多個索引鍵/值組,其中的欄位名稱與其值是以等號 (=) 分隔。A query string can contain one or more key-value pairs, in which the field name and its value are separated by an equals sign (=). 每個索引鍵/值組是以 & 符號分隔。Each key-value pair is separated by an ampersand (&). 例如: http://www.contoso.com/content.mov?field1=value1&field2=value2For example, http://www.contoso.com/content.mov?field1=value1&field2=value2. 如果要求的查詢字串中有不止一個索引鍵/值組,其順序無關緊要。If there is more than one key-value pair in a query string of a request, their order does not matter.

  • 忽略查詢字串:預設模式。Ignore query strings: Default mode. 在此模式中,Front Door 會將要求者發出的查詢字串,傳遞至第一個要求的後端並快取資產。In this mode, Front Door passes the query strings from the requestor to the backend on the first request and caches the asset. 所有後續對該資產提出並由 Front Door 環境提供服務的要求都會忽略查詢字串,直到所快取的資產到期為止。All subsequent requests for the asset that are served from the Front Door environment ignore the query strings until the cached asset expires.

  • 快取所有不重複的 URL:在此模式下,每個具有唯一 URL (包含查詢字串) 的要求都會被視為具有專屬快取的唯一資產。Cache every unique URL: In this mode, each request with a unique URL, including the query string, is treated as a unique asset with its own cache. 例如,系統會將後端對 www.example.ashx?q=test1 要求做出的回應快取於 Front Door 環境中,然後針對後續具有相同查詢字串的快取傳回此回應。For example, the response from the backend for a request for www.example.ashx?q=test1 is cached at the Front Door environment and returned for subsequent caches with the same query string. 系統快取針對 www.example.ashx?q=test2 的要求,會將其視為具有專屬存留時間設定的個別資產。A request for www.example.ashx?q=test2 is cached as a separate asset with its own time-to-live setting.

快取清除Cache purge

Front Door 將會快取資產,直到資產的存留時間 (TTL) 到期。Front Door will cache assets until the asset's time-to-live (TTL) expires. 資產的 TTL 到期之後,當用戶端要求資產時,Front Door 環境將會擷取資產新的更新複本以服務用戶端的要求並儲存重新整理快取。After the asset's TTL expires, when a client requests the asset, the Front Door environment will retrieve a new updated copy of the asset to serve the client request and store refresh the cache.
若要確定使用者一律會取得最新的資產複本,最佳作法是為每個更新設定資產版本,然後將它們發佈為新的 URL。The best practice to make sure your users always obtain the latest copy of your assets is to version your assets for each update and publish them as new URLs. Front Door 將立即為下一個用戶端要求擷取新的資產。Front Door will immediately retrieve the new assets for the next client requests. 有時您可能想要清除所有邊緣節點的快取內容,並強制它們全部擷取新的更新的資產。Sometimes you may wish to purge cached content from all edge nodes and force them all to retrieve new updated assets. 可能是因為您的 Web 應用程式更新,或快速更新包含不正確資訊的資產。This might be due to updates to your web application, or to quickly update assets that contain incorrect information.

選取您希望從邊緣節點清除的資產。Select what assets you wish to purge from the edge nodes. 如果您希望清除所有資產,請按一下 [全部清除] 核取方塊。If you wish to clear all assets, click the Purge all checkbox. 或者,在 [路徑] 文字方塊中輸入每個您想要清除之資產的路徑。Otherwise, type the path of each asset you wish to purge in the Path textbox. 路徑支援下列格式。Below formats are supported in the path.

  1. 單一 URL 清除:透過指定完整的 URL (含副檔名) 來清除個別資產,例如 /pictures/strasbourg.png;Single URL purge: Purge individual asset by specifying the full URL, with the file extension, for example, /pictures/strasbourg.png;
  2. 萬用字元清除:星號 (*) 可作為萬用字元。Wildcard purge: Asterisk (*) may be used as a wildcard. 清除路徑中有 /* 之端點下的所有資料夾、子資料夾與檔案,或指定後接 /* 的資料夾來清除特定資料夾下的所有子資料夾與檔案,例如 /pictures/*。Purge all folders, subfolders and files under an endpoint with /* in the path or purge all subfolders and files under a specific folder by specifying the folder followed by /*, for example, /pictures/*.
  3. 根網域清除:清除路徑中有 "/" 之端點的根目錄。Root domain purge: Purge the root of the endpoint with "/" in the path.

Front Door 上的快取清除是不區分大小寫的。Cache purges on the Front Door are case-insensitive. 此外,它們是無從驗證查詢字串的,這表示清除 URL 將會清除它的所有查詢字串變數。Additionally, they are query string agnostic, meaning purging a URL will purge all query-string variations of it.

快取到期Cache expiration

下列標頭的順序可用來判斷項目將在我們的快取中儲存的時間:The following order of headers is used in order to determine how long an item will be stored in our cache:

  1. Cache-Control: s-maxage=<seconds>Cache-Control: s-maxage=<seconds>
  2. Cache-Control: max-age=<seconds>Cache-Control: max-age=<seconds>
  3. 到期: <http 日期 >Expires: <http-date>

系統會接受指出將不會快取回應的 Cache-Control 回應標頭 (例如,Cache-Control: private、Cache-Control: no-cache 與 Cache-Control: no-store)。Cache-Control response headers that indicate that the response won’t be cached such as Cache-Control: private, Cache-Control: no-cache, and Cache-Control: no-store are honored. 不過,如果 POP 中有多個針對相同 URL 的要求,它們可能共用回應。However, if there are multiple requests in-flight at a POP for the same URL, they may share the response. 若沒有快取控制項,則預設行為是時間的 AFD 會快取的資源量 X X 隨機挑選介於 1 到 3 天之間。If no Cache-Control is present the default behavior is that AFD will cache the resource for X amount of time where X is randomly picked between 1 to 3 days.

要求標頭Request headers

使用快取時,不會將下列要求標頭轉送至後端。The following request headers will not be forwarded to a backend when using caching.

  • 授權Authorization
  • Content-LengthContent-Length
  • Transfer-EncodingTransfer-Encoding

後續步驟Next steps