Search code examples
apachereverse-proxyvirtual-hosts

All requests being send to first(default) virtual host


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.


Solution

  • 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.