1

We are trying reverse-proxy between apache 2.2.22 and tomcat-7. The entires in my httpd.conf files are as below:

NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /apps/httpd-2.2.22/docroot/app1/
ServerName app1.mycompany.com
ErrorLog logs/app1.mycompany.com.error_log
CustomLog logs/app1.mycompany.com.access_log common
......
.........
</VirtualHost *.80>

<VirtualHost *:80>
DocumentRoot /apps/httpd-2.2.22/docroot/app2/
ServerName app2.mycompany.com
ErrorLog logs/app2.mycompany.com.error_log
CustomLog logs/app2.mycompany.com.access_log common
....
......
</VirtualHost *.80>

As am still waiting to get the SSL certs, just wanted to check HTTP requests for now. I am just giving the IP in browser to check everything works.

Requests from browser for both applications are landing on first virtual host.

http://1.2.3.4/app1  (Gives me the correct site)

http://1.2.3.4/app2  (Gives me wrong, as docroot it reads is of 1st host)

If i remove the app1 virtual host entry then http://1.2.3.4/app2 works fine.

We already have the same setup running in older server, so am not giving the url as http://app1.mycompany.com/app1 (by giving server name instead of IP). We need to replace it once we get this server setup up and running well.

  1. When i give the old server's IP in browser, the applications turns up fine . Why its failing for this server? Do i need some extra setup for this?

  2. How the ServerName maps up?

Forgot to mention that app1 and app2 are running on tomcat server which are at say machine A, and apache web server is on machine B. Do i need to do some configuration for this? Already the tomcat server details are there in worker.properties.

pranav
  • 421
  • 1
  • 11
  • 27

2 Answers2

1

If you are using name-based virtual hosts, the ServerName inside a section specifies what hostname must appear in the request's Host: header to match this virtual host.

Source: https://httpd.apache.org/docs/2.4/mod/core.html#servername

The domain name that you enter in the browser is sent to the server as a HTTP request header named "Host". The server will select the correct virtual host configuration to use for each request by matching the ServerName value and the value present in the "Host" HTTP request header.

If no matching ServerName or ServerAlias is found in the set of virtual hosts containing the most specific matching IP address and port combination, then the first listed virtual host that matches that will be used.

Source:https://httpd.apache.org/docs/2.4/vhosts/name-based.html

Currently, the server is receiving the IP "1.2.3.4" in the Host header, instead of the domain "app2.mycompany.com", so it serving the content based on the configuration in the first virtual host.

Solution: Test your new server, by modifying the hosts file only on your local machine. Add the following lines in the hosts file:

1.2.3.4 app1.mycompany.com
1.2.3.4 app2.mycompany.com

Now enter http://app2.mycompany.com in the browser on your local machine and the DNS resolution for "app2.mycompany.com" will return the IP of your new server. So, only on your local machine, the content will be fetched from the new server. For the rest of the world, the content will still be fetched from the old server. The hosts file is located under the path /etc/hosts in Linux and C:\Windows\System32\drivers\etc\hosts in Windows.

Nidhi
  • 858
  • 4
  • 9
  • Did that. Still the same error. Ran dig as dig app2.mycompany.com. Got **NXDOMAIN** as status. nslookup app2.mycompany.com gives the same-: ** server can't find app2.mycompany.com: NXDOMAIN. The /etc/hosts file has the entries with 127.0.0.1 , 1.2.3.4 and 8.8.8.8 too. Being a developer am not sure what else am missing. – pranav Mar 31 '16 at 08:45
  • Forgot to mention the app1 and app2 are running on Tomcat servers which are in say machine A and Apache web server is in machine B. Do i need to set up some configuration for this in some linux system files. The tomcat server entries are already there in worker.properties file. – pranav Mar 31 '16 at 09:06
  • One more update, am able to **ping http://app2.mycompany.com** from my system, though access from browser still gives error 400. – pranav Mar 31 '16 at 14:13
  • 1. When you ping app2.mycompany.com from your machine, is it resolving to the new server's IP that you have entered in your hosts file? 2. After adding the new server's IP in the hosts file, browse app2.mycomapny.com in Firefox. What is the value of "Remote Address"? You can find it under the "Headers" section, in the "Network" tab in Developer Tools. – Nidhi Mar 31 '16 at 19:49
  • Which Apache module are you using to forward requests from apache to tomcat? – Nidhi Mar 31 '16 at 21:04
  • My mistake, i was editing my apache server host file. IT team confirmed that they will put the DNS entries in Linux server(which has Apache) and it will work. Nidhi am using Apache mod_jk connector. Thanks for helping out!! – pranav Apr 01 '16 at 10:35
  • Done and Thanks !! – pranav Apr 01 '16 at 10:39
0

I got it sorted out. Gave the hostname entries in my windows host.ini file, then was able to access the apps. IT team confirmed that they will put the DNS entries in Linux server(which has Apache) and it will work. Nidhi am using Apache mod_jk connector. Thanks!!

pranav
  • 421
  • 1
  • 11
  • 27