Dienstag, 12. Januar 2010

Costum 503 error page for apache with mod_proxy

I recently needed to create a custom 503 error page for an apache that serves multiple vhosts via ProxyPass.

Consider the following situation:
We have an apache proxy that serves a bunch of internal webservices via mod_proxy.
These webservices all have nice costum error pages esp. for the classical 404 - page not found.
Every now and then one of these Servers is not reachable, due to maintenance or heavy load. If this happens, the apache running mod_proxy will show an ugly standard 503 error message.



So lets get rid of that:
The first thought is putting an ErrorDocument directive in the virtual host definition:
<VirtualHost *:80>
...servername and other fancy stuff...
ProxyPass / http://some_internal_server/
ProxyPassReverse / http://some_internal_server/

ErrorDocument 503 /my_cool_503_error.html
</VirtualHost>

Alas this does not work as intended:
when apache tries to access the error document, the request is again proxied, resulting in another 503 error (good luck apache will notice this and not go in an recursive loop).

My next idea was to use the ProxyErrorOverride directive, but this will result in the 404 pages no longer being proxied as intended.

After some googling (which didn't realy help) i finally came up with the followin solution:

Let apache listen an an additional port (ex.81) and create an additional vhost for this port:

Listen *:81
<VirtualHost *:81>
DocumentRoot /var/www/
</VirtualHost>

Now, create a proxy rule in the original vhost:
<VirtualHost *:80>
...servername and other fancy stuff...

ProxyPass /proxy-error/ http://localhost:81/proxy-error/
ProxyPassReverse /proxy-error http://localhost:81/proxy-error/


ProxyPass / http://some_internal_server/
ProxyPassReverse / http://some_internal_server/

ErrorDocument 503 /proxy-error/my_cool_503_error.html
</VirtualHost>

...and put your my_cool_503_error.html into /var/www/proxy-error/

Now everything is being proxied to the internal server, except for the 503 document, which is proxied to the local instance - just as we wanted.

Note: You could secure this setup a little by binding the local vhost on port 81 to 127.0.0.1

Keine Kommentare: