I'm trying to configure apache as a reverse proxy where the remove server is specified in a query string parameter.

Let's say I'm serving a site at mywebapp.com. If you navigated to http://mywebapp.com/proxy?url=http://www.someothersite.com, then apache should act as a reverse proxy to http://www.someothersite.com. Similarly, if you navigated to http://mywebapp.com/proxy?url=https://login.notmysite.com, then apache should act as a reverse proxy to https://login.notmysite.com.

1. Rewrite relative urls in the response html to go through the proxy
2. Rewrite redirect responses from the remote server. For example, if the user requests mywebapp.com/proxy?url=https://login.notmysite.com and the remote server responds with a redirect to https://login.notmysite.com/, then the user's browser should see a redirect to mywebapp.com/proxy?url=https://login.notmysite.com/.
3. Support http and https
4. Support cookies
5. Reasonable security. I don't want to open a proxy for anyone in the world to use - it should only be used in an iframe on the site hosted at mywebapp.com.

I'm using apache 2.2.24, but I can upgrade if that would make the problem easier to solve.

Current configuration:

    ServerName www.mywebapp.com
    SSLProxyEngine On

    RewriteEngine on
    RewriteCond %{QUERY_STRING} ^url=(.*)$
    RewriteRule ^/proxy - [E=url:%1]

    RewriteCond %{QUERY_STRING} ^url=(https?://[^:/]+)/?
    RewriteRule ^/proxy - [E=url_host:%1]

    RewriteCond %{QUERY_STRING} ^url=(.*)$
    RewriteRule ^/proxy                %1   [P]

    ProxyPassInterpolateEnv On
    ProxyPassReverse /proxy/ ${url_host} interpolate

    <Location /proxy>
        ProxyHTMLEnable On
        ProxyHTMLInterp On
        ProxyHTMLURLMap /   ${url_host}/ V
        RequestHeader   unset   Accept-Encoding

This configuration appears to be working correctly for consideration #1, but I hit a roadblock with consideration #2. I know that you usually handle redirects with the ProxyPassReverse command, but I'm not sure how get it to rewrite the query string parameter.

Given the considerations, is Apache an appropriate tool for solving this problem?
If so, how do I setup the configuration for apache to meet all of the above considerations?

I know this is a big question - if you can contribute an answer that helps me meet just one of the considerations then I'll update the above configuration for the community to see.

  • 2
    This sounds kind of like an XY problem. – GregL Jun 27 '15 at 12:48

mod_headers may be able to handle this.

Header set Location http://mywebapp.com/proxy?%{url}e expr="resp('Location') =~ /.*/"

The expr part requires apache 2.4.

Note: I have not tested this.


http://httpd.apache.org/docs/2.4/mod/mod_headers.html http://httpd.apache.org/docs/2.4/expr.html

  • I think this answer is just in regards to consideration #2, is that right? – jrullmann Apr 19 '13 at 23:34
  • 3
    Yes, and based on the difficulty of this setup--URL in query string--I wouldn't use apache for this; rather, a custom app would be less awkward and more extensible. – Mark Wagner Apr 20 '13 at 0:18

Maybe solving it with Pound would be easier and more secure. But then it's not a redirect but a reverse-proxy.

Also a small php-script could do the job too...

  • A reverse proxy is exactly what I'm trying to setup with Apache, so that's fine. But the reverse proxy would need to correctly handle all the considerations above - including handling redirects from the remote server. Questions for you: 1) Why do you think Pound would be better than apache? 2) Have you seen a php script that handles all of the considerations? It doesn't seem to me that it would be small or straight-forward given those, so I'm very interested in seeing an example. – jrullmann Apr 22 '13 at 15:22
  • Well as for the solution using pound: *) it directly supports URL-parameters for rules, *) It will require having all remote pages set up in the config, so its quite static - the PHP script: *) Can load arbitrary remote location (even pictures etc), *) phpmyproxy does this for example. Basicly all you need is a file_get_contents() if you're keen with PHP. – f0o Apr 22 '13 at 16:25
  • Will file_get_contents() rewrite resource urls with relative paths? Intercept redirects from the remote server to send the browser through the proxy? I'm not very familiar with php, but it doesn't seem like one function would support all the above considerations. – jrullmann Apr 25 '13 at 13:32

Biggest problem with your idea is that if you expect the reverse proxy to send cookies to the backend, but the client wouldn't send cookies to the frontend (unless the backend cookie domain was inferior to the frontend).

I would suggest that mod_proxy would likely be too difficult to use, and in this case you'd get to your goal faster with mod_rewrite.

... but I think you're probably trying to solve a problem with a likely-faulty design. Perhaps if you can elucidate further about the problem you are trying to solve with this solution, we might be able to suggest something a bit cleaner.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.