Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ProxyPassMatch with ProxyPassReverse

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

like image 565
Raj Avatar asked Aug 07 '12 18:08

Raj


People also ask

What is ProxyPass and ProxyPassReverse?

The "ProxyPass" and "ProxyPassReverse" parameters are used to tell Apache how to proxy requests. They require the "mod_proxy.so" and "mod_proxy_http.so" Apache modules, which are loaded by default in RHEL5 and RHEL6, but check the following lines are uncommented in the "/etc/httpd/conf/httpd. conf" file to make sure.

What is proxy pass match?

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.

What is proxy preserve host?

The ProxyPreserveHost directive is used to instruct Apache mod_proxy, when acting as a reverse proxy, to preserve and retain the original Host: header from the client browser when constructing the proxied request to send to the target server.

What is Proxyrequests?

When you send a web request, your request goes to the proxy server first. The proxy server then makes your web request on your behalf, collects the response from the web server, and forwards you the web page data so you can see the page in your browser.


Video Answer


1 Answers

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
like image 65
Jon Lin Avatar answered Oct 07 '22 14:10

Jon Lin