Friday, July 23, 2010

External Rewriting Program and RewriteLock

QUESTION:
I use External Rewriting Program in my apache config
RewriteEngine on
RewriteMap myMapProgram prg:/proxy/location.php

but the output of this script is messed up and apache gives weird errors. What's that about?

ANSWER:
When you use External Rewriting Program make sure to always specify RewriteLock directive.

In fact after adding RewriteMap prg: statement you'll notice that in your error logs apache warns that you should use RewriteLock directive:
Running external rewrite maps without defining a RewriteLock is DANGEROUS!

And there's at least one good reason to use RewriteLock.
If you don't do it, everything will seem to be fine until your live server starts getting much traffic. Concurrent apache hits result in calls to your External Rewriting Program which won't be able to normally synchronize it's output across apache threads.
The results of this are unpredictable - sometimes apache reads normal output from myMapProgram, but sometimes it is bullshit like 1 or 2 random symbols from output.

For example I started getting
"Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace."
error because incorrect output of my custom rewriting script caused wrong redirects in my code.

What's this blog about?

I'm a web developer and come across interesting problems and solutions all the time.
For me this blog is a place where I can share my solutions for specific problems with anyone else.
Usually problems described here are non-trivial and can't be simply googled.
Cheers!