Skip to content

Apache and how to correctly use NameVirtualHost

As I often get involved in server administration (SVN, git, redmine, etc setup), I deal with Apache on a regular basis.  For simple configurations, the default Ubuntu/debian config works well.  However, for a more complex setup with virtual hosts, multiple IP addresses, and SSL support, it is common to run into the following message:

mixing * ports and non-* ports with a NameVirtualHost address is not supported, proceeding with undefined results

Googling does not return a clear explanation to this problem, so that is why I’m writing this article. The impulsive approach of randomly changing things does not get you too far — especially with the Debian/Ubuntu Apache configuration that is composed of seperate files for each virtual host.  After reading the documentation, things become clearer:

Note that the argument to the <VirtualHost> directive must exactly match the argument to the NameVirtualHost directive.

And, by considering the fact that the seperate virtual host config files really just get concatenated into one big file, it makes sense that you can’t have more than one NameVirtualHost entry.  Ubuntu by default puts the NameVirtualHost in the default virtual host file, which gets soft linked to sites-enabled/000-default.  I presume the ‘000’ means it gets loaded first.  I find it much more intuitive to put the NameVitualHost directives in the /etc/apache2/httpd.conf file.  This way it is clear that these entries are common to all virtual host entries.  So what I end up with is:

/etc/apache2/httpd.conf

NameVirtualHost 74.208.184.45:80
NameVirtualHost 74.208.184.46:80
NameVirtualHost 74.208.184.45:443

/etc/apache2/sites-available/bec-systems.com

<VirtualHost 74.208.184.45:80>
        ServerName bec-systems.com
...

/etc/apach2/sites-available/bec-systems.com.ssl

<VirtualHost 74.208.184.45:443>
        ServerName bec-systems.com
...

/etc/apach2/sites-available/svn.bec-systems.com

<VirtualHost 74.208.184.45:80>
        ServerName svn.bec-systems.com
...

/etc/apach2/sites-available/site2.com (with different IP address)

<VirtualHost 74.208.184.46:80>
        ServerName site2.com
...

Again, the points to remember are:

  1. The argument to VirtualHost must match a NameVirtualHost directive
  2. the NameVirtualHost directive is common, so there should only be one entry

8 thoughts on “Apache and how to correctly use NameVirtualHost”

  1. Thank you so much for this simple example. This particular issue has been bugging me for quite some time.

  2. I’m confused. First you seem to say that you have three “entries” for NameVirtualHost in your httpd.conf

    /etc/apache2/httpd.conf
    NameVirtualHost 74.208.184.45:80
    NameVirtualHost 74.208.184.46:80
    NameVirtualHost 74.208.184.45:443

    Then at the end your second point to remember is that there should only be one entry.

  3. Let me clarify–there should only be one NameVirtualHost per IP address and port combination. Notice in the above list, there are three separate IP address/port combinations. It is often common to bind multiple sites to a single IP address, which is where you run into this issue. For example, svn.bec-systems.com, git.bec-systems.com, http://www.bec-systems.com all resolve to a single IP.

  4. Whenever I read through the Apache docs, my eyes go into a spiral and I’m unable to grok things that are there. If only they were as well written as this. Your post helped me to get two additional IP addresses working on our server, but more importantly to understand the NameVirtualHost directive. Thanks.

  5. Thanks! This helped a bunch.

    To sumarize what I did, just to make it easier for others coming here later, I added

    NameVirtualHost 11.22.33.44:80

    to /etc/apache2/httpd.conf

    then changed

    in files in /etc/apache2/sites-available/

    to be

    and made sure

    ServerName website.address

    was there too.

Comments are closed.