Adventures with mod_ext_filter

Omertà unfortunately prevents me from telling you why I’ve been experimenting with this lesser known corner of Apache HTTPD, but I can at least write up a few hints, since this is a sparsely documented area.

Let’s set the scene: you have some sort of web site or web app. You want to make some alterations which could literally be achieved by running find and replace over the site/application output. However, you either can’t or don’t want to modify the site/app directly.

You might well be running the site behind an Apache HTTPD proxy in any case – all my Django sites are like this, so Apache can serve the static content.

Wouldn’t it be convenient if there was some way of having the proxy do a little light modification of the content on its way to the end user?

There is. However, if the wording on that page about writing output filters in C puts you off (it certainly put me off!), don’t despair. Just dig a little deeper and you will discover that you can write filters in any old programming language and have Apache call them.

There were just a couple of things I had to watch out for when getting this working in Apache 2.4:

  • The filter chain includes gzip/deflate if you have that on, which is pretty much the norm these days. So you need to force the ordering of your filters if you don’t want your script being fed gzipped content (duh!)
  • You can use ExtFilterOptions to dump stderr from your filter into the Apache error log, which is handy for diagnostic purposes when staring at a blank page

My config ended up thus

ExtFilterDefine tweak_stuff mode=output cmd=”/path/to/script-which-replaces-some-stuff.py”

<Location /to/filter>

SetOutputFilter INFLATE;tweak_stuff;DEFLATE

ExtFilterOptions LogStderr

</Location>

It’s not obvious from the documentation that filters will work in a Location block, but they do.

Fortunately, in my case, the filtered elements were rarely used pages of an application, but I can imagine this might start to rack up some performance problems if you did it too enthusiastically across a site

Leave a Reply