Reverse proxy for sites with HTTP authentication

Publication date
06/28/2007
Categories
, ,

Situation

You want to create a reverse proxy to a site that requires HTTP basic authentication (the typical dialog asking you for a user and password) without having to ask the user for this credentials.

This functionality is specially useful when you require multiple HTTP authentications as it seems you can only have one (after providing the first credentials user will be asked for the second ones, but after sending it, he will be asked again for the first ones, as if browser only remembered the last one).

Steps

Note: This guide is for Apache httpd web server.

  1. Create a reverse proxy as usual:

    ProxyPass /example/ http://example.com/
    ProxyPassReverse /example/ http://example.com/

    If you try it now, you will see it asks your for a user and password.

  2. Configure automatic login:

    <Location /example/>
        # User: foo, password: 123
        RequestHeader set Authorization: "Basic Zm9vOjEyMw=="
    </Location>

    This strange string (Zm9vOjEyMw==) is the Base 64 encoding of user and password ("foo" and "123" in this example), and yes, you will have to generate your own for your user and password. You can use the following command-line PHP script (http-auth-encode.php) to do it:

    <?php echo base64_encode($argv[1] . ':' . $argv[2]) . "\n" ?>

    Calling it this way:

    $ php http-auth-encode.php user password

    After restarting apache and trying it again, you should be logged in automatically :-)

Comment this post

Fields marked with * are required.

*
It won't be made public. If you have a gravatar it will be displayed.
*