0

I'm trying to get Apache to cache the results from running an expensive PHP script (it accesses a third party server which has rate limiting) but it seems if the client sends an If-Modified-Since header the cache is bypassed.

CacheEnable disk /script.php
CacheLock on
CacheLockMaxAge 60
CacheIgnoreHeaders Set-Cookie
# prevent client explicitly requesting un-cached content
CacheIgnoreCacheControl on
CacheDetailHeader on

The php script sets the following headers in its response:

  • Last-Modified set to the current time, e.g. Sun, 20 Jun 2021 11:30:20 GMT
  • Expires set to the current time plus 60 seconds, e.g. Sun, 20 Jun 2021 11:31:20 GMT

If I issue the following request:

GET /script.php HTTP/1.1
Host: example.com
Connection: close

Then the caching works correctly and the php script is only run once a minute with other requests being served from the cache.

However issuing the following request:

GET /script.php HTTP/1.1
Host: example.com
Connection: close
If-Modified-Since: Sun, 20 Jun 2021 11:30:30 GMT

Runs the php script every time with:

X-Cache-Detail: "cache miss: attempting entity save" from example.com

in the response.

I'm using Apache 2.4.29 on Ubuntu 18.04.

1 Answers1

0

Mod-Cache Manual from Apache.org

https://httpd.apache.org/docs/2.4/mod/mod_cache.html

Using Custom Logs to identify the problem

CustomLog "cached-requests.log" common env=cache-hit
CustomLog "uncached-requests.log" common env=cache-miss
CustomLog "revalidated-requests.log" common env=cache-revalidate
CustomLog "invalidated-requests.log" common env=cache-invalidate

Enable the X-Cache-Detail header

CacheDetailHeader on

Set to Ignore Query Cache Control Strings

CacheIgnoreCacheControl On
CacheIgnoreQueryString on

Using a Session token like url.com/my.php?jsessionid=123 should not be cached or even seperatly

CacheIgnoreURLSessionIdentifiers jsessenionid

Use MINIMUM modified factor, that uncached request will be delivered 0.0 = 0% or 1.0 = 100%

CacheLastModifiedFactor 0.99

Set Minimum time for Valid caching (Default 0)

CacheMinExpire 3600

Remove if-Modfied Header before it will take action

Header unset Last-Modified 
* Can also be used in htaccess

This is more to debug at first as it would not Fit in a Comment but should help to Inc raise the Caching of any Object.

djdomi
  • 1,599
  • 3
  • 12
  • 19
  • `CacheDetailHeader` is already on. `CacheIgnoreCacheControl` is already on. `CacheIgnoreQueryString` is undesirable as the query string changes the returned result. `CacheIgnoreURLSessionIdentifiers` wont change anything as there is no session ID in the URL. `CacheLastModifiedFactor` and `CacheMinExpire` only apply when `Expires` isn't specified. `Header unset` doesn't seem to affect `mod_cache`'s behaviour – Alan Birtles Jun 25 '21 at 10:53
  • There's a second way but it makes it a bit more confusing, use Nginx in front of the Apache, it has more control over the caching. – djdomi Jun 27 '21 at 14:49