Httpd – Two reverse proxies with apache 2.4 and mod_proxy

apache-2.4httpdmod-proxyreverse-proxy

I'm Trying to configure a environment with two proxies. The idea is that the first proxy redirects to the second and the second to the final web.

The configuration works for normal request. but I have problems with the HTML redirect.

The environment is as follows:

M1 (with apache)        M2 (with apache)        M3 (e.g. Jetty)
  host: h1                host: h2                host: h3
  port:9001               port: 9002              port: 9003
  proxy policies:         proxy policies:         webs: 
    /a/b/ *2:9002/          / *3:9003/              c
                                                    d

The normal request is http://h1:9001/a/b/d. The url translation will be the following:

(m1) http://h1:9001/a/b/d -> (m2) http://h2:9002/d -> (m3) http://h3:9003/d

My configuration works for this request.

The problem is when I try to do a html redirection from web c to d. (typically 302). The redirect url must return to browser. The url translation should be the following:

(m1) http://h1:9001/a/b/c -> (m2) http://h2:9002/c -> (m3) http://h3:9003/c 
     ---(redirection to http://h3:9003/d)---
(m3) http://h3:9003/d  -> (m2) http://h2:9002/d -> (m1) http://h1:9001/a/b/d
     ---(making redirection)---
(m1) http://h1:9001/a/b/d -> (m2) http://h2:9002/d -> (m3) http://h3:9003/d

The problem is that the url that return to the browser is http://h1:9001/d and not http://h1:9001/a/b/d

If the second proxy (m2) does not exist, there is not this problem and the address that return to the browser is http://h1:a/b/d

Which can be the problem?

Thanks in advance.

Configuration files:

extract of httpd.conf in m1:

<VirtualHost *:9001>
    ProxyRequests On
    ProxyPreserveHost On
    ProxyPass /a/b/ http://h2:9002/
    ProxyPassReverse /a/b/ http://h2:9002/
</VirtualHost>

extract of httpd.conf in m2:

<VirtualHost *:9002>
    ProxyRequests On
    ProxyPreserveHost On
    ProxyPass / http://h3:9003/
    ProxyPassReverse /a/b/ http://h3:9003/
</VirtualHost>

I test this configuration with curl command:

curl -L -i http://h1:7080/a/b/c

The result:

HTTP/1.1 302 Found
Date: Wed, 09 Dec 2015 13:49:15 GMT
Server: Jetty(9.3.5.v20151012)
Location: http://h1:9001/d
Content-Length: 0

HTTP/1.1 404 Not Found
Date: Wed, 09 Dec 2015 13:49:15 GMT
Server: Apache/2.4.17 (Unix)
Content-Length: 201
Content-Type: text/html; charset=iso-8859-1

# ERROR BODY (404)

Best Answer

I have found the problem. It is the ProxyPreserveHost directive in the m1 reverse proxy. It must be set to off

I think that the correct configuration must set the directives ProxyPreserveHost and ProxyRequests to off

The final configuration are

<VirtualHost *:9001>
    ProxyRequests Off
    ProxyPreserveHost Off
    ProxyPass /a/b/ http://h2:9002/
    ProxyPassReverse /a/b/ http://h2:9002/
</VirtualHost>

and

<VirtualHost *:9002>
    ProxyRequests Off
    ProxyPreserveHost Off
    ProxyPass / http://h3:9003/
    ProxyPassReverse /a/b/ http://h3:9003/
</VirtualHost>
Related Topic