Tutorials

How to Add a New Domain to Your Nginx Server

Administrator
By Administrator
Published Oct 03, 2025
8 min read
How to Add a New Domain to Your Nginx Server

How to Add a New Domain to Your Nginx Server

So you've got your Nginx server running smoothly with one website, and now you want to add another domain. Maybe you're starting a new project, setting up a client site, or just experimenting with different ideas. Whatever the reason, adding multiple domains to your Nginx server is surprisingly straightforward once you understand the structure.

I remember when I first tried to add a second domain to my server. I spent hours trying to figure out why both domains were showing the same website, despite having separate configuration files. It turned out I was missing one crucial step that made all the difference.

In this guide, I'll walk you through the entire process of adding a new domain to your existing Nginx setup. We'll cover everything from DNS configuration to SSL setup, with plenty of real-world examples and troubleshooting tips. Let's expand your server! 🚀

Understanding Nginx Server Blocks

Before diving in, let's understand how Nginx handles multiple domains. Nginx uses "server blocks" (similar to Apache's virtual hosts) to manage different websites on the same server. Each server block can have its own:

  • Domain name (server_name directive)
  • Document root (where website files are stored)
  • Configuration settings (PHP processing, caching, etc.)
  • SSL certificates for HTTPS

The beauty of this system is that each domain behaves completely independently while sharing the same Nginx installation.

Prerequisites: What You Need Before Starting

  • Existing Nginx server with at least one domain already configured
  • New domain name that you own
  • SSH access to your server with sudo privileges
  • Basic understanding of the command line and file editing

Step-by-Step Guide to Adding a New Domain

Step 1: Configure DNS for Your New Domain

Before anything else, your new domain needs to point to your server's IP address.

1.1 Find Your Server's IP Address

curl ifconfig.me

This will display your server's public IP address.

1.2 Update DNS Records

Log into your domain registrar (GoDaddy, Namecheap, Cloudflare, etc.) and create/update these DNS records:

Type: A Record
Name: @ (or your domain name)
Value: your_server_ip
TTL: 3600 (or default)

Type: A Record
Name: www
Value: your_server_ip
TTL: 3600 (or default)

1.3 Verify DNS Propagation

DNS changes can take anywhere from a few minutes to 48 hours to propagate. You can check the status with:

nslookup yournewdomain.com

Once it shows your server's IP, you're ready to proceed.

Step 2: Create Directory Structure for the New Domain

Let's create a home for your new website's files.

2.1 Create Web Directory

sudo mkdir -p /var/www/yournewdomain.com
sudo mkdir -p /var/www/yournewdomain.com/logs

2.2 Set Proper Permissions

sudo chown -R www-data:www-data /var/www/yournewdomain.com
sudo chmod -R 755 /var/www/yournewdomain.com

2.3 Create a Test Index Page

Let's create a simple test page to verify everything works:

sudo nano /var/www/yournewdomain.com/index.html

Add this content:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Welcome to Your New Domain!</title>
    <style>
        body { font-family: Arial, sans-serif; text-align: center; margin-top: 50px; }
        .container { max-width: 600px; margin: 0 auto; }
    </style>
</head>
<body>
    <div class="container">
        <h1>🎉 Your New Domain is Working!</h1>
        <p>This is the test page for yournewdomain.com</p>
        <p>Server time: <?php echo date('Y-m-d H:i:s'); ?></p>
    </div>
</body>
</html>

Step 3: Create Nginx Server Block Configuration

Now for the main event - configuring Nginx to recognize your new domain.

3.1 Create Server Block File

sudo nano /etc/nginx/sites-available/yournewdomain.com

3.2 Add Basic Configuration

Start with a basic HTTP configuration:

server {
    listen 80;
    server_name yournewdomain.com www.yournewdomain.com;

    root /var/www/yournewdomain.com;
    index index.html index.php;

    # Log files
    access_log /var/www/yournewdomain.com/logs/access.log;
    error_log /var/www/yournewdomain.com/logs/error.log;

    # Handle static files
    location / {
        try_files $uri $uri/ =404;
    }

    # Security headers
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header X-Content-Type-Options "nosniff" always;
}

3.3 Enable the Site

Create a symbolic link to enable your new site:

sudo ln -s /etc/nginx/sites-available/yournewdomain.com /etc/nginx/sites-enabled/

3.4 Test and Reload Nginx

Always test your configuration before applying it:

sudo nginx -t

If you see "syntax is ok" and "test is successful", reload Nginx:

sudo systemctl reload nginx

Step 4: Test Your New Domain

Now let's make sure everything is working correctly.

4.1 Test HTTP Access

Open your browser and navigate to http://yournewdomain.com. You should see your test page.

4.2 Test Nginx Configuration

You can also test from the command line:

curl -I http://yournewdomain.com

You should see a 200 OK response.

Step 5: Add SSL Certificate (Optional but Recommended)

Let's secure your new domain with HTTPS using Let's Encrypt.

5.1 Install Certbot (if not already installed)

sudo apt update
sudo apt install certbot python3-certbot-nginx -y

5.2 Obtain SSL Certificate

sudo certbot --nginx -d yournewdomain.com -d www.yournewdomain.com

Follow the prompts:

  • Enter your email address
  • Agree to terms of service
  • Choose whether to share your email (optional)
  • Select redirect option (choose option 2 for HTTPS redirect)

5.3 Verify SSL Installation

Certbot will automatically update your Nginx configuration. Visit https://yournewdomain.com and look for the padlock icon.

Step 6: Advanced Configuration Examples

Depending on what you're hosting, you might need different configurations.

6.1 PHP Application (WordPress, Laravel, etc.)

server {
    listen 443 ssl http2;
    server_name yournewdomain.com www.yournewdomain.com;

    root /var/www/yournewdomain.com;
    index index.php index.html;

    # SSL configuration (added by Certbot)
    ssl_certificate /etc/letsencrypt/live/yournewdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/yournewdomain.com/privkey.pem;

    # Handle static files
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    # PHP processing
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    # Deny access to .htaccess files
    location ~ /\.ht {
        deny all;
    }
}

# Redirect HTTP to HTTPS
server {
    listen 80;
    server_name yournewdomain.com www.yournewdomain.com;
    return 301 https://$server_name$request_uri;
}

6.2 Static HTML Site

server {
    listen 443 ssl http2;
    server_name yournewdomain.com;

    root /var/www/yournewdomain.com;
    index index.html;

    # SSL configuration
    ssl_certificate /etc/letsencrypt/live/yournewdomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/yournewdomain.com/privkey.pem;

    # Enable gzip compression
    gzip on;
    gzip_vary on;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml;

    # Cache static files
    location ~* \.(css|js|png|jpg|jpeg|gif|ico|svg)$ {
        expires 1y;
        add_header Cache-Control "public, immutable";
    }

    # Handle all other requests
    location / {
        try_files $uri $uri/ =404;
    }
}

6.3 Reverse Proxy Configuration

If you're proxying to another application:

server {
    listen 443 ssl http2;
    server_name yournewdomain.com;

    # SSL configuration

    location / {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Step 7: Managing Multiple Domains

Here are some tips for managing multiple domains efficiently.

7.1 List All Enabled Sites

ls -la /etc/nginx/sites-enabled/

7.2 Disable a Site

If you need to temporarily disable a domain:

sudo rm /etc/nginx/sites-enabled/yourdomain.com
sudo systemctl reload nginx

7.3 Common Configuration File

For shared settings across multiple domains, create a common configuration:

sudo nano /etc/nginx/conf.d/common-settings.conf

Add shared settings:

# Common security headers
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Content-Type-Options "nosniff" always;

# Gzip compression
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

Then include it in your server blocks:

server {
    listen 443 ssl http2;
    server_name yournewdomain.com;

    # Include common settings
    include /etc/nginx/conf.d/common-settings.conf;

    # Rest of your configuration
}

Troubleshooting Common Issues

"Domain Shows Same Content as Another Domain"

Cause: Nginx is using the default server block or catching requests in the wrong configuration.

Solution: Check your server_name directives and ensure each domain has its own configuration:

# Check which configuration is being used
sudo nginx -T | grep -A 10 "server_name yournewdomain.com"

"502 Bad Gateway Error"

Cause: Backend application (like PHP-FPM) isn't running or configured incorrectly.

Solution:

# Check PHP-FPM status
sudo systemctl status php8.1-fpm

# Check error logs
sudo tail -f /var/www/yournewdomain.com/logs/error.log

"SSL Certificate Not Working"

Cause: DNS hasn't propagated or certificate configuration is incorrect.

Solution:

# Check DNS resolution
nslookup yournewdomain.com

# Test certificate
sudo certbot certificates

"Permission Denied Errors"

Cause: Incorrect file permissions or ownership.

Solution:

sudo chown -R www-data:www-data /var/www/yournewdomain.com
sudo chmod -R 755 /var/www/yournewdomain.com

Best Practices for Multiple Domains

  • Use descriptive names for your configuration files
  • Keep consistent directory structure across domains
  • Regularly backup configurations in /etc/nginx/sites-available/
  • Monitor disk space as multiple domains consume more storage
  • Set up separate log files for each domain to simplify debugging
  • Use SSL certificates for all domains to ensure security
  • Test configurations with nginx -t before reloading

Automation Scripts (Optional)

If you frequently add new domains, consider creating a simple script:

#!/bin/bash
# add-domain.sh

DOMAIN=$1
if [ -z "$DOMAIN" ]; then
    echo "Usage: ./add-domain.sh domain.com"
    exit 1
fi

# Create directory
sudo mkdir -p /var/www/$DOMAIN
sudo chown www-data:www-data /var/www/$DOMAIN

# Create basic config
sudo tee /etc/nginx/sites-available/$DOMAIN > /dev/null <

Final Thoughts

Adding new domains to your Nginx server is a skill that opens up countless possibilities. Whether you're building a portfolio of websites, hosting client projects, or just experimenting with different ideas, understanding how to manage multiple domains efficiently is invaluable.

The key takeaways are:

  • Plan your directory structure before starting
  • Test DNS thoroughly before configuring Nginx
  • Always test configurations with nginx -t
  • Use SSL certificates for all domains
  • Keep organized logs for easy troubleshooting

Remember, practice makes perfect. The more domains you add, the more comfortable you'll become with the process. Start simple, test thoroughly, and gradually add more complex configurations as you gain confidence.

Happy domain hosting! 🌐 Your server is ready to grow.

Related Articles

How to Backup and Restore a Website on VPS Linux

How to Backup and Restore a Website on VPS Linux

Oct 03, 2025

How to Backup and Restore a Website on VPS Linux That moment when you realize your website is gon...

Setting Up Load Balancing with Nginx for High Traffic Sites

Setting Up Load Balancing with Nginx for High Traffic Sites

Oct 03, 2025

Setting Up Load Balancing with Nginx for High Traffic Sites Your website is growing. Traffic is i...

How to Monitor Server Resources with htop and netstat

How to Monitor Server Resources with htop and netstat

Oct 03, 2025

How to Monitor Server Resources with htop and netstat Ever wonder why your website suddenly slows...

Basic Firewall Configuration for Linux Web Servers

Basic Firewall Configuration for Linux Web Servers

Oct 03, 2025

Basic Firewall Configuration for Linux Web Servers Your web server is like a house in a busy neig...