Expand my Community achievements bar.

SOLVED

Issue with Cache Control for Download Assets in AEM

Avatar

Level 2

Background

In my project, I am working on caching control for downloadable assets in AEM. I have written a cache rule to handle assets with URLs following these patterns:

 

Example URL for myprojectdownload assets:

https://dev-publish.myproject.com/content/dam/myproject/path/subsi/public/en/downloadsmajor-resoluti...

 

Example URL for coredownload assets:

 

https://dev-publish.myproject.com/content/dam/myproject/path/subsi/public/en/downloadsmajor-resoluti...

 

Implemented Cache Rule

To ensure these assets are cached correctly, I have added the following LocationMatch rule in the Apache configuration:

 

<LocationMatch "^/content/dam/.*\.(coredownload|myprojectdownload)\.(?i:pdf).*">
Header set Cache-Control "max-age=95400, stale-while-revalidate=1800, stale-if-error=43200, public, immutable" "expr=%{REQUEST_STATUS} < 400"
Header set Age 0
</LocationMatch>

 

Issue Encountered

Despite implementing the above cache rule, the cache control headers are missing from the response. Below is the current response header

 

 

accept-ranges: bytes
age: 0
content-disposition: attachment; filename="112 .pdf"
content-encoding: gzip
content-type: application/pdf
permissions-policy: private-state-token-redemption=()
permissions-policy: private-state-token-issuance=()
permissions-policy: run-ad-auction=()
permissions-policy: join-ad-interest-group=()
vary: Accept-Encoding
x-cache: MISS
x-content-type-options: nosniff
x-frame-options: ORIGIN
x-served-by: cache-BOM

 

Expected Behavior

I expect the response to include the Cache-Control header with the specified directives so that these assets can be cached properly by browsers and CDNs.

 

Questions

  • Why is the Cache-Control header missing, despite being explicitly set in the configuration?
  • How can I ensure that cache control settings are correctly applied to these assets?

    Any insights or recommendations would be greatly appreciated!

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @RahulSi13 ,

Below are the answers to your queries.

1. Why is the Cache-Control header missing, despite being explicitly set in the configuration?

  • Incorrect Regular Expression in LocationMatch. The regex is matching filenames before .pdf, but URLs have additional encoded characters, so modify the regex as below:
<LocationMatch "^/content/dam/.*\.(coredownload|myprojectdownload)\.pdf$">
  • The Header set directive requires mod_headers ensure it should be enabled.
// check if it is enabled
apachectl -M | grep headers

//If not enabled, Activate it
a2enmod headers
systemctl restart apache2
  • Check if another rule in Apache, Dispatcher, or a CDN (Akamai, Cloudflare) is stripping headers. ensure Header unset Cache-Control should NOT present.
grep -Ri "Header unset Cache-Control" /etc/apache2/
  • Ensure dispatcher.any contains below configurations
/clientheaders
  {
  "Cache-Control"
  }

2. How can I ensure that cache control settings are correctly applied to these assets?

  • Use curl to check the response:
// Curl Command
curl -I https://dev-publish.myproject.com/content/dam/myproject/path/subsi/public/en/downloadsmajor-resolutions-of-board-meetings/112.pdf.coredownload.pdf

//Expected Output
Cache-Control: max-age=95400, stale-while-revalidate=1800, stale-if-error=43200, public, immutable
  • Bypass CDN/Dispatcher to ensure headers are being set at Apache level as below curls, If headers appear, the issue is likely Dispatcher/CDN stripping them
curl -I -H "Pragma: no-cache" https://your-server-ip/content/dam/...
  • Ensure AEM allows custom headers by unchecking "Enable For All Paths" in Apache Sling Content Disposition Filter under AEM OSGi Configuration (/system/console/configMgr).

Regards,

Shiv Prakash

View solution in original post

3 Replies

Avatar

Level 2

URL are :

 

dev-publish.myproject.com/content/dam/myproject/path/subsi/public/en/downloadsmajor-resolutions-of-board-meetings/112%A0%85.pdf.coredownload.pdf/%85_112%E5%B9%B4

 

dev1-publish.myproject.com/content/dam/myproject/path/subsi/public/en/downloadsmajor-resolutions-of-board-meetings/112%A0%85.pdf.myprojectdownload.pdf/%85_112%E5%B9%B4

Avatar

Level 2

Avatar

Correct answer by
Community Advisor

Hi @RahulSi13 ,

Below are the answers to your queries.

1. Why is the Cache-Control header missing, despite being explicitly set in the configuration?

  • Incorrect Regular Expression in LocationMatch. The regex is matching filenames before .pdf, but URLs have additional encoded characters, so modify the regex as below:
<LocationMatch "^/content/dam/.*\.(coredownload|myprojectdownload)\.pdf$">
  • The Header set directive requires mod_headers ensure it should be enabled.
// check if it is enabled
apachectl -M | grep headers

//If not enabled, Activate it
a2enmod headers
systemctl restart apache2
  • Check if another rule in Apache, Dispatcher, or a CDN (Akamai, Cloudflare) is stripping headers. ensure Header unset Cache-Control should NOT present.
grep -Ri "Header unset Cache-Control" /etc/apache2/
  • Ensure dispatcher.any contains below configurations
/clientheaders
  {
  "Cache-Control"
  }

2. How can I ensure that cache control settings are correctly applied to these assets?

  • Use curl to check the response:
// Curl Command
curl -I https://dev-publish.myproject.com/content/dam/myproject/path/subsi/public/en/downloadsmajor-resolutions-of-board-meetings/112.pdf.coredownload.pdf

//Expected Output
Cache-Control: max-age=95400, stale-while-revalidate=1800, stale-if-error=43200, public, immutable
  • Bypass CDN/Dispatcher to ensure headers are being set at Apache level as below curls, If headers appear, the issue is likely Dispatcher/CDN stripping them
curl -I -H "Pragma: no-cache" https://your-server-ip/content/dam/...
  • Ensure AEM allows custom headers by unchecking "Enable For All Paths" in Apache Sling Content Disposition Filter under AEM OSGi Configuration (/system/console/configMgr).

Regards,

Shiv Prakash