How do I force Azure CDN content to be purged or invalidated?

会有一股神秘感。 提交于 2019-11-30 19:16:57

In 2015 December the Azure team added the ability to refresh or purge the CDN via REST API (https://msdn.microsoft.com/en-us/library/mt634451.aspx). In the beginning, this function is only available for endpoints created with the new Azure Portal (http://portal.azure.com), however, CDNs created with the old management surface will be migrated in the beginning of 2016 (https://feedback.azure.com/forums/169397-cdn/suggestions/556307-ability-to-force-the-cdn-to-refresh-any-cached-con).

There is NO API to invalidate an Azure CDN.

Workaround:

  1. Enable "Query String Status" on your CDN in Azure portal. Then you can append a new query string name and random value eg. /images/background.png?v=1234

  2. Upload and rename the new file with a timestamp or random value. for example: /images/background.20140917225200.png

  3. Set a shorter cache header and wait for it to expire. Here's an article from the Azure team http://msdn.microsoft.com/en-us/library/azure/gg680306.aspx

You can also purge CDN content via PowerShell:

$AzureCdnResourceGroupName = "--RESOURCE GROUP--"
$AzureCdnEndpoint = "--ENDPOINT NAME--"
$AzureCdnProfileName = "--CDN PROFILE NAME--"

#In case there's multiple subscriptions on the account
Set-AzureRmContext -SubscriptionId $AzureCdnSubscriptionId
$AzureCdnSubscriptionId = "--SUBSCRIPTION GUID--"

Write-Host "Purging CDN $AzureCdnProfileName/$AzureCdnEndpoint"

Invoke-AzureRmResourceAction `
    -ResourceGroupName $AzureCdnResourceGroupName `
    -ResourceType 'Microsoft.Cdn/profiles/endpoints' `
    -ResourceName $AzureCdnProfileName/$AzureCdnEndpoint `
    -ApiVersion '2015-06-01' `
    -Action 'Purge' `
    -Parameters @{ ContentPaths = @('/static/js/*','/static/css/*') } `
    -Force

Write-Host 'Purging completed'

You cannot force CDN purge.

Best practice is to append version/date info to your file name, and design your app to dynamically get the current file name.

For product photos as an example, append the version to the blob name, store the blob name in a table, and then serve a link to the name from the table, instead of hard-coding the filename.

This way you can set max-expiry on the cache headers, and Azure will just clean up stale content when it needs to.

John Wildes

You can use the az cli to purge the cdn endpoint

az cdn endpoint purge is the root command

an example

az cdn endpoint purge -g group -n endpoint \
--profile-name profile-name \
--content-paths '/scripts/app.js' '/styles/*'

You can find more information on the cli command here

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!