What’s wrong with the VirtualHost

apache-2.2virtualhost

I have the following VirtualHost

// filename: /etc/apache2/sites-available/ccbbbcc

<VirtualHost 1.1.1.1:80>
     ServerAdmin utopia@ccbbbcc.com
     ServerName ccbbbcc.com
     ServerAlias www.ccbbbcc.com
     DocumentRoot /srv/www/ccbbbcc/production/public_html/
     ErrorLog /srv/www/ccbbbcc/production/logs/error.log
     CustomLog /srv/www/ccbbbcc/production/logs/access.log combined
</VirtualHost>

And then I also have

//filename: /etc/apache2/sites-available/default

<VirtualHost 1.1.1.1:80>
ServerAdmin webmaster@localhost

        DocumentRoot /var/www/
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
blah blah blah

How come when I type into my browser http://1.1.1.1, it takes me to http://ccbbbcc.com ? Even when I point new urls to the IP 1.1.1.1, webpages serve from http://ccbbbcc.com. Why am I unable to serve pages from /var/www directory?

Additional symptoms – the ccbbbcc vhost overrides only SOME of my other vhosts. Not all.

Additional Notes

I've made sure to use a2ensite and to restart apache.

This is what my /etc/apache2/ports.conf looks like

NameVirtualHost 1.1.1.1:80
Listen 80
Listen 443

I made a file called /srv/www/ccbbbcc/production/public_html/test.html with the text "Hello World". When I type http://ccbbbcc.com/test.html, the page loads fine. When I type http://1.1.1.1/test.html, I get a 404 Page Not Found. When I type http://1.1.1.1/, the web browser refreshes and then displays the url http://ccbbbcc.com. I then append test.html to the end of the url, and the Hello World shows up again.

So does that mean there's an http redirect happening somewhere? If so, I can't seem to determine what's causing it.

Also, i notice ccbbbcc overrides SOME of my virtualhosts, not ALL. There doesn't seem to be a pattern to which vhost is discriminated/overridden.

I also noticed that if I do the following

1. a2dissite ccbbbcc
2. /etc/init.d/apache2 reload
3. a2ensite ccbbbcc
4. /etc/init.d/apache2 reload

Then all of a sudden, http://ccbbbcc.com serves pages from /var/www/, and http://1.1.1.1 also serves pages from /var/www. How come the a2ensite/a2dissite of affects where pages are loaded from? The only way to make ccbbbcc serve pages from /srv/www/cbs/production again is to reboot my entire linux server! What's going on?

Best Answer

As you can read in An In-Depth Discussion of Virtual Host Matching:

The first vhost on this list (the first vhost in the config file with the specified IP address) has the highest priority and catches any request to an unknown server name or a request without a Host: header field.

Your Apache httpd loads and evaluates the file /etc/apache2/sites-available/ccbbbcc before /etc/apache2/sites-available/default.

Rename /etc/apache2/sites-available/default for example to /etc/apache2/sites-available/00_default and /etc/apache2/sites-available/ccbbbcc to /etc/apache2/sites-available/01_ccbbbcc, so that the default file is loaded first.

Related Topic