简体   繁体   中英

Apache rewrite or proxy with redirect of internal server

I am trying to use Apache as a proxy for an internal tomcat server, but the tomcat server performs a redirect that got me crazy

I first tried mod_proxy through:

ProxyPass / ajp://127.0.0.1:8045/bv-via-web/
ProxyPassReverse / ajp://127.0.0.1:8045/bv-via-web/

When accessing / (root) the internal server performs a redirect to /bv-via-web/hola which doesn't exist. It should redirect to /hola . How can I achieve this through the Apache config?

As a workaround I though I add the following:

ProxyPass /bv-via-web/ ajp://127.0.0.1:8045/bv-via-web/
ProxyPassReverse /bv-via-web/ ajp://127.0.0.1:8045/bv-via-web/

But that doesn't work as it will extend the url to an internal request of bv-via-web/bv-via-web/hola . So basically I want to change the url but don't know how that is possible with mod_proxy.

I then tried mod_rewrite , with the following:

RewriteEngine on
RewriteRule ^/bv-via-web/(.*)$ http://127.0.0.1:8040/bv-via-web/$1 [P,L]
RewriteRule ^/(.*)$ http://127.0.0.1:8040/bv-via-web/$1 [P,L]

But then when I open the root of the website it performs a redirect and http://127.0.0.1:8040/bv-via-web/hola appears in the browser address.

I don't understand, as it should work as an internal proxy due to the [P] flag.
What am I doing wrong ?

I solved it by adding:

ProxyPreserveHost On

Such it's not forwarded to the 127.0.0.1 as mentioned above. So the total config snippet is:

RewriteEngine on
RewriteRule ^/bv-via-web/(.*)$ http://127.0.0.1:8040/bv-via-web/$1 [P]
RewriteRule ^/(.*)$ http://127.0.0.1:8040/bv-via-web/$1 [P]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM