Apache mod_proxy with automatic authentication
I am setting up a proxy in apache through the directive ProxyPass
and ProxyPassReverse
for some urls:
ProxyPass /mypath http://myotherserver.com
ProxyPassReverse /mypath http://myotherserver.com
However, myotherserver.com
require a (basic) authentication. If I don't do anything, this authentication is passed to the final client. For some reason, I don't want that and I would like to add the credentials directly in my apache configuration. How can I do that ?
I tried:
ProxyPass /mypath http://user:[email protected]
ProxyPassReverse /mypath http://user:[email protected]
But it does not seems to work. Thanks in advance for your help.
Solution 1:
I have actually found the solution. I hope it can be useful for other people:
Run the following python script to get your authentification hash
:
import base64
hash = base64.b64encode(b'user:password')
Add the following directive in your apache configuration:
<Location /mypath>
RequestHeader set Authorization "Basic $hash"
</Location>
where $hash
is replaced with the previously computed string.
Make sure that mod_proxy
and mod_headers
are available (a2enmod proxy
and a2enmod headers
). Restart apache2 and you are done :)
Solution 2:
Thanks @ThR37 - this is more of a comment for your answer, but can't format it there. :/
I needed to do it with mod_rewrite, but your method was used:
RewriteEngine On
SSLProxyEngine on
RewriteCond %{REQUEST_URI} ^/34506a81-1a6d-4596-beaf-580da9c98cca$
SetEnvIf REQUEST_URI "/34506a81-1a6d-4596-beaf-580da9c98cca" DOAUTH
RequestHeader set Authorization "Basic dXNlcjpwYXNzd29yZA==" env=DOAUTH
RewriteRule /34506a81-1a6d-4596-beaf-580da9c98cca https://www.example.com/my/path [P,L]
ProxyPassReverse /34506a81-1a6d-4596-beaf-580da9c98cca https://www.example.com/my/path