Saturday, April 15, 2017

ubuntu - Apache2 proxy preserve domain name

I'm trying to implement the scheme Browser Client -> Apache2 Proxy -> Tomcat Application Server. Apache2 and Tomcat on separate servers. But the proxy does not work as I expected.

Apache2 virtual host setting:



  
ServerName example.com
ServerAlias www.example.com

ProxyPass /MyApp http://tomcatdomain.com/MyApp
ProxyPassReverse /MyApp tomcatdomain.com/MyApp




if I make a request to open the page in the browser, http://example.com/MyApp, the application opens correctly, but the URL is different - http://tomcatdomain.com/MyApp.
Next, I look at the Ajax request and see that it does not work according to the scheme I expected:



 12:35:20.537 GET https://example.com/MyApp/service/test [HTTP/1.1 302  41ms]
12:35:20.617 GET https://tomcatdomain.com/MyApp/service/test


Expected: [request] client->apache2->tomcat [response] tomcat->apache2->client




Actually: [request] client->apache2 [response] apache2->client [request2] client ->tomcat [response2] tomcat -> client



My first question is how to make the client receive a response from the tomkat with one query?



The next problem with the ProxyPreserveHost parameter - I need to keep the original url(example.com) when opening the application (not tomcatdomain.com). I append ProxyPreserveHost to the appache2 setting:




ServerName example.com
ServerAlias www.example.com


ProxyPreserveHost On

ProxyPass /MyApp http://tomcatdomain.com/MyApp
ProxyPassReverse /MyApp tomcatdomain.com/MyApp



I also prepared the tomkat server.xml:



   

www.example.com




I make a request and what I see in the browser:



The page isn’t redirecting properly
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.




I make AJAX request and I see 22 identical requests that are not answered:



 12:54:48.020 GET https://example.com/MyApp/service/test [HTTP/1.1 302  28ms]
12:54:48.042 GET https://example.com/MyApp/service/test [HTTP/1.1 302 4ms]
... 22 requests!
12:54:48.367 GET https://example.com/MyApp/service/test [HTTP/1.1 302 3ms]


I conclude that the request is not redirected to the tomcat server.




To confirm my guesses, I corrected the Apache2 settings:



  
ServerName example.com
ServerAlias www.example.com

ProxyPreserveHost On

ProxyPass /MyApp http://tomcatdomain.com/MyApp**ABCD**
ProxyPassReverse /MyApp tomcatdomain.com/MyApp**ABCD**




And in browser I see:
Not Found
The requested URL /MyAppABCD was not found on this server.
Apache/2.4.27 (Ubuntu) Server at example.com Port 80



Apache2 searches for URL mapping not on tomcat, but on the same apache2?




Tell me, please, how to implement the scheme, when the browser open the page, the data will be received from Tomcat via Apache2 proxy, and the original URL will be saved? Thanks.

No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...