user7044932
user7044932

Reputation: 39

Reverse proxy for external URL - Apache

I configured my apache so that it can forward my requests to external URL like google.com, but the reverse proxy doesn't work.

<VirtualHost *:443>
ServerName authtest.com
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
  Order allow,deny
  Allow from All
</Proxy>
<LocationMatch "/google">
  ProxyPass https://www.google.com/
  ProxyPassReverse https://www.google.com/
</LocationMatch>
</VirtualHost>

Is it possible for me to reverse proxy external websites?

Upvotes: 0

Views: 15858

Answers (2)

Jubba Smail
Jubba Smail

Reputation: 1225

Chech this GIT Repo I forked a GIT Repo and customized it to work with scenario:

[browser] --(Host: google.local)--> [apache proxy] --(Host: google.nl)--> Google

The Apache config as follows:

<VirtualHost *:80>
        ServerName google.local
    SSLProxyEngine on
    ProxyRequests Off
    <Proxy *>
        Order allow,deny
        Allow from All
    </Proxy>
        ProxyPass / https://www.google.nl/
        ProxyPassReverse / https://www.google.nl/

        ErrorLog /var/log/apache2/google.local-error.log
        CustomLog /var/log/apache2/google.local-access.log combined

</VirtualHost>

Upvotes: 3

David Duponchel
David Duponchel

Reputation: 4069

Is it possible for me to reverse proxy external websites?

Yes but with significant downsides.

Note: when I tried your configuration, I got SSL Proxy requested for [...] but not enabled [Hint: SSLProxyEngine] in the logs so I added SSLProxyEngine on.

Host issue

When you make a HTTP/1.1 request to a server, you automatically add the hostname in the request. When you proxy them, you have two possibilites:

[browser] --(Host: authtest.com)--> [apache proxy] --(Host: authtest.com)--> Google

or

[browser] --(Host: authtest.com)--> [apache proxy] --(Host: google.com)--> Google

The first one is what you get with ProxyPreserveHost On. Google servers won't handle requests for authtest.com, you should remove this line.

Even in the second case, you can have issues. ProxyPassReverse will handle redirects but only for the given domain: I'm in France, google.com redirects me to google.fr (a different domain) and the reverse proxy doesn't rewrite the redirect.

An other issue is the referer: if a service sees requests for images/css/js coming from a different web site it may consider it as bandwidth leeching and block them. Now, you need to rewrite the html of the response too (mod_proxy_html will help but it's not a silver bullet).

Path issue

In your example, you proxy <authtest>/google to <google>/. Like above, you need to rewrite the html: absolute links/resources won't work unless your server adds /google everywhere. Same for relative links/resources (but with more edge cases). If you owned the backend server, you could have checked urls in html/css/js files. Here, if the url is built dynamically in the browser using js you can't do anything.

If you can proxy / to / (or /whatever to /whatever) you will avoid a lot of issues here.

Upvotes: 3

Related Questions