0

I have two web servers, www.example.com and www.userdir.com. I'm trying to make www.example.com as the front end proxy server to serve requests like in the format of http://www.example.com/~username such as

http://www.example.com/~john/

so that it sends an internal request of

http://www.userdir.com/~john/

to www.userdir.com. I can achieve this in Apache with

ProxyPass /~john http://www.userdir.com/~john  
ProxyPassReverse /~john http://www.userdir.com/~john

The ProxyPassReverse is necessary as without it a request like http://www.example.com/~john without the trailing forward slash will be redirected as http://www.userdir.com/~john/ and I want my users to stay in the example.com space.

Now, my problem is that I have a lot of users and I cannot list all those user names in httpd.conf. So, I use

ProxyPassMatch ^(/~.*)$ http://www.userdir.com$1

but there is no such thing as ProxyPassReverseMatch in Apache. Without it, whenever the trailing forward slash is missing in the URL, one will be directed to www.userdir.com, and that's not what I want.

I also tried the following to add the trailing forward slash

RewriteCond %{REQUEST_URI} ^/~[^./]*$  
RewriteRule ^/(.*)$ http://www.userdir.com/$1/ [P]

but then it will render a page with broken image and CSS because they are linked to http://www.example.com/images/image.gif while it should be http://www.example.com/~john/images/image.gif.

I have been googling for a long time and still can't figure out a good solution for this. Would really appreciate it if any one can shed some light on this issue. Thank you!

3 Answers 3

1

You can just ignore the username and anything that follows when fixing up the redirect:

ProxyPassReverse /~ http://www.userdir.com/~

Since this is just a prefix substitution.

1
  • 1
    Ah, what a simple answer. I asked a silly question. It'd be great if they still implement a ProxyPassReverseMatch though as it can handle regular expression for more complicated cases (like matching suffix). Thanks covener!
    – Alex Man
    Jan 11, 2011 at 0:53
0

I don't think you understand the function of the ProxyPassReverse directive correctly. Here is an exerpt from the documentation:

http://httpd.apache.org/docs/2.2/mod/mod_proxy.html#proxypassreverse

This directive lets Apache adjust the URL in the Location, Content-Location and URI headers on HTTP redirect responses. This is essential when Apache is used as a reverse proxy (or gateway) to avoid by-passing the reverse proxy because of HTTP redirects on the backend servers which stay behind the reverse proxy.

Only the HTTP response headers specifically mentioned above will be rewritten. Apache will not rewrite other response headers, nor will it rewrite URL references inside HTML pages. This means that if the proxied content contains absolute URL references, they will by-pass the proxy. A third-party module that will look inside the HTML and rewrite URL references is Nick Kew's mod_proxy_html.

Also, you don't want to add a trailing slash automatically to any URL - this can break a lot of links.

Now, to force a trailing slash after a user directory, I'd go for something like that:

RewriteRule ^/(~\w+)(/(.+)?)?$ http://www.userdir.com/$1/$3 [P]
3
  • Thanks for your answer ExtraT. The reason I used ProxyPassReverse is that it would avoid by-passing the reverse proxy (www.example.com) due to redirects on www.userdir.com. I can see the sentences you quoted but for some reason all the URL references on the user page DID get rewritten correctly when I use a combination of ProxyPass and ProxyPassReverse. I tried using your RewriteRule with a URL without the trailing slash but the effect is the same as mine, that is, all the images and CSS on the user page are broken due to the same reason (example.com/images/image.gif). Any idea?
    – Alex Man
    Jan 7, 2011 at 2:46
  • By the way, is there a way to add link breaks and formatting in comment? Or I should click "Answer Your Question" (but I'm not answer my own question, just want to leave a more readable comment)?
    – Alex Man
    Jan 7, 2011 at 2:47
  • You should add your information to your question.
    – womble
    Jul 15, 2011 at 13:22
0

A very interesting post, I all user directories on a backend server and was doing the proxying on a front end server, this worked for me..

ProxyPassMatch ^(/~.*)$ http://www.backend.com$1
ProxyPassReverse /~ http://www.backend.com/~

I tested this using an incognito page in Chrome, as Firefox was caching things so my test pages didn't work properly. I think to fix your problems in page you might have to use mod_proxy_html as suggested above, I was lucky on the back end all css and images was in a folder called media that wasn't on my front end machine so I just added proxy pass and reverse to my /media directory.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .