21

Folks, We are trying to setup Apache reverse proxy for the following scenario:

  • Incoming requests take the form http://foo.com/APP/v1/main.html
  • For some servers the URL will reference a difference version, say, http://foo.com/APP/v2/main.html
  • An upstream load balancer (HAProxy) will send the request to the right server which will have an Apache2 reverse proxy fronting a JBoss server.
  • When the request shows up at Apache 2 it will have request path like /APP/v1/main.html
  • We want it to (reverse) proxy out to http://localhost:8080/AppContext/main.html, irrespective of version fragment in URL (v1, v2, etc.).

I have been trying to do this like so:

ProxyPassMatch ^/.*?/APP.*?/(.*)$ http://localhost:8080/AppContext/$1
ProxyPassReverse /APP http://localhost:8080/AppContext

My questions are:

  1. Is my use of ProxyPassMatch correct?
  2. My ProxyPassReverse is "static". How do I make it aware of the potentially variable stuff after /APP?

Thanks for any insights.

-Raj

Raj
  • 2,852
  • 4
  • 29
  • 48

1 Answers1

13

You're close, try changing the regex a little to account for the version fragment:

ProxyPassMatch ^/.*?/APP.*?/v[0-9]+/(.*)$ http://localhost:8080/AppContext/$1

The ProxyPassReverse is mostly to ensure the rewriting on-the-fly of location header fields in the responses given by the proxied app. So when it returns a 301 redirect to, say, http://localhost:8080/AppContext/something, apache knows to change it to /APP/v1/something so information behind the proxy won't get exposed. Because you have a dynamic URL used in the reverse proxy, you have a few choices here. You can either send it to the HAProxy load balancer (not sure where that is for you), or you can just pick one and hope for the best. For example, if you have a load balancer at /APP/balancer/ which then sends requests to /APP/v1/, /APP/v2/, /APP/v3/, etc. Then you can do this:

ProxyPassReverse /APP/balancer http://localhost:8080/AppContext

Otherwise, you can just point it to one and hope for the best:

ProxyPassReverse /APP/v1 http://localhost:8080/AppContext
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • Thanks Jon for the prompt response. We are trying to keep our Apache config free of version specific entries in order to have a homogenous template that can be freely deployed in the cluster. It looks like the ProxyPassReverse thing cannot be reliably done. Would you suggest accomplishing this through some kind of a rewrite module? – Raj Aug 07 '12 at 20:02
  • @Raj Rewrite isn't going to help, because this is in the other direction. The app server attempts to redirect a request, it doesn't know anything about the actual URL that's being used to access it, so it just says: "Go to http://localhost:8080/AppContext/something", which the browser will return a 404 because the computer the browser is on doesn't have an application server running on localhost port 8080 – Jon Lin Aug 07 '12 at 20:08