It's the following part of a virtual host config that I need further clarification on:

<VirtualHost *:80>
  # Admin email, Server Name (domain name), and any aliases
  ServerAdmin example@example.com
  ServerName  141.29.495.999
  ServerAlias example.com

This is and example config, similar to what I currently have (I don't have a domain name at the moment).

<VirtualHost *:80> - Allow the following settings for all HTTP requests made on port 80 to IPs that this server can be contacted on. For instance, if the server could be accessed on more than one IP, you could restrict this directive to just one instead of both.

ServerName - If the host part of the HTTP request matches this name, then allow the request. Normally this would be a domain name that maps to an IP, but in this case the HTTP request host must match this IP.

ServerAlias - Alternate names accepted by the server.

The confusing part for me is, in the above scenario, if I set ServerAlias mytestname.com and then made an HTTP request to mytestname.com, there would have to be a DNS record pointing to the server's IP for this to work? In which case, is ServerAlias just basically EXTRA ServerName entries?

Say I had a DNS entry such that foobar.com = 141.29.495.999 but then I had ServerName = 141.29.495.999 and ServerAlias was empty, would that mean that although foobar.com gets resolved to the right IP, because there is no reference to accept foobar.com in ServerName or ServerAlias?

Or something. Man I'm confused.

Jenny D
  • 27,780
  • 21
  • 75
  • 114
  • 1,102
  • 2
  • 8
  • 7

4 Answers4


Think of it like this:

DNS is the phone directory/yellow pages. When someone wants to call your phone, they can look up your name and get your phone number and call that phone. DNS does the same but for computers - when someone wants to go to www.example.com they ask DNS for the IP address and then they can contact the computer that has that IP address. That is what resolve means. Resolving an IP address has nothing at all to do with Apache; it is strictly a DNS question.

The ServerName and ServerAlias is more like a company's internal phone list. Your webserver is the switchboard; it will accept all incoming connections to the server. Then the client/caller will tell them what name they're looking for, and it will look in the Apache configuration for how to handle that name.

If the name isn't listed as a ServerName/ServerAlias in the apache configuration, apache will always give them the first VirtualHost listed. Or, if there's no VirtualHost at all, it will give the same content no matter what hostname is given in the request.

ETA: So, step by step for a normal connection:

  1. You type http://www.example.com into your browser.
  2. Your computer asks its DNS resolver which IP address it should use when it wants to talk to www.example.com.
  3. Your computer connects to that IP address, and says that it wants to talk to www.example.com (that's the Host:header in HTTP).
  4. The webserver looks at its configuration to figure out what to do with a request for content from www.example.com. Any one of the following may happen:
    • www.example.com is listed as a ServerName or ServerAlias for a VirtualHost - if so, then it will use the configuration for that VirtualHost to deliver the content.
    • The server doesn't have any VirtualHosts at all - if so, then it will use the configuration in its httpd.conf to deliver the content.
    • The server has VirtualHosts but www.example.com isn't listed in any of them - if so, the first Virtualhost in the list will be used to deliver the content.
Jenny D
  • 27,780
  • 21
  • 75
  • 114
  • So, if the host part of an HTTP request is `www.example.com`, the DNS will resolve the IP address for that name, and the HTTP request host part will actually be an IP; upon reaching the server, apache will also know that the request asked for `www.example.com` and if there's a `ServerName` or `ServerAlias` directive for that domain name, it can serve the document root? – njp Jul 02 '13 at 14:26
  • No - the HTTP request part is still the hostname. I'll clarify a little more. – Jenny D Jul 02 '13 at 14:45
  • 1
    `The server has VirtualHosts but www.example.com isn't listed in any of them - if so, the first Virtualhost in the list will be used to deliver the content.`. I tested this. It is right but I am not sure it is first Virtualhost. Thank you for information. +1 – S.M.Mousavi Apr 27 '16 at 11:43

If you don't define ServerName, then apache2 will try to guess it from /etc/hosts. ServerAlias is optional. The most usual use case is where

ServerName example.com
ServerAlias www.example.com

It's up to you (not apache2's job) to make sure that requests reach the webserver IP, e.g, registering a domain and setting up DNS records. A very common way to do so (for test & development) without domain and dns records is to set your local machine /etc/hosts file so that example.com points to the IP of your server. example.com www.example.com
  • 738
  • 4
  • 9
<VirtualHost *:80>
    ServerName influenceknowledge.test
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/work-force/public
    ServerAlias influenceknowledge.test *.influenceknowledge.test
    <Directory /var/www/html/influence-knowledge/public >
       AllowOverride All
           Require all granted
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
  • 133,124
  • 18
  • 176
  • 300

I would strongly suggest to read the official documentation on the topic : https://httpd.apache.org/docs/2.4/vhosts/name-based.html

it says "If no matching ServerName or ServerAlias is found in the set of virtual hosts containing the most specific matching IP address and port combination, then the first listed virtual host that matches that will be used." . So in your case if you access "foobar.com", the first VirtualHost entry who somehow matches the IP 141.29.495.999 would be used by httpd

The same document says "Many servers want to be accessible by more than one name. This is possible with the ServerAlias directive,". So you can regard ServerAlias simply as additional ServerName entries.

  • I'm pretty sure that's not correct. If there isn't a `ServerName` or `ServerAlias` for **foobar.com**, it will use the first `VirtualHost` in the configuration, **even** if foobar.com resolves to 141.29.495.999, and there's a later VirtualHost matching 141.29.495.999 – Auspex Oct 17 '22 at 14:09