Apache redirect request to another server on error

I currently have 3 web servers serving large files load balanced at DNS level. There's one "master" server, and two "slave" servers.

The master server has all of the files required, and it pushes files to the slaves. In some instances, one or both of the slaves don't have a particular file. In this case, the whole process gives up which isn't ideal.

When a slave server doesn't have a file, I want it to redirect the request to the master so the process doesn't fail.

I know I can use ErrorDocuments to show a custom error page (even if that error page is on another server, like so): ErrorDocument 404 http://master.exmaple.com/404page.html however, I want it to serve the file from the master, and not an error page.

It seems to me that it can be accomplished with something like ErrorDocument 404 http://master.example.com/{{requested_file}} but I don't know if such a thing is even possible.

I think I'm missing something simple here. Could someone point me in the right direction?

Apache 2.4.7 running on Ubuntu 14.04

Thank you.


Solution 1:

You may be able to do this with a combination of ErrorDocuments and mod_rewrite

So your ErrorDocument should be

ErrorDocument 404 /not_found_redirect/

So when a doc is not found, the server redirects to a local url. The trick is that many of the environment variables are passed to the redirected URL (http://httpd.apache.org/docs/current/custom-error.html#variables). The REDIRECT_REQUEST_URI should be the one you want.

Because then in mod_rewrite, you use the environment variables to redirect to the master server

RewriteEngine On
RewriteRule   ^/not_found_redirect/  http://master.example.com%{REDIRECT_REQUEST_URI}  [R,L]

And the browser should be sent to the correct location on the master server.