How to Configure Multiple Domain Names in Nginx Using Server Name

Introduction

When multiple domain names need to point to the same website or document root, Nginx can be configured to respond to all of those domain names using the server_name directive.

In Apache, the ServerAlias directive is commonly used for this purpose. Nginx does not use a separate ServerAlias directive. Instead, multiple domain names can be specified in the same server_name directive.

In this article, we will configure both the root domain and its www version to serve the same website from the same Nginx server block.

Prerequisites

Before proceeding, ensure that:

  1. Nginx is installed on the server.
  2. You have root or sudo access.
  3. The domain names point to the Nginx server.
  4. You have access to the Nginx virtual host configuration.
  5. The website document root is already configured.

For example, the following domains will be configured:

domain.tld
www.domain.tld

Both domains will use the same Nginx server configuration and document root.

Implementation

Step 1: Open the Nginx Configuration

If the default Nginx configuration is being used, open:

vi /etc/nginx/sites-available/default

If your website has a separate Nginx configuration file, open the corresponding configuration instead.

Step 2: Configure Multiple Domain Names

Locate the existing server_name directive.

For example:

server_name _;

Replace it with:

server_name domain.tld www.domain.tld;

The complete server block may look similar to:

server {
    listen 80;
    listen [::]:80;

    server_name domain.tld www.domain.tld;

    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;

    location / {
        try_files $uri $uri/ =404;
    }
}

The important configuration is:

server_name domain.tld www.domain.tld;

This tells Nginx to use the same server block when a request is made for either:

domain.tld

or:

www.domain.tld

Adding Multiple Domain Names

Multiple domain names can be specified within the same server_name directive.

For example:

server_name domain.tld www.domain.tld example.com www.example.com;

All of these names can use the same server block and document root.

Step 3: Test the Nginx Configuration

Before applying the changes, check the Nginx configuration for syntax errors:

nginx -t

A successful test should return output similar to:

syntax is ok
test is successful

If the configuration test reports an error, correct the configuration before reloading Nginx.

Step 4: Reload Nginx

After successfully testing the configuration, reload Nginx:

service nginx reload

Alternatively, on systems using systemd:

systemctl reload nginx

A reload applies the configuration changes without requiring a full service restart.

Verification

Open the configured domains in a browser:

http://domain.tld

and:

http://www.domain.tld

Both requests should be handled by the same Nginx server block and serve content from the configured document root.

You can also verify the DNS records to ensure that both domain names resolve to the correct Nginx server.

Note: Alias vs Redirect

Configuring multiple names in server_name means Nginx can serve the same website for both domain names. It does not automatically redirect one domain to another.

For example:

server_name domain.tld www.domain.tld;

allows both names to serve the site.

If the requirement is to redirect www.domain.tld to domain.tld, or the other way around, a separate Nginx server block with a return 301 redirect should be configured.

Conclusion

Nginx does not use the Apache ServerAlias directive. Instead, multiple domain names can be configured using the server_name directive within an Nginx server block.

In this example, we configured domain.tld and www.domain.tld to use the same Nginx configuration and document root. We also verified the configuration using nginx -t and reloaded Nginx to apply the changes.

Frequently Asked Questions

1. Does Nginx support the ServerAlias directive?

Nginx does not use Apache’s ServerAlias directive. Multiple domain names can be specified using the server_name directive.

2. How do I configure both www and non-www domains?

Add both domain names to the same server_name directive:

server_name domain.tld www.domain.tld;

3. Can I add multiple domains to one Nginx server block?

Yes. Multiple domain names can be specified in the same server_name directive:

server_name domain.tld www.domain.tld example.com www.example.com;

4. How do I check whether the Nginx configuration is valid?

Run:

nginx -t

5. Do I need to restart Nginx after changing the configuration?

A reload is generally sufficient:

systemctl reload nginx

or:

service nginx reload

6. Will configuring multiple domain names automatically redirect www to non-www?

No. server_name allows Nginx to serve requests for multiple names. A separate redirect configuration is required if you want one hostname to redirect to another.

Talk to our experts

Have a technology challenge or looking for the right solution for your business? Our team can help you with cloud, DevOps, development, infrastructure, design, and more. Feel free to reach out to our experts here.

admin

Our team has expertise across software and web development, WordPress, e-commerce, mobile applications, UI/UX, cloud and infrastructure, DevOps, CI/CD, API integration, security, testing, automation, and technical support. The team also works with AI-based software solutions, LLMs, AI workflows, AI agents, and intelligent application development to help businesses automate processes and build smarter digital solutions. We focus on developing, deploying, maintaining, and optimising secure, scalable, and reliable technology solutions while helping businesses adopt modern technologies and drive digital transformation.

Leave a Reply

Scroll to Top