Search code examples
apachednshttpd.confvhostsvirtual-hosts

Name Based Virtual Hosts on Apache with 2 IP Address


I have 2 IP addresses. It is basically for SSL but let's assume that I want example1.com and example3.com work at IP 1.1.1.1 and example2.com works at IP 2.2.2.2.

I should be missing something, somewhere. I have basically following settings related with this in the configuration files - I did read and apply the same thing in Apache documentation as well as many other sources.

Listen 80

<VirtualHost *:80>
        ServerName example1.com
        # ...
</VirtualHost>

<VirtualHost *:80>
        ServerName example3.com
        # ...
</VirtualHost>

<VirtualHost 2.2.2.2:80>
        ServerName example2.com
        # ...
</VirtualHost>

I have example2.com pointed to 2.2.2.2 and and the others to 1.1.1.1 in DNS records.

I can go to example1.com and example3.com successfully but example2.com goes to default server, which is example1.com for this sample since it was entered first in the configuration.

Another weirdness: keeping DNS same for example2.com but change vhost config to be *:80 and it opens the website and server ip is reported as 1.1.1.1 as I can get from the web script to see what's going on.

And another one: Even I keep only example2.com domain in the configuration and remove others, it still doesn't work with the IP. As I tried to mention, if I enter 2.2.2.2, it successfully access to the server via this IP.

Could someone please help me to solve this problem which is obviously caused by something I am doing wrong or missing.

Thank you.


Solution

  • I had DHCP enabled in the server. I changed it to be static networking and it all worked.

    I edited /etc/sysconfig/network-scripts/ifcfg-eth0 as necessary to add static IPs and disabled Auto Configure Network option for my server. It is a Linode server and required settings explained here - just in case someone else needs: https://www.linode.com/docs/networking/linux-static-ip-configuration