Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse

NodeBB

  1. Home
  2. VPS Tutorial
  3. Configure Nginx for Multiple Domains

Configure Nginx for Multiple Domains

Scheduled Pinned Locked Moved VPS Tutorial
1 Posts 1 Posters 48 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cuong
    wrote last edited by
    #1

    Configuring Nginx to host multiple domains on a single server is done using Server Blocks (similar to "Virtual Hosts" in Apache), which define separate configurations for each domain. Nginx looks at the requested domain name (the Host header) and routes the traffic to the corresponding directory.

    Here is a step-by-step guide to setting up two domains, which we will call domain1.com and domain2.com

    Step 1 : Create Document Root Directories

    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 /var/www/ directory.

    Now, ceate 2 separate directories to store the website files for each domain:

    sudo mkdir -p /var/www/domain1.com
    sudo mkdir -p /var/www/domain2.com
    

    Next, assign ownership of the directories to your regular user account (so you can edit files without needing sudo every time):

    sudo chown -R $USER:$USER /var/www/domain1.com
    sudo chown -R $USER:$USER /var/www/domain2.com
    

    Ensure the permissions are correct so the web server can read the files:

    sudo chmod -R 755 /var/www
    

    Step 2 : Create Sample Pages (Optional, for Testing)

    To test that everything is working later, create a simple index.html file in each directory.

    For domain1.com :

    nano /var/www/domain1.com/index.html
    

    Add this HTML:

    <html>
        <head><title>Welcome to Domain 1!</title></head>
        <body><h1>Success! The domain1.com server block is working!</h1></body>
    </html>
    

    For domain2.com :

    nano /var/www/domain2.com/index.html
    

    Add this HTML:

    <html>
        <head>
            <title>Welcome to Domain 2!</title>
        </head>
        <body>
            <h1>Success! The domain2.com server block is working!</h1>
        </body>
    </html>
    

    Step 3: Create the Server Block Configuration Files

    Nginx keeps configuration files for individual sites in /etc/nginx/sites-available/.

    Create the first configuration file for domain1.com :

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

    Paste the following configuration block into the file. Be sure to replace domain1.com with your actual domain name:

    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;
        }
    }
    

    Save and close the file.

    Now, create the second configuration file for domain2.com :

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

    Add a similar configuration, making sure to update the root path and the server_name directives to match the second domain :

    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;
        }
    }
    

    Save and close the file.

    Step 4: Enable the Server Blocks

    To enable these configurations, you need to create a symbolic link (symlink) from the files in sites-available to the sites-enabled directory. Nginx reads the sites-enabled directory during startup.

    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/
    

    Note: To avoid memory bucket problems that can arise from adding multiple server names, open the main Nginx configuration file:

    sudo nano /etc/nginx/nginx.conf
    

    Find the server_names_hash_bucket_size directive and uncomment it (remove the # symbol):

    server_names_hash_bucket_size 64;
    

    Save and close the file.

    Step 5: Test and Restart Nginx

    Before restarting the web server, it is highly recommended to test your configuration files to make sure there are no syntax errors.

    sudo nginx -t
    

    If the test is successful, you will see output confirming that syntax is ok and test is successful. If you see errors, double-check your configuration files for missing semicolons or typos.

    Finally, restart Nginx to apply the changes:

    sudo systemctl restart nginx
    

    6. Configure DNS Records

    Before you can see respective sample pages for domain1.com and domain2.com, ensure your DNS A-records for both domains are pointed to your server's public IP address.

    You should now be able to visit http://domain1.com and http://domain2.com in your browser and see your respective sample pages.

    Would you like me to walk you through how to secure these new domains with free SSL/HTTPS certificates using Let's Encrypt (Certbot)?

    1 Reply Last reply
    0

    Hello! It looks like you're interested in this conversation, but you don't have an account yet.

    Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

    With your input, this post could be even better 💗

    Register Login
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    Powered by NodeBB Contributors
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups