5

I have a PHP site that has many dynamically generated pages. I'm trying to turn to mod_cache to help boost performance, because in most cases, content does not change in a given day.

I have configured mod_cache as best I could, following examples around the web, including the mod_cache page on apache.org. When I set LogLevel debug, I see a bit of information about the caching that is [not] happening. There are plenty of pairs of lines like this:

[Fri Jun 01 17:28:18 2012] [debug] mod_cache.c(141): Adding CACHE_SAVE filter for /foo/bar
[Fri Jun 01 17:28:18 2012] [debug] mod_cache.c(148): Adding CACHE_REMOVE_URL filter for /foo/bar

Which is fine, because I've set CacheEnable disk /foo, to indicate that I want everything under /foo cached. I'm new to mod_cache, but my understanding about these lines is that it just means that mod_cache has acknowledged that the URL is supposed to be cached, but there are supposed to be more lines indicating that it is saving the data to cache, and then later retrieving them on subsequent hits to the same URL.

I can hit the same URL till I'm blue in the face, whether with F5 refreshing, or not, or with different browsers, or different computers. It's always that pair of lines that shows in the logs, and nothing else.

When I set CacheEnable disk /, then I see more activity. But I don't want to cache the entire site, and there are many, many different subpaths to the site, so I don't want to have to modify code to set no-cache headers in all the necessary places.

I'll mention that mod_rewrite is in use here, rewriting /foo/bar to something like index.php?baz=/foo/bar, but my understanding is that mod_cache uses the pre-rewrite URL, not the post-rewrite URL.

As far as I can tell, I have the response headers not getting in the way of caching. Here's an example from one hit:

Cache-Control:must-revalidate, max-age=3600
Connection:Keep-Alive
Content-Encoding:gzip
Content-Length:16790
Content-Type:text/html
Date:Fri, 01 Jun 2012 21:43:09 GMT
Expires:Fri,  1 Jun 2012 18:43:09 -0400
Keep-Alive:timeout=15, max=100
Pragma:
Server:Apache
Vary:Accept-Encoding

mod_cache config is as follows:

CacheRoot   /var/cache/apache2/
CacheDirLevels 3
CacheDirLength 2
CacheEnable disk /foo

What is getting in the way of mod_cache doing its job of caching?

Pistos
  • 2,863
  • 5
  • 21
  • 21
  • I should add that I am outputting time() at the bottom of each page, so I can see whether a served page is cached or not. – Pistos Jun 01 '12 at 22:29
  • Please check first if apache created directories in /var/cache/apache2. This was my problem. You need to create the cache directory with the right permissions. – Janning Jun 03 '12 at 09:24
  • @Janning: Yes, the cache directory is getting filled with dirs and files with recent timestamps. – Pistos Jun 04 '12 at 01:00
  • I can clear that dir out and double check, though. – Pistos Jun 04 '12 at 01:01
  • Possibly related: http://serverfault.com/questions/377048/apache-mod-cache-strip-ignore-only-google-analytics-cookies – Pistos Jun 04 '12 at 03:34
  • By the was: You NEED to add "CacheIgnoreHeaders Set-Cookie" because otherwise different user got the same session cookie set if PHP page is served from cache and sends a cookie in the original request. It is a security hazard! – Janning Jun 04 '12 at 12:09
  • I have already added that. It doesn't help. My current investigations have led me to think that the PHPSESSID cookie is getting in the way, though. – Pistos Jun 04 '12 at 15:00
  • For the time being, I am using `EnableCache /` with `DisableCache` on selected subpaths to blacklist them. The current issue now is that the pages I'm interested in use sessions, so I need to decide what to do about that. – Pistos Jun 06 '12 at 18:47

2 Answers2

2

This is my configuration and it works fine:

<IfModule mod_disk_cache.c>
    CacheEnable disk /
    CacheRoot /var/cache/apache2/mod_disk_cache
    CacheIgnoreCacheControl On
    CacheMaxFileSize 2500000
    CacheIgnoreURLSessionIdentifiers jsessionid
    CacheIgnoreHeaders Set-Cookie
</IfModule>

You need to have CacheIgnoreURLSessionIdentifiers which fits your needs so maybe on PHP you need

CacheIgnoreURLSessionIdentifiers PHPSESSID

regards Janning

Janning
  • 1,191
  • 1
  • 19
  • 35
  • Ignoring PHPSESSID didn't help, but I appreciate you taking the time to comment on my problem. – Pistos Jun 04 '12 at 21:49
  • send your full and real config to me per PM at janning@vygen.de i will check it out and try to help you. We shouldn't use SO as a messaging platform. btw: your question relates to serverfault.com and not stackoverflow – Janning Jun 05 '12 at 07:43
1

I was facing the same problem trying to map

CacheEnable Disk /api

The only mapping that worked was

CacheEnable Disk / 

Which cached everything.

Even though the documentation says that mod_cache runs after url-rewrite it didn't. To get it working I changed it to

CacheEnable Disk /index.php/api
Niclas
  • 119
  • 4