Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overwrite cache-headers with mod_expires

I want to set cache-headers using the mod_expires module from apache. My configuration looks somewhat like this:

<LocationMatch ^/static >
    ExpiresDefault "access plus 1 years"
</LocationMatch>

The problem is, that the files are generated by a third system I don't control. These system provides files with the following headers:

Date Mon, 24 Oct 2011 08:39:02 GMT
Cache-Control no-cache,no-store,must-revalidate
Pragma no-cache
Expires Thu, 01 Dec 1994 16:00:00 GMT

These headers makes it impossible to set the cache-headers with mod_expires. http://httpd.apache.org/docs/2.2/mod/mod_expires.html tells us why:

When the Expires header is already part of the response generated by the server, for example when generated by a CGI script or proxied from an origin server, this module does not change or add an Expires or Cache-Control header.

Is there any possible way to circumvent this rule and overwrite the headers with mod_expires?

Update: One possible solution, to avoid this limitation is to use only mod_headers to set the cache-headers. Unfortunately, this isn't an alternative because the values have to be calculated.

Thanks it advance.

like image 221
scheffield Avatar asked Oct 28 '11 13:10

scheffield


2 Answers

Unfortunately, it's a known limitation and we had to fall back to use only mod_headers.

like image 179
scheffield Avatar answered Oct 29 '22 22:10

scheffield


Regilero's suggestion won't work because header directives will be processed very late in the response processing - after mod_expire directive. So you'd unset the headers after mod_expires did (or didn't) what it was supposed to do.

If it's apache 2.2 you could try putting early at the end of each header directive. That will tell it to do this in an early stage of response processing as opposed to at the end.

so try:

<LocationMatch ^/static >
  Header unset Cache-Control early
  Header unset Pragma early
  Header unset Expires early
  ExpiresDefault "access plus 1 years"
</LocationMatch>

Haven't tested tho, but give it a try...

like image 1
intelekt Avatar answered Oct 29 '22 22:10

intelekt