Score:0

Apache Caching disabled when client sends If-Modified-Since header

li flag

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.

no flag
Can you try setting the two headers ? `"CacheIgnoreNoLastMod On"` and `"CacheIgnoreHeaders If-Modified-Since"`
li flag
@SreerajKarichery no change from setting those settings in mod_cache
djdomi avatar
za flag
there might be a chance to use `<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css)$"> Header unset Last-Modified Header unset If-Modified-Since </FilesMatch>` or similar
li flag
@djdomi no, no change. I think I'd already tried that, I'm not sure the headers are removed before mod_cache sees them
djdomi avatar
za flag
thats why you should try to remove it :)
djdomi avatar
za flag
By thee way, i found maybe some intressting point to you: https://stackoverflow.com/questions/10847157/handling-if-modified-since-header-in-a-php-script
Score:0
za flag

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.

li flag
`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
djdomi avatar
za flag
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.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.