{"id":1265,"date":"2017-03-21T19:56:03","date_gmt":"2017-03-21T14:26:03","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=1265"},"modified":"2026-09-17T14:00:31","modified_gmt":"2026-09-17T08:30:31","slug":"setup-serveralias-ngnix-webserveralias-domain-name","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/setup-serveralias-ngnix-webserveralias-domain-name\/","title":{"rendered":"How to Configure Multiple Domain Names in Nginx Using Server Name"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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 <code>server_name<\/code> directive.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In Apache, the <code>ServerAlias<\/code> directive is commonly used for this purpose. Nginx does not use a separate <code>ServerAlias<\/code> directive. Instead, multiple domain names can be specified in the same <code>server_name<\/code> directive.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this article, we will configure both the root domain and its <code>www<\/code> version to serve the same website from the same Nginx server block.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before proceeding, ensure that:<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>Nginx is installed on the server.<\/li>\n\n\n\n<li>You have root or sudo access.<\/li>\n\n\n\n<li>The domain names point to the Nginx server.<\/li>\n\n\n\n<li>You have access to the Nginx virtual host configuration.<\/li>\n\n\n\n<li>The website document root is already configured.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">For example, the following domains will be configured:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">domain.tld\nwww.domain.tld<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Both domains will use the same Nginx server configuration and document root.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Implementation<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Open the Nginx Configuration<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If the default Nginx configuration is being used, open:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">vi \/etc\/nginx\/sites-available\/default<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If your website has a separate Nginx configuration file, open the corresponding configuration instead.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Configure Multiple Domain Names<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Locate the existing <code>server_name<\/code> directive.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">server_name _;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Replace it with:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">server_name domain.tld www.domain.tld;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The complete server block may look similar to:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">server {\n    listen 80;\n    listen [::]:80;\n\n    server_name domain.tld www.domain.tld;\n\n    root \/var\/www\/html;\n    index index.html index.htm index.nginx-debian.html;\n\n    location \/ {\n        try_files $uri $uri\/ =404;\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The important configuration is:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">server_name domain.tld www.domain.tld;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This tells Nginx to use the same server block when a request is made for either:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">domain.tld<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">or:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">www.domain.tld<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Adding Multiple Domain Names<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Multiple domain names can be specified within the same <code>server_name<\/code> directive.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">server_name domain.tld www.domain.tld example.com www.example.com;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">All of these names can use the same server block and document root.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: Test the Nginx Configuration<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before applying the changes, check the Nginx configuration for syntax errors:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">nginx -t<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A successful test should return output similar to:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">syntax is ok\ntest is successful<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If the configuration test reports an error, correct the configuration before reloading Nginx.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 4: Reload Nginx<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">After successfully testing the configuration, reload Nginx:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">service nginx reload<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Alternatively, on systems using <code>systemd<\/code>:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">systemctl reload nginx<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A reload applies the configuration changes without requiring a full service restart.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Verification<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Open the configured domains in a browser:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">http:\/\/domain.tld<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">and:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">http:\/\/www.domain.tld<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Both requests should be handled by the same Nginx server block and serve content from the configured document root.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can also verify the DNS records to ensure that both domain names resolve to the correct Nginx server.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Note: Alias vs Redirect<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Configuring multiple names in <code>server_name<\/code> means Nginx can serve the same website for both domain names. It does <strong>not<\/strong> automatically redirect one domain to another.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">server_name domain.tld www.domain.tld;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">allows both names to serve the site.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If the requirement is to redirect <code>www.domain.tld<\/code> to <code>domain.tld<\/code>, or the other way around, a separate Nginx server block with a <code>return 301<\/code> redirect should be configured.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Nginx does not use the Apache <code>ServerAlias<\/code> directive. Instead, multiple domain names can be configured using the <code>server_name<\/code> directive within an Nginx server block.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this example, we configured <code>domain.tld<\/code> and <code>www.domain.tld<\/code> to use the same Nginx configuration and document root. We also verified the configuration using <code>nginx -t<\/code> and reloaded Nginx to apply the changes.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently Asked Questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. Does Nginx support the <code>ServerAlias<\/code> directive?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Nginx does not use Apache&#8217;s <code>ServerAlias<\/code> directive. Multiple domain names can be specified using the <code>server_name<\/code> directive.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. How do I configure both www and non-www domains?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Add both domain names to the same <code>server_name<\/code> directive:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">server_name domain.tld www.domain.tld;<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. Can I add multiple domains to one Nginx server block?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. Multiple domain names can be specified in the same <code>server_name<\/code> directive:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">server_name domain.tld www.domain.tld example.com www.example.com;<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. How do I check whether the Nginx configuration is valid?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Run:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">nginx -t<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5. Do I need to restart Nginx after changing the configuration?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A reload is generally sufficient:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">systemctl reload nginx<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">or:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">service nginx reload<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">6. Will configuring multiple domain names automatically redirect www to non-www?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. <code>server_name<\/code> allows Nginx to serve requests for multiple names. A separate redirect configuration is required if you want one hostname to redirect to another.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related Articles<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/pheonixsolutions.com\/blog\/how-to-install-and-configure-php-with-nginx-on-centos7\/\">How to install and configure PHP with Nginx on centos7<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/pheonixsolutions.com\/blog\/how-to-setup-ssl-certificate-in-nginx-on-ubuntu-20-04\/\">How to setup SSL Certificate in Nginx on Ubuntu 20.04<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/pheonixsolutions.com\/blog\/how-to-install-nginx-and-lets-encrypt-ssl-with-html-docker-ubuntu-20-04\/\">How to Install Nginx and Let\u2019s Encrypt SSL with HTML + Docker \u2013 Ubuntu 20.04<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Talk to our experts<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Have a technology challenge or looking for the right solution for your business? Our team can help you with <strong>cloud, DevOps, development, infrastructure, design, and more<\/strong>. Feel free to reach out to our experts <a href=\"https:\/\/pheonixsolutions.com\/contact\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction When multiple domain names need to point to the same website or document root, Nginx can be configured to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[302,261,271],"class_list":["post-1265","post","type-post","status-publish","format-standard","hentry","category-web-architecture","tag-alias","tag-linux","tag-nginx","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-kp","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1265","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=1265"}],"version-history":[{"count":1,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1265\/revisions"}],"predecessor-version":[{"id":11823,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1265\/revisions\/11823"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=1265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=1265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=1265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}