Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Problem:
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.

Considerations:
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.

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

Current configuration:

<VirtualHost 127.0.0.1:8081>
    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
    </Location>
</VirtualHost>

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.

Questions:
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.

share|improve this question

2 Answers 2

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.

References:

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

share|improve this answer
    
I think this answer is just in regards to consideration #2, is that right? –  jrullmann Apr 19 '13 at 23:34
2  
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...

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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