Understanding ServerName vs ServerAlias: DNS Resolution and Apache Virtual Host Configuration


3 views

When configuring Apache virtual hosts, two directives often cause confusion: ServerName and ServerAlias. Let's examine their relationship with DNS resolution through concrete examples.

Apache matches incoming requests against virtual hosts using the Host header, not the resolved IP address. Consider this configuration:


  ServerName 141.29.495.999
  ServerAlias example.com
  DocumentRoot /var/www/example

This virtual host will handle:

  • Requests to http://141.29.495.999 (matches ServerName)
  • Requests to http://example.com (matches ServerAlias)

For domain-based matching to work, DNS must resolve to your server's IP. Using your example:

# DNS Records Required:
example.com.    IN A    141.29.495.999
mytestname.com. IN A    141.29.495.999

Without proper DNS records, browsers can't reach your server using those domain names, regardless of your Apache configuration.

Here are three typical setups:

1. IP-Based Access Only


  ServerName 141.29.495.999
  DocumentRoot /var/www/html

2. Single Domain


  ServerName example.com
  DocumentRoot /var/www/example

3. Multiple Aliases


  ServerName primary.com
  ServerAlias secondary.com www.secondary.com *.dev.secondary.com
  DocumentRoot /var/www/multi

Q: Is ServerAlias just extra ServerName entries?

A: Effectively yes - it allows multiple hostnames to map to the same virtual host, with ServerName being the canonical name.

Q: What happens with foobar.com DNS pointing to 141.29.495.999 but not in ServerName/Alias?

A: Apache will either:

  • Serve the default virtual host (first defined if no default)
  • Return 404 if no matching virtual host exists

Imagine this scenario:

# DNS:
app.example.com   IN A 192.0.2.1
api.example.com   IN A 192.0.2.1

# Apache:

  ServerName app.example.com
  ServerAlias *.example.com
  DocumentRoot /var/www/app



  ServerName api.example.com
  DocumentRoot /var/www/api

In this case:

  • api.example.com goes to the API virtual host (exact ServerName match)
  • app.example.com and any other subdomain (*.example.com) go to the app virtual host

When virtual hosts don't match as expected:

  1. Check DNS resolution with dig example.com or nslookup example.com
  2. Verify Apache sees the correct Host header with curl -v http://example.com
  3. Check which virtual host is being used with apachectl -S

Apache's VirtualHost directive operates through a two-step matching process:

  1. First matches the IP:port combination (from <VirtualHost> directive)
  2. Then matches the Host header (via ServerName/ServerAlias)

The ServerName directive specifies the canonical hostname for the virtual host. It serves three key purposes:

ServerName www.example.com
  • Determines the hostname Apache uses in self-referential URLs
  • Acts as the primary matching criterion for HTTP requests
  • Used for redirections if no matching ServerName/ServerAlias is found

ServerAlias allows additional hostnames to match the same VirtualHost:

ServerAlias example.com *.example.com dev.example.*

Key characteristics:

  • Supports wildcards (*) for pattern matching
  • Can match multiple domains in a single directive
  • Doesn't affect canonical URL generation

Your understanding is correct - DNS and VirtualHost configuration are separate but complementary:

# DNS Record:
foo.example.com. IN A 192.0.2.1

# Apache Config:
<VirtualHost *:80>
    ServerName primary.example.com
    ServerAlias foo.example.com
    # Config here...
</VirtualHost>

Both must align for proper functionality:

  1. DNS must resolve the hostname to the server's IP
  2. Apache must recognize the hostname in ServerName/ServerAlias

Basic Setup:

<VirtualHost *:80>
    ServerName maindomain.com
    ServerAlias www.maindomain.com
    DocumentRoot /var/www/main
</VirtualHost>

Wildcard Usage:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias *.example.com
    # Matches dev.example.com, test.example.com, etc.
</VirtualHost>

IP-Based Fallback:

<VirtualHost 192.0.2.1:80>
    ServerName ""
    # Catches all requests to this IP not matching other vhosts
</VirtualHost>

Problem: Domain resolves but shows wrong site

Solution: Ensure the hostname appears in either ServerName or ServerAlias of the intended VirtualHost

Problem: "ServerName takes precedence" confusion

Clarification: The first VirtualHost with a matching ServerName/ServerAlias wins, regardless of ServerName declaration order

Debugging Tip: Use this command to verify which VirtualHost Apache selects:

apachectl -S

While ServerAlias is convenient, excessive use (especially with wildcards) can:

  • Slow down Apache's hostname matching
  • Make configuration harder to maintain
  • Potentially cause security issues if too permissive

Best practice is to explicitly list needed aliases rather than using broad wildcards.

<VirtualHost *:8080>
    ServerName api.example.com
    # Handles requests to api.example.com:8080
</VirtualHost>

<VirtualHost *:80>
    ServerName example.com
    # Handles regular HTTP requests
</VirtualHost>