<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Configure Nginx for Multiple Domains]]></title><description><![CDATA[<p dir="auto">Configuring Nginx to host multiple domains on a single server is done using <strong>Server Blocks</strong> (similar to "Virtual Hosts" in Apache), which define separate configurations for each domain. Nginx looks at the requested domain name (the <code>Host</code> header) and routes the traffic to the corresponding directory.</p>
<p dir="auto">Here is a step-by-step guide to setting up two domains, which we will call <strong><a href="http://domain1.com" rel="nofollow ugc">domain1.com</a></strong> and <strong><a href="http://domain2.com" rel="nofollow ugc">domain2.com</a></strong></p>
<h2>Step 1 : Create Document Root Directories</h2>
<p dir="auto">First, you need to create a document root (the directory where the website files live) for each domain. It is standard practice to put these in the <code>/var/www/</code> directory.</p>
<p dir="auto">Now, ceate 2 separate directories to store the website files for each domain:</p>
<pre><code>sudo mkdir -p /var/www/domain1.com
sudo mkdir -p /var/www/domain2.com
</code></pre>
<p dir="auto">Next, assign ownership of the directories to your regular user account (so you can edit files without needing <code>sudo</code> every time):</p>
<pre><code>sudo chown -R $USER:$USER /var/www/domain1.com
sudo chown -R $USER:$USER /var/www/domain2.com
</code></pre>
<p dir="auto">Ensure the permissions are correct so the web server can read the files:</p>
<pre><code>sudo chmod -R 755 /var/www
</code></pre>
<h2>Step 2 : Create Sample Pages (Optional, for Testing)</h2>
<p dir="auto">To test that everything is working later, create a simple <code>index.html</code> file in each directory.</p>
<p dir="auto">For <strong><a href="http://domain1.com" rel="nofollow ugc">domain1.com</a></strong> :</p>
<pre><code>nano /var/www/domain1.com/index.html
</code></pre>
<p dir="auto">Add this HTML:</p>
<pre><code class="language-html">&lt;html&gt;
    &lt;head&gt;&lt;title&gt;Welcome to Domain 1!&lt;/title&gt;&lt;/head&gt;
    &lt;body&gt;&lt;h1&gt;Success! The domain1.com server block is working!&lt;/h1&gt;&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p dir="auto">For <strong><a href="http://domain2.com" rel="nofollow ugc">domain2.com</a></strong> :</p>
<pre><code>nano /var/www/domain2.com/index.html
</code></pre>
<p dir="auto">Add this HTML:</p>
<pre><code class="language-html">&lt;html&gt;
    &lt;head&gt;
        &lt;title&gt;Welcome to Domain 2!&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;h1&gt;Success! The domain2.com server block is working!&lt;/h1&gt;
    &lt;/body&gt;
&lt;/html&gt;
</code></pre>
<h2>Step 3: Create the Server Block Configuration Files</h2>
<p dir="auto">Nginx keeps configuration files for individual sites in <code>/etc/nginx/sites-available/</code>.</p>
<p dir="auto">Create the first configuration file for <strong><a href="http://domain1.com" rel="nofollow ugc">domain1.com</a></strong> :</p>
<pre><code>sudo nano /etc/nginx/sites-available/domain1.com
</code></pre>
<p dir="auto">Paste the following configuration block into the file. Be sure to replace <strong><a href="http://domain1.com" rel="nofollow ugc">domain1.com</a></strong> with your actual domain name:</p>
<pre><code>server {
    listen 80;
    listen [::]:80;

    # The directory where your website files are located
    root /var/www/domain1.com;

    # The default files to serve
    index index.html index.htm;

    # The domain names this server block will respond to
    server_name domain1.com www.domain1.com;

    location / {
        try_files $uri $uri/ =404;
    }
}
</code></pre>
<p dir="auto">Save and close the file.</p>
<p dir="auto">Now, create the second configuration file for <strong><a href="http://domain2.com" rel="nofollow ugc">domain2.com</a></strong> :</p>
<pre><code>sudo nano /etc/nginx/sites-available/domain2.com
</code></pre>
<p dir="auto">Add a similar configuration, making sure to update the <code>root</code> path and the <code>server_name</code> directives to match the second domain :</p>
<pre><code>server {
    listen 80;
    listen [::]:80;

    # The directory where your website files are located
    root /var/www/domain2.com;

    # The default files to serve
    index index.html index.htm;

    # The domain names this server block will respond to
    server_name domain2.com www.domain2.com;

    location / {
        try_files $uri $uri/ =404;
    }
}
</code></pre>
<p dir="auto">Save and close the file.</p>
<h2>Step 4: Enable the Server Blocks</h2>
<p dir="auto">To enable these configurations, you need to create a symbolic link (symlink) from the files in <code>sites-available</code> to the <code>sites-enabled</code> directory. Nginx reads the <code>sites-enabled</code> directory during startup.</p>
<pre><code>sudo ln -s /etc/nginx/sites-available/domain1.com /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/domain2.com /etc/nginx/sites-enabled/
</code></pre>
<p dir="auto"><strong>Note:</strong> To avoid memory bucket problems that can arise from adding multiple server names, open the main Nginx configuration file:</p>
<pre><code>sudo nano /etc/nginx/nginx.conf
</code></pre>
<p dir="auto">Find the <code>server_names_hash_bucket_size</code> directive and uncomment it (remove the <code>#</code> symbol):</p>
<pre><code>server_names_hash_bucket_size 64;
</code></pre>
<p dir="auto">Save and close the file.</p>
<h2>Step 5: Test and Restart Nginx</h2>
<p dir="auto">Before restarting the web server, it is highly recommended to test your configuration files to make sure there are no syntax errors.</p>
<pre><code>sudo nginx -t
</code></pre>
<p dir="auto">If the test is successful, you will see output confirming that <code>syntax is ok</code> and <code>test is successful</code>. If you see errors, double-check your configuration files for missing semicolons or typos.</p>
<p dir="auto">Finally, restart Nginx to apply the changes:</p>
<pre><code>sudo systemctl restart nginx
</code></pre>
<h2>6. Configure DNS Records</h2>
<p dir="auto">Before you can see respective sample pages for <strong><a href="http://domain1.com" rel="nofollow ugc">domain1.com</a></strong> and <strong><a href="http://domain2.com" rel="nofollow ugc">domain2.com</a></strong>, ensure your DNS A-records for both domains are pointed to your server's public IP address.</p>
<p dir="auto">You should now be able to visit <strong><a href="http://domain1.com" rel="nofollow ugc">http://domain1.com</a></strong> and <strong><a href="http://domain2.com" rel="nofollow ugc">http://domain2.com</a></strong> in your browser and see your respective sample pages.</p>
<p dir="auto"><strong>Would you like me to walk you through how to secure these new domains with free SSL/HTTPS certificates using Let's Encrypt (Certbot)?</strong></p>
]]></description><link>https://ivan9.com/topic/3/configure-nginx-for-multiple-domains</link><generator>RSS for Node</generator><lastBuildDate>Mon, 04 May 2026 01:38:18 GMT</lastBuildDate><atom:link href="https://ivan9.com/topic/3.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 15 Mar 2026 04:01:18 GMT</pubDate><ttl>60</ttl></channel></rss>