11

Looking for help for the next Apache's reverse-proxy problem.

  • have an Apache 2.4 on the localhost:80 (a.a.a.a:80)
  • have 3 different web applications
    • one runs on localhost:8000 (a.a.a.a:8000)
    • two others are in another machines - so x.x.x.x:8000 and y.y.y.y:8000
  • have some static content (few html pages) on the basic apache:80 's DocumentRoot

Requirements:

  • appX (on the x.x.x.x:8000) should be accessed by http://a.a.a.a/appX/
  • appY (on the y.y.y.y:8000) as http://a.a.a.a/appY/
  • existing static pages (from the apache's DocumentRoot) should be served as usually by apache
  • defapp - everything other should be proxied to a.a.a.a:8000

It is easy configure the appX and appY like the next:

ProxyPass         /appX/ http://x.x.x.x:8000/
ProxyPassReverse  /appX/ http://x.x.x.x:8000/

ProxyPass         /appY/ http://y.y.y.y:8000/
ProxyPassReverse  /appY/ http://y.y.y.y:8000/

The above works OK. So when tried access http://localhost/appX/ got a response from the appX at the x.x.x.x:8000.

But have a problem with the default destinaton for everything other. When tried to add:

ProxyPass         /   http://127.0.0.1:8000/
ProxyPassReverse  /   http://127.0.0.1:8000/

It doesn't works as I hope...

With the above want tell to apache - everything other what is not /appX/ or /appY/ send to 0:8000.

And this is unfortunately doesn't works, the defapp what runs on 0:8000 got the requests for the appX and appY, and the requests for the static pages too.

Switching the order of definitions, so

#define first the "default destination"
ProxyPass         /   http://127.0.0.1:8000/
ProxyPassReverse  /   http://127.0.0.1:8000/

#and after the appX and appY
ProxyPass         /appX/ http://x.x.x.x:8000/
ProxyPassReverse  /appX/ http://x.x.x.x:8000/
ProxyPass         /appY/ http://y.y.y.y:8000/
ProxyPassReverse  /appY/ http://y.y.y.y:8000/

doesn't works either. Everything is proxied to localhost:8000.

So, the question is: is possible configure Apache as a reverse proxy to handling the above defined requiremens?

kobame
  • 435
  • 2
  • 5
  • 12

3 Answers3

7

Wrap your ProxyPass references in a <VirtualHost> definition, like this:

<VirtualHost *:*>
ProxyPreserveHost On
ProxyPass / http://192.168.111.2/
ProxyPassReverse / http://192.168.111.2/
ServerName hostname.example.com
</VirtualHost>

Create two separate ones with the ServerNames you want, then another without ServerName or the Proxy options defined (just DocumentRoot). Put that one last.

More info here.

Nathan C
  • 2,762
  • 1
  • 19
  • 26
4

Small impovement to Warren Seine's answer (missed "app/" in the first RewriteRule (....127.0.0.1/$1 [P....) )

RewriteRule         ^/app/(.*) http://127.0.0.1/$1 [P,L]

must be

RewriteRule         ^/app/(.*) http://127.0.0.1/app/$1 [P,L]

So, summing up - In case when all your links are distributed among several hosts (accurately as in your case) try this:

ProxyPreserveHost   On
RewriteEngine       On

RewriteRule         ^/appX/(.*) http://x.x.x.x:8000/$1 [P,L]
ProxyPassReverse    /appX/      http://x.x.x.x:8000/
RewriteRule         ^/appY/(.*) http://y.y.y.y:8000/$1 [P,L]
ProxyPassReverse    /appY/      http://y.y.y.y:8000/
RewriteRule         ^/(.*)      http://127.0.0.1:8000/$1 [P,L]
ProxyPassReverse    /           http://127.0.0.1:8000

In case when you need to spread your links at the one host (with different ports & BaseURLs)

ProxyPreserveHost   On
RewriteEngine       On

RewriteRule         ^/appX/(.*) http://127.0.0.1:8001/appX/$1 [P,L]
ProxyPassReverse    /appX/      http://127.0.0.1:8001/appX
RewriteRule         ^/appY/(.*) http://127.0.0.1:8002/appY/$1 [P,L]
ProxyPassReverse    /appY/      http://127.0.0.1:8002/appY/
RewriteRule         ^/(.*)      http://127.0.0.1:8000/$1 [P,L]
ProxyPassReverse    /           http://127.0.0.1:8000
osterik
  • 41
2

You can use rewrite rules with the P flag.

Rewrite rules are more flexible than ProxyPass directives and you can use the L flag to stop processing the rule set and avoid the problem you mention (hitting multiple backends).

Here's an example:

ProxyPreserveHost   On
RewriteEngine       On

RewriteRule         ^/app/(.*) http://127.0.0.1/$1 [P,L]
ProxyPassReverse    /app/ http://127.0.0.1/app

RewriteRule         ^/blog/(.*) http://127.0.0.1/blog/$1 [P,L]
ProxyPassReverse    /blog/ http://127.0.0.1/blog

RewriteRule         ^/(.*) http://127.0.0.1/$1 [P,L]
ProxyPassReverse    / http://127.0.0.1
Warren Seine
  • 249
  • 3
  • 10