Based on Adobe's official documentation, using surrogate keys is the most straightforward and recommended method for purging CDN cache for a specific website in a multi-site AEM as a Cloud Service environment. Unfortunately, there is no simpler built-in option that automatically segments cache by website without configuration.
AEMasCS provides three purge variations:
Single URL purge - Purges one resource at a time
Surrogate key purge - Purges multiple resources grouped by key(s) (recommended for your use case)
Full purge - Purges all cached resources (use with caution)
There is no native "site-based" filtering in the CDN purge API. The CDN cache doesn't automatically know which pages belong to which website in your multi-site setup. Surrogate keys exist specifically to solve this problem by allowing you to tag and group related content for selective purging.
You can add the Surrogate-Key header using Apache mod_headers in your Dispatcher configuration. This is typically the simplest approach:
# In your vhost file or dispatcher configuration
<LocationMatch "^/content/site1/.*\.html$">
Header set Surrogate-Key "site1 html-content"
</LocationMatch>
<LocationMatch "^/content/site2/.*\.html$">
Header set Surrogate-Key "site2"
</LocationMatch>If you need more dynamic control, you can add the Surrogate-Key header programmatically using a Sling Filter:
@8220494(service = Filter.class)
@ServletFilter(scope = SlingServletFilterScope.REQUEST)
public class SurrogateKeyFilter implements Filter {
@9944223
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) {
SlingHttpServletRequest slingRequest = (SlingHttpServletRequest) request;
SlingHttpServletResponse slingResponse = (SlingHttpServletResponse) response;
String path = slingRequest.getResource().getPath();
if (path.startsWith("/content/site1/")) {
slingResponse.setHeader("Surrogate-Key", "site1");
} else if (path.startsWith("/content/site2/")) {
slingResponse.setHeader("Surrogate-Key", "site2");
}
chain.doFilter(request, response);
}
}Before using the Purge API, you need to Configure Purge API Token in your AEM project's cdn.yaml file and Deploy the configuration via Cloud Manager Config Pipeline (of course you've to store the purge key as a Cloud Manager secret-type environment variable). More info here.
Btw, ensure the dispatcher cache is also invalidated before CDN purge, as the CDN will fetch from Dispatcher when cache expires