Ben D Ben D - 1 year ago 83
Apache Configuration Question

Cache Headers on PHP file not working

I have an application where a number of otherwise static javascript files are being generated via PHP to allow configuration options to alter the static files (path like:

). Everything works fine EXCEPT that I can't seem to get the cache settings to work and these resources are being reloaded with every page load.

The PHP file uses the following headers to try to set the cache settings:

$expires= 60 * 60 * 24 * 60; //cache for 60 days
header('Pragma: public');
header('Cache-Control: max-age=' . $expires);
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $expires) . ' GMT');
header("content-type: application/x-javascript");

However, when the files are served they're showing headers that look like:

HTTP/1.1 200 OK
Date: Sun, 06 Nov 2016 19:18:00 GMT
Server: Apache/2.2.15 (CentOS)
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 744
Keep-Alive: timeout=15, max=95
Connection: Keep-Alive
Content-Type: application/x-javascript

My first thought was that it was that it might be because Apache has the
flag set on but I don't see any
rules set for PHP files.

Reading online it sounds like
issues could be the problem, but I've added

Header unset Pragma
FileETag None
Header unset ETag

to the http.conf file (and restarted the service) and still no dice.

Any thoughts?

Answer Source

Source: PHP: Worry about some magical added “Cache-Control” Header ?

These headers are automatically set by the PHP Session module to prevent browser/proxy based caching of your pages. Depending on your environment setup, it’s possible to control these headers by using the session_cache_limiter() function or use the php.ini

To disable these behaviour just pass an empty string to the session_cache_limiter() function as mentioned in the documentation:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download