O'Reilly logo

Linux Server Hacks by Rob Flickenger

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Hack #99. Distributing Load with Apache RewriteMap

Scale to any number of web application servers with RewriteMap

As we saw in the previous hack, it is possible to transparently serve content from any arbitrary web server using mod_proxy and mod_rewrite. Rather than using an external redirect [R], the proxy target [P] makes the content appear to come from the server from which the client originally requested content, without changing the URL line in the client browser. As we saw earlier, one application of this technique is to spread the load of serving pages across two machines, making it possible to serve many more hits than a single machine can handle.

But what if you need to serve even more traffic, to the point that a single application server is no longer sufficient? We need a mechanism that will allow the proxy servers to choose from an available pool of application servers, preferably in a way that we can direct more hits at more capable servers. The RewriteMap directive gives us this functionality.

Here's an example of how to specify a RewriteMap:

RewriteMap server rnd:/usr/local/apache/conf/servers.map

The RewriteMap line just defines a map called server, physically bound to the servers.map file in the conf/ directory. The rnd: makes this a randomized plain text map.

The format of the servers.map file is very straightforward:

web www1|www2|www3|www4|www5

The left-hand side is an arbitrary variable name that we can use just about anywhere in our configuration file, as ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required