ProxyPassMatch with ProxyPassReverse

2020-05-25 08:06发布

问题:

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

回答1:

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