Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have multiple domains linked via A-Record to my server. For all of them I created a virtualhost. Some of them run with and without of www. Some have additional subdomains and some have wildcard (sub)domains. All runs pretty fine, except one thing.

When I surf to my server by using the servers ip adress, I don't get to the default host (/var/www) but to one of the other virtual hosts. This is because the ip address doesn't match any of the vhosts and then will use the alphabetically first defined vhost. So far, ok.

But now I wan't that the ip address matches the default host and not the other vhost. So I renamed the "default" vhost file to "aaa-default". Everything runs fine. The direct ip access matches the default host. All other domains match their vhost except one.

The vhost for the domain code-bude.net matches perfect for all cases except the access without www.

Problem/Question But when the default was named default http://code-bude.net worked. So why doesn't it work, when the default is the first host?

Note: At the moment default is named default, because otherwise my blog (code-bude.net) wouldn't be reachable. If you now go to the server by using the ip (37.221.194.133) you will see, that it matches blitzartig.net and not the default server page (The one which is also shown if you navigate to admin.code-bude.net).

The "aaa-default/default" vhost file:

<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www
<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>
<Directory /var/www/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all
</Directory>

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Order allow,deny
    Allow from all
</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog ${APACHE_LOG_DIR}/access.log combined

Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
    Options Indexes MultiViews FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from all
    Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>

The "code-bude.net" vhost file:

<VirtualHost *:80>
    ServerName admin.code-bude.net
    DocumentRoot /var/www
</VirtualHost>

<VirtualHost *:80>
    ServerName dev.code-bude.net
    DocumentRoot /var/www/dev
</VirtualHost>

<VirtualHost *:80>
    ServerAdmin webmaster@code-bude.net
    ServerName code-bude.net
    ServerAlias www.code-bude.net *.code-bude.net
    DocumentRoot /var/www/code-bude.net/public_html
    <Directory />
        Options FollowSymLinks
        AllowOverride All
    </Directory>
    <Directory /var/www/code-bude.net/public_html>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

The "blitzartig.net" vhost file (this is the one which is the first when default is named default and not aaa-default):

<VirtualHost *:80>
ServerAdmin kontakt@blitzartig.net
ServerName blitzartig.net
ServerAlias www.blitzartig.net
DocumentRoot /var/www/blitzartig.net/public_html
<Directory />
    Options FollowSymLinks
    AllowOverride All
</Directory>
<Directory /var/www/blitzartig.net/public_html>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all
</Directory>

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Order allow,deny
    Allow from all
</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog ${APACHE_LOG_DIR}/access.log combined

Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
    Options Indexes MultiViews FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from all
    Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>

apachectl -S output when default named as default:

VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
*:80                   is a NameVirtualHost
         default server code-bude.net (/etc/apache2/sites-enabled/000-default:1)
         port 80 namevhost code-bude.net (/etc/apache2/sites-enabled/000-default:1)
         port 80 namevhost blitzartig.net (/etc/apache2/sites-enabled/blitzartig.net:1)
         port 80 namevhost admin.code-bude.net (/etc/apache2/sites-enabled/code-bude.net:1)
         port 80 namevhost dev.code-bude.net (/etc/apache2/sites-enabled/code-bude.net:6)
         port 80 namevhost code-bude.net (/etc/apache2/sites-enabled/code-bude.net:11)
         port 80 namevhost derwirtschaftsinformatiker.de (/etc/apache2/sites-enabled/derwirtschaftsinformatiker.de:1)
         port 80 namevhost raffaelherrmann.de (/etc/apache2/sites-enabled/raffaelherrmann.de:1)
         port 80 namevhost wortkonstrukte.net (/etc/apache2/sites-enabled/wortkonstrukte.net:1)
Syntax OK

apachectl -S output when default named as aaa-default:

VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
*:80                   is a NameVirtualHost
         default server code-bude.net (/etc/apache2/sites-enabled/aaa-default:1)
         port 80 namevhost code-bude.net (/etc/apache2/sites-enabled/aaa-default:1)
         port 80 namevhost blitzartig.net (/etc/apache2/sites-enabled/blitzartig.net:1)
         port 80 namevhost admin.code-bude.net (/etc/apache2/sites-enabled/code-bude.net:1)
         port 80 namevhost dev.code-bude.net (/etc/apache2/sites-enabled/code-bude.net:6)
         port 80 namevhost code-bude.net (/etc/apache2/sites-enabled/code-bude.net:11)
         port 80 namevhost derwirtschaftsinformatiker.de (/etc/apache2/sites-enabled/derwirtschaftsinformatiker.de:1)
         port 80 namevhost raffaelherrmann.de (/etc/apache2/sites-enabled/raffaelherrmann.de:1)
         port 80 namevhost wortkonstrukte.net (/etc/apache2/sites-enabled/wortkonstrukte.net:1)
Syntax OK
share|improve this question
    
Can you grab the output of apachectl -S? –  Shane Madden Apr 12 '13 at 5:41
    
@ShaneMadden No problem. I've edited the question, because the output was to long for a comment. Could the behaviour result from my hostname? If I execute the "hostname" command in shell I get "code-bude.net" as hostname... –  netblognet Apr 12 '13 at 6:02

2 Answers 2

up vote 2 down vote accepted

Oh, I see. You have no ServerName directive in your default <VirtualHost>. It needs to have a ServerName, or one will be generated for it (either inherited from the main configuration or guessed via reverse DNS lookup). Note that this doesn't create a conflict with the www name, since it's just taking precedence for the exact name.

Just give it something nonsense like ServerName catchall and it'll stop taking requests that are bound for code-bude.net.

share|improve this answer
    
Ok, I'll try that, but I'm a little bit confused, because everywhere in the internet they say "don't set up a ServerName for the default vhost, then it will catch all...". –  netblognet Apr 12 '13 at 6:40
    
Ok, that worked. Thank you very, very much! But nevertheless I'm a little bit confused. The my above comment. –  netblognet Apr 12 '13 at 6:47
    
@netblognet Maybe just a case of poor wording -- where have you seen that? –  Shane Madden Apr 12 '13 at 19:50

When you setup your apache configuration as I have done, when no match to ServerName is found, it hosts the first VirtualHost declaration.
The website which apache will serve is defined by the browser HOST header.

/etc/apache2/sites-enabled/000-default

<VirtualHost *:80>
    ServerAdmin admin@alpha.com
    ServerName www.alpha.com
    DocumentRoot /var/www/alpha.com
</VirtualHost>  

<VirtualHost *:80>
    ServerAdmin beta@def.com
    ServerName www.beta.com
    DocumentRoot /var/www/beta.com
</VirtualHost>  


<VirtualHost *:80>
    ServerAdmin admin@charlie.com
    ServerName www.charlie.com
    DocumentRoot /var/www/charlie.com
</VirtualHost> 

You may override the default site if your ip is static, which it most likely is. Lets assume your ip is 8.8.8.8
Add the following VirtualHost to serve a new configuration when accessing the server by ip.

<VirtualHost *:80>
    ServerAdmin admin@somedomain.com
    ServerName 8.8.8.8
    DocumentRoot /var/www/somedomain.com
</VirtualHost>   

Which may be a duplication of another configuration.

The following has not been tested but should do the serve the same function. Lets assume you want www.beta.com to be the default site when accessing via the ip. Insert a ServerAlias where your ip is again assumed to be 8.8.8.8

<VirtualHost *:80>
    ServerAdmin admin@beta.com
    ServerName www.beta.com
    ServerAlias 8.8.8.8
    DocumentRoot /var/www/beta.com
</VirtualHost>  

I hope this answers your question, I am not familiar with the method in which you have used to setup your apache server. This is the setup which I use.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.