How to Set Up Multiple Websites with Nginx Virtual Hosts
top of page
  • Writer's pictureSuraj Dhakre

How to Set Up Multiple Websites with Nginx Virtual Hosts

Introduction

Nginx is a popular web server that is known for its high performance and scalability. One of the key features of Nginx is its ability to set up virtual hosts, which allows you to host multiple websites on a single server. In this article, we will explore the benefits of setting up multiple websites with Nginx virtual hosts and provide a step-by-step guide on how to do it. Setting up multiple websites with Nginx virtual hosts is important for several reasons. Firstly, it improves website performance by allowing each website to have its own dedicated resources. This means that if one website experiences a spike in traffic, it will not affect the performance of other websites hosted on the same server. Additionally, virtual hosts allow you to better manage and organize your websites, making it easier to make changes and updates.


nginx virtual hosts


Understanding the Benefits of Setting Up Multiple Websites with Nginx Virtual Hosts

Improved website performance

By setting up multiple websites with Nginx virtual hosts, you can ensure that each website has its own dedicated resources. This means that if one website experiences a spike in traffic or requires more processing power, it will not affect the performance of other websites hosted on the same server. This can greatly improve the overall performance and stability of your websites.

Better security

With Nginx virtual hosts, you can isolate each website from one another, providing an added layer of security. If one website gets compromised, it will not affect the security of other websites hosted on the same server. This is especially important if you are hosting websites for different clients or if you have multiple websites with different security requirements.

Cost-effective solution

Setting up multiple websites with Nginx virtual hosts is a cost-effective solution compared to hosting each website on a separate server. By utilizing the resources of a single server, you can save on hardware costs and reduce maintenance and management overhead.

Easy management of multiple websites

Nginx virtual hosts make it easy to manage and organize multiple websites. Each website can have its own configuration file, making it easy to make changes and updates. Additionally, Nginx provides tools and features that allow you to easily monitor and manage your websites, making it a convenient solution for website management.

Preparing Your Server for Multiple Websites with Nginx Virtual Hosts

Before setting up multiple websites with Nginx virtual hosts, there are a few steps you need to take to prepare your server.

Installing Nginx

The first step is to install Nginx on your server. The installation process may vary depending on your operating system, but it typically involves running a few commands in the terminal. Once Nginx is installed, you can start configuring it for virtual hosts.

Configuring firewall settings

It is important to configure your server's firewall settings to allow incoming traffic to your websites. This involves opening the necessary ports and setting up rules to allow traffic to pass through. This step is crucial for ensuring that your websites are accessible to users.

sudo ufw allow 'Nginx Full'

Setting up DNS records

In order for users to access your websites, you need to set up DNS records that point to your server's IP address. This involves configuring the DNS settings for each domain or subdomain that you want to host on your server. This step is necessary for mapping domain names to IP addresses.

Creating Nginx Virtual Hosts for Your Websites

Once your server is prepared, you can start creating Nginx virtual hosts for your websites. Here are the steps involved:

Creating a new server block

In Nginx, each website is configured using a server block. To create a new server block, you need to create a new configuration file in the Nginx configuration directory. This file should be named after the domain or subdomain that you want to host.


Defining the root directory

In the server block configuration file, you need to define the root directory for your website. This is the directory where your website's files are stored. You can specify an absolute path or a relative path to the root directory.

Setting up server name and aliases

In the server block configuration file, you need to specify the server name and any aliases for your website. The server name is the domain or subdomain that you want to host, while aliases are alternative names that users can use to access your website.

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

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

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

Configuring Nginx Virtual Hosts for Multiple Domains and Subdomains

Nginx virtual hosts can be configured to host multiple domains and subdomains on a single server. Here are the steps involved:

Setting up multiple domains

To set up multiple domains, you need to create a separate server block configuration file for each domain. Each server block should have a unique server name and root directory. You can also specify any aliases for each domain.

Setting up subdomains

To set up subdomains, you can create a separate server block configuration file for each subdomain or use wildcard subdomains. Wildcard subdomains allow you to host multiple subdomains under a single server block. You can specify the root directory and aliases for each subdomain.

Redirecting HTTP to HTTPS

If you want to redirect HTTP traffic to HTTPS, you can add a redirect rule to your Nginx configuration. This ensures that all traffic to your websites is encrypted and secure. You can specify the redirect rule in each server block configuration file.


Hands On Steps By Step Guide To Configure Nginx

When configuring Nginx for multiple domains, you can create separate server blocks for each domain. Here's an example configuration for two domains: example.com and anotherdomain.com.


Create directories for your websites

sudo mkdir -p /var/www/example.com/html
sudo mkdir -p /var/www/anotherdomain.com/html

Set permissions for the web directories

sudo chown -R $USER:$USER /var/www/example.com/html
sudo chown -R $USER:$USER /var/www/anotherdomain.com/html
sudo chmod -R 755 /var/www

Create sample index.html files

echo "<html><head><title>Welcome to example.com</title></head><body><h1>Success! The example.com virtual host is working!</h1></body></html>" | sudo tee /var/www/example.com/html/index.html

echo "<html><head><title>Welcome to anotherdomain.com</title></head><body><h1>Success! The anotherdomain.com virtual host is working!</h1></body></html>" | sudo tee /var/www/anotherdomain.com/html/index.html

Create Nginx server block configurations

For example.com

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

Add below config to the file.

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

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

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

For anotherdomain.com

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

Add below config to the file.

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

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

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

Create symbolic links to enable the virtual hosts

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

Test Nginx configuration

sudo nginx -t

Restart Nginx

sudo systemctl restart nginx

Adjust firewall settings (if applicable)

sudo ufw allow 'Nginx Full'

Verify your websites

Open your web browser and navigate to both domain names (e.g., http://example.com and http://anotherdomain.com). You should see the respective sample index.html pages.



Troubleshooting Common Issues with Nginx Virtual Hosts

While setting up Nginx virtual hosts is relatively straightforward, there may be some common issues that you may encounter. Here are some troubleshooting tips:

Checking Nginx error logs

Nginx logs any errors or issues in its error log file. By checking the error log file, you can identify any issues with your virtual hosts configuration. The error log file is typically located in the Nginx log directory.

Debugging configuration files

If you are experiencing issues with your virtual hosts configuration, you can use the Nginx configuration test command to check for syntax errors. This command will check your configuration files for any errors and provide feedback on how to fix them.

Common issues and solutions

Some common issues with Nginx virtual hosts include incorrect file permissions, misconfigured DNS records, and conflicting server blocks. By understanding these common issues and their solutions, you can quickly resolve any problems that arise. For nginx 404 not found error, please click here.

For nginx 502 bad gateway error, please click here.



Conclusion

Setting up multiple websites with Nginx virtual hosts offers several benefits, including improved website performance, better security, cost-effectiveness, and easy management. By following the steps outlined in this article, you can easily configure Nginx virtual hosts for your websites. Additionally, by following best practices and regularly monitoring and maintaining your websites, you can ensure that they are running smoothly and efficiently. So why not give it a try and see how Nginx virtual hosts can improve your website management and performance?

bottom of page