Apache reverse proxy to application on tomcat with 8080 port gives wrong URL in response header

5,827

Solution 1

Update ProxyPassReverse as below.

     ProxyPassReverse /myapp http://ext-domain/myapp

Refer: http://httpd.apache.org/docs/2.2/mod/mod_proxy_ajp.html#usage

Solution 2

OK, the old answer is incomplete. The culprit is: ProxyPreserveHost on. You hardly ever need this clause.

The initial configuration template should therefore be:

....  
ServerName ext-domain  
ProxyPass        /app  http://int-domain:8080/myapp
ProxyPassReverse /app  http://int-domain:8080/myapp
....

The 302 response that startles you is generated by the backend. You've told backend the header Host: ext-domain, this is the effect of ProxyPreserveHost, so backend wants to be nice and obedient and if it need to give you 302 redirect it also uses the same ext-domain as a convenience to you. Backed knows on which port you connected, so it tries to use the same port as a convenience to you. If it's not a convenience, then don't use ProxyPreserveHost in the first place, it's the cleanest and least confusing solution to the problem. Most applications (not all) will not require ProxyPreserveHost, but use the usual X-Forwaded-xxx headers.

The 302 processing is roughly like this:

  • HTTP 302 http://ext-domain:8080/app/foo is generated by the backend
  • Apache checks the configuration for any ProxyPassReverse xxx http://ext-domain:8080/app
  • if matching, Apache continues with HTTP 302 xxx/foo (this step doesn't happen in your case)
  • if xxx is not a full URL, it's expanded with the usual ServerName and similar (so the full meaning of ProxyPassReverse /app42 ... is for Apache to say 302 http://ext-domain/app42)
  • Apache also knows that browser talks to Apache's port 80 so it will not misdirect a browser to 8080 unless you tell it to explicitly with a misconfigured ServerName.
Share:
5,827
Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin over 1 year

    I have Apache (2.2) server with below configuration for reverse proxy to application running on tomcat(6) on 8080 port.

    ....  
    ServerName ext-domain  
    ProxyPreserveHost On  
    ProxyPass  /myapp http://int-domain:8080/myapp  
    ProxyPassReverse /myapp http://int-domain:8080/myapp  
    ....  
    

    When I access below URL from browser

    "http://ext-domain/myapp"  
    

    I get below URL replaced in browser address bar.

    "http://ext-domain:8080/myapp"  
    

    And apache access log says...

    "GET /myapp HTTP/1.1" 302 421 "-" "Mozilla/5.0"...    
    

    How to avoid this port 8080 insertion in response URL?
    Can someone help on this? thanks.

  • BTR Naidu
    BTR Naidu over 7 years
    Ho NO. removing ProxyPreserveHost on was a bad idea. I am running my mmonit server in a docker container and if I remove that flat, my url becomes 192.168.2.37:8080/index.csp. This is not what was expected.
  • Naveed Abbas
    Naveed Abbas over 7 years
    I would apply an additional ProxyPassReverse / http://192.168.2.37:8080/ to workaround that weird "feature" of the application (I mean a weird redirect). And the problem is?...