How to Migrate Apache ServerAlias to NGINX server_name Directive: A Complete Configuration Guide


2 views

When migrating from Apache to NGINX, one of the key configuration elements that needs attention is the ServerAlias directive. In Apache's VirtualHost configuration, ServerAlias specifies additional domain names that should be served by the same virtual host. NGINX achieves the same functionality through its server_name directive.

Here's the fundamental difference between the two approaches:

# Apache configuration
ServerAlias our-domain.com www1.our-domain.com our-domain.ie

# Equivalent NGINX configuration
server_name www.our-domain.com our-domain.com www.our-domain.ie our-domain.ie;

While the concept is similar, there are important implementation details to consider:

  • NGINX doesn't have a separate ServerAlias directive - all domain variations go in server_name
  • The order of server names matters in NGINX for matching purposes
  • NGINX supports pattern matching and regular expressions in server names

Here's a full server block configuration showing the proper implementation:

server {
    listen 80;
    listen [::]:80;
    
    server_name www.our-domain.com our-domain.com 
                www.our-domain.ie our-domain.ie
                *.our-domain.com;
                
    root /var/www/our-domain;
    index index.html index.php;
    
    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    
    # Additional configuration...
}

NGINX offers more flexibility than Apache's ServerAlias with pattern matching:

# Match all subdomains of our-domain.com
server_name ~^(.*)\.our-domain\.com$;

# Match multiple TLD variations
server_name ~^www\.our-domain\.(com|ie|eu)$;

# Special handling for naked domains
server_name our-domain.com our-domain.ie ~^www\..+\.our-domain\.(com|ie)$;

When configuring multiple server names:

  • Place the most frequently accessed domain first in the list
  • For large numbers of domains (50+), consider using a map directive instead
  • Regular expressions have higher processing overhead than exact matches

Always verify your NGINX configuration after changes:

nginx -t
systemctl reload nginx

You can test domain resolution with:

curl -I -H "Host: our-domain.ie" http://localhost

When transitioning from Apache to NGINX, one of the key configurations that needs careful attention is domain aliasing. Apache's ServerAlias directive has a direct counterpart in NGINX's server_name, but there are important differences in implementation.

The server_name in NGINX serves the same fundamental purpose as Apache's ServerAlias, but with more flexible pattern matching capabilities. Your conversion example is correct:

# Original Apache configuration
ServerAlias our-domain.com www1.our-domain.com our-domain.ie

# Equivalent NGINX configuration
server_name www.our-domain.com our-domain.com www.our-domain.ie our-domain.ie;

NGINX offers several powerful features beyond basic domain listing:

# Wildcard matching
server_name *.our-domain.com;

# Regular expression matching
server_name ~^www\d+\.our-domain\.com$;

# Catch-all server block
server_name _;

The order of server_name declarations matters in NGINX:

  • Exact names have highest priority (our-domain.com)
  • Wildcard names starting with asterisk come next (*.our-domain.com)
  • Wildcard names ending with asterisk follow (our-domain.*)
  • Regular expressions have lowest priority

Here's a full server block demonstrating best practices:

server {
    listen 80;
    listen [::]:80;
    
    server_name www.our-domain.com our-domain.com 
                www.our-domain.ie our-domain.ie
                *.our-domain.net;
    
    root /var/www/our-domain;
    index index.html index.php;
    
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    
    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
    }
}

If your aliases aren't working:

  1. Check for syntax errors with nginx -t
  2. Verify the DNS records for all aliases
  3. Ensure no conflicting server blocks exist
  4. Check the NGINX error logs (/var/log/nginx/error.log)