Virtual Servers not answering correctly on multiple IPs

Discussion in 'Apache' started by entilza, Jul 17, 2012.

  1. #1
    Hi all,

    I have a problem where a second "virtual" IP address has been introduced on the same physical NIC as the old IP address. Previously I was running multiple virtual apache servers without incident on the one IP. Now I am trying to add new virtual servers to the new IP (keeping the old servers on the old IP), but the wrong sites are coming up for some domain names once the new IP is used.

    For example, I have been running the following virtual config for multiple virtual servers without issue. We'll call this the "OLD" config, as it is known to be working. In this example the old virtual server is named www.test.com, with an IP of 111.111.111.111 (fake details for this post):

    
    <VirtualHost *:80>
            ServerAdmin test@test.com
    
    
            DocumentRoot /var/www/test.com/html/
            ServerName www.test.com
    
    
            # To redirect all users to access the site WITH the www. prefix,
            # (http://example.com/... will be redirected to http://www.example.com/...)
            # adapt and uncomment the following:
    
    
            RewriteEngine On
            RewriteCond %{HTTP_HOST} ^test\.com$ [NC]
            RewriteRule ^(.*)$ http://www.test.com$1 [L,R=301]
    
    
            <Directory />
                    Options FollowSymLinks
                    AllowOverride All
            </Directory>
            <Directory /var/www/test.com/html>
                    Options FollowSymLinks MultiViews
                    AllowOverride All
                    Order allow,deny
                    allow from all
            </Directory>
    
    
            ErrorLog /var/log/apache2/test.com/error.log
    
    
            # Possible values include: debug, info, notice, warn, error, crit,
            # alert, emerg.
            LogLevel warn
    
    
            CustomLog /var/log/apache2/test.com/access.log combined
    </VirtualHost>
    
    
    <VirtualHost *:80>
            ServerAdmin test@test.com
    
    
            DocumentRoot /var/www/test.com/html/
            ServerName test.com
    
    
            # To redirect all users to access the site WITH the www. prefix,
            # (http://example.com/... will be redirected to http://www.example.com/...)
            # adapt and uncomment the following:
    
    
            RewriteEngine On
            RewriteCond %{HTTP_HOST} ^test\.com$ [NC]
            RewriteRule ^(.*)$ http://www.test.com$1 [L,R=301]
    
    
            <Directory />
                    Options FollowSymLinks
                    AllowOverride All
            </Directory>
            <Directory /var/www/test.com/html>
                    Options FollowSymLinks MultiViews
                    AllowOverride All
                    Order allow,deny
                    allow from all
            </Directory>
    
    
            ScriptAlias /cgi-bin/ /var/www/test.com/cgi-bin/
            <Directory "/var/www/test.com/cgi-bin">
                    AllowOverride None
                    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                    Order allow,deny
                    Allow from all
            </Directory>
    
    
            ErrorLog /var/log/apache2/test.com/error.log
    
    
            # Possible values include: debug, info, notice, warn, error, crit,
            # alert, emerg.
            LogLevel warn
    
    
            CustomLog /var/log/apache2/test.com/access.log combined
    </VirtualHost>
    
    Code (markup):

    Now that I have the new (second) IP address, I have configured the DNS entry my new domain name to point to the new server IP (www.newtest.com, IP 222.222.222.222), with the following virtual config. I figure that the apache virtual servers can answer on any IP, but they'll pick up which site to show via the domain name used:

    
     <VirtualHost *:80>
            ServerAdmin info@newtest.com
    
    
            DocumentRoot /var/www/newtest.com/html/
            ServerName www.newtest.com
    
    
            # To redirect all users to access the site WITH the www. prefix,
            # (http://example.com/... will be redirected to http://www.example.com/...)
            # adapt and uncomment the following:
    
    
            RewriteEngine On
            RewriteCond %{HTTP_HOST} ^newtest\.com$ [NC]
            RewriteRule ^(.*)$ http://www.newtest.com$1 [L,R=301]
    
    
            <Directory />
                    Options FollowSymLinks
                    AllowOverride All
            </Directory>
            <Directory /var/www/newtest.com/html>
                    Options FollowSymLinks MultiViews
                    AllowOverride All
                    Order allow,deny
                    allow from all
            </Directory>
    
    
            ErrorLog /var/log/apache2/newtest.com/error.log
    
    
            # Possible values include: debug, info, notice, warn, error, crit,
            # alert, emerg.
            LogLevel warn
    
    
            CustomLog /var/log/apache2/newtest.com/access.log combined
    </VirtualHost>
    
    Code (markup):
    But when I run the above, all the sites on the OLD IP are replaced by the same single site, being the first site configured by the server on that IP. To be clear, 7 "old" sites are replaced by duplicates of 1 "old" site. The NEW site is also a duplicate of the same one old site too.

    So, I instead direct the NEW server to respond only on one IP address

    
    <VirtualHost 222.222.222.222:80>
            ServerAdmin info@newtest.com
    
    
            DocumentRoot /var/www/newtest.com/html/
            ServerName www.newtest.com
    
    
            # To redirect all users to access the site WITH the www. prefix,
            # (http://example.com/... will be redirected to http://www.example.com/...)
            # adapt and uncomment the following:
    
    
            RewriteEngine On
            RewriteCond %{HTTP_HOST} ^newtest\.com$ [NC]
            RewriteRule ^(.*)$ http://www.newtest.com$1 [L,R=301]
    
    
            <Directory />
                    Options FollowSymLinks
                    AllowOverride All
            </Directory>
            <Directory /var/www/newtest.com/html>
                    Options FollowSymLinks MultiViews
                    AllowOverride All
                    Order allow,deny
                    allow from all
            </Directory>
    
    
            ErrorLog /var/log/apache2/newtest.com/error.log
    
    
            # Possible values include: debug, info, notice, warn, error, crit,
            # alert, emerg.
            LogLevel warn
    
    
            CustomLog /var/log/apache2/newtest.com/access.log combined
    </VirtualHost>
    
    Code (markup):


    The above returns the OLD sites back to their normal use, but the NEW site on the NEW IP address still shows the first configured site from the OLD IP address.

    So I figured perhaps I needed to explicitly configure the OLD IP address for the OLD websites instead of *, but again, the wrong site appears for the old domain names.

    Any hints on where I may be going wrong? Thanks in advance.

    Version info:
    Apache/2.2.14 (Ubuntu)
     
    entilza, Jul 17, 2012 IP
  2. Ray Baron

    Ray Baron Member

    Messages:
    148
    Likes Received:
    10
    Best Answers:
    3
    Trophy Points:
    43
    #2
    Have you defined NameVirtualHost?

    You should be able to find a solution to this problem here:

    http:// httpd.apache.org/docs/2.0/vhosts/examples.html
     
    Ray Baron, Jul 17, 2012 IP
  3. entilza

    entilza Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi Ray,

    Thanks for taking the time to respond.

    I have NameVirtualHost * set in ports.conf, as that is were the command natively appeared in the default apache config.

    I've also read the vhosts examples doco from apache (as linked) and could not find issue with my config.

    Thanks,
    Ent.
     
    entilza, Jul 17, 2012 IP
  4. Ray Baron

    Ray Baron Member

    Messages:
    148
    Likes Received:
    10
    Best Answers:
    3
    Trophy Points:
    43
    #4
    Hi Ent,

    I only manage one Ubuntu server (normally work with CentoOS) and it has NameVirtualHost in httpd.conf. You might try adding it to your httpd.conf and commenting out the configuration in ports.conf. NameVirtualHost should go right above the VirtualHost containers.

    If that doesn't work, I would suggest the Ubuntu forums (ubuntuforums.org) as a more authoritative source.
     
    Ray Baron, Jul 17, 2012 IP
  5. entilza

    entilza Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hi all - I solved this issue.

    The problem was caused by two misconfigurations acting together.

    1. I have several sites enabled (under ubuntu, there is a linked config folder called sites-enabled which has a separate virtual config file per enabled site). On the old original IP, I had missed that one site (the one that kept asserting itself over the top of the other sites) had two configuration files, not one. Upon removing the second config file, it ceased asserting itself over the top of the other sites on the old original IP.

    2. The 2 new sites on the new IP address still had a problem with one site asserting itself over both. I re-examined the configs and found the site that was being missed had it's IP address configure in the virtualhost line due to numerous changes being made in testing, eg

    I incorrectly had the actual IP address hardcoded (not a real IP below)
    <VirtualHost 111.222.333.444:80>
    
    Code (markup):
    when I wanted
    <VirtualHost *:80>
    
    Code (markup):
    When I correctly set it to <VirtualHost *:80>, and after I'd corrected the mistake with the double config on the old original IP, everything worked fine (ie: both new and old IP addresses serve the correct sites, dependent on which IP the domain name is configured to in DNS).


    Thanks for taking a look and I hope this solution helps someone.

    Ent.
     
    entilza, Jul 21, 2012 IP