Nginx Server Block: Ultimate Guide to Serve Multiple Sites with Nginx

Nginx server block

Table of Contents

Get up to 50% off now

Become a partner with CyberPanel and gain access to an incredible offer of up to 50% off on CyberPanel add-ons. Plus, as a partner, you’ll also benefit from comprehensive marketing support and a whole lot more. Join us on this journey today!

Nginx server blocks are a powerful tool when it comes to setting up and managing multiple websites or web applications on one web server. Nginx is chosen by the administrators with a web server management role due to its lightweight architecture, speed, and flexibility. Nginx Proxy Server – One of the key features of Nginx is the ability to handle multiple server blocks, which enables you to run multiple websites or applications (or a mix of them) on a single machine. Nginx server blocks are very useful for serving multiple domains through the same server, and each site can have different configurations.

Whether you are working on a DigitalOcean Nginx server block or you are working on how to set the proper configuration for multiple server blocks on your Nginx, this article is a guide on how to set Nginx server blocks correctly! If you are managing multiple websites, each with different settings and resources, you will know how to do so with the help of Nginx conf multiple server blocks by the end of this article. We are also going to discuss how Nginx server block configuration can improve performance and help you with site management.

Let’s get started!

What Is Nginx Server Block?

Nginx server blocks are like Apache virtual hosts. This is used to set up how Nginx serves up a particular domain or website. Server blocks are used in an Nginx configuration file to indicate the rules regarding which port/ domain to accept requests to, the web files to serve from the disk, and other rules such as SSL config and custom error pages, etc.

How to Set Up Nginx Server Blocks

To create multi-server blocks in Nginx, we have to create each block in the Nginx configuration file. In Nginx, you can have different configurations for each domain or subdomain hosted on your server. The nginx. conf file will have the server blocks.

Example: Basic Nginx Conf Server Block

Tech Delivered to Your Inbox!

Get exclusive access to all things tech-savvy, and be the first to receive 

the latest updates directly in your inbox.

server {

    listen 80;

    server_name example.com www.example.com;

    root /var/www/example;

    index index.html;

    location / {

        try_files $uri $uri/ =404;

    }

}

In the example above:

listen 80; //tells Nginx to listen on port 80 (default HTTP port)

server_name example. com www. example. com; declares which domains will be processed by this server block.

root /var/www/example; The document root of the website.

The location / block is responsible for handling requests to the root directory and passing them on.

Setup on Nginx for Multiple Server Blocks

Nginx can handle domain-based traffic via multiple server blocks, where you have one for the main domain as well as additional blocks for other domains and subdomains. As an example, below is how to set up more than 1 website in nginx with server blocks:

# Example 1: Server Block for example.com

server {

    listen 80;

    server_name example.com www.example.com;

    root /var/www/example;

    index index.html;

    location / {

        try_files $uri $uri/ =404;

    }

}

# Example 2: Server Block for example2.com

server {

    listen 80;

    server_name example2.com www.example2.com;

    root /var/www/example2;

    index index.html;

    location / {

        try_files $uri $uri/ =404;

    }

}

DigitalOcean Nginx Server Blocks

DigitalOcean is a virtual private server provider known for its ease of management, and Droplets, its servers, are widely used among those getting started. The deployment of an Nginx server for your websites is a click away on DigitalOcean, and configuring the server blocks on a DigitalOcean Droplet is pretty much exactly like on any other server, but with some added conveniences and optimizations.

How to Create Host Configurations for Nginx on DigitalOcean

Nginx has a compiled binary file and package for the majority of the Linux based operating systems, so you first have to install an OS that is compatible with Nginx on your Droplet; when you create a Droplet on DigitalOcean, it gives you the choice to install an OS on your Droplet (like Ubuntu). Setting Up Nginx Server Blocks on a DigitalOcean Droplet. Here are the steps to set up Nginx server blocks.

Install Nginx on DigitalOcean

If you do not have Nginx installed on your Droplet yet, you can install it with the following commands:

Enhance Your CyerPanel Experience Today!
Discover a world of enhanced features and show your support for our ongoing development with CyberPanel add-ons. Elevate your experience today!

sudo apt update

sudo apt install nginx

You can install the Nginx web server by using the command: sudo apt update sudo apt install nginx

This secures the latest stable version of Nginx is installed on your server.

Create Your Server Block Files

The default directory for Nginx configuration files is /etc/nginx/sites-available/. You will need a separate configuration file for each site you want to configure. You can, for instance, create a block for your website with a new configuration file inside this directory:

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

Paste your Nginx server block configuration in this file. For example:

server {

    listen 80;

    server_name example.com www.example.com;

    root /var/www/example;

    index index.html;

    location / {

        try_files $uri $uri/ =404;

    }

}

Create a Symbolic Link

Once you create a server block file in the sites-available directory, we need to create a symbolic link to it in the /etc/nginx/sites-enabled/ directory to make Nginx recognize it:

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

Test the Configuration

Before restarting Nginx, it’s important to check for any syntax errors:

sudo nginx -t

Restart Nginx

Assuming everything is configured properly, go ahead and restart Nginx to load the new configuration:

sudo systemctl restart nginx

Set Up DNS Records

Ensure that the DNS records for your domain direct traffic to the DigitalOcean server’s IP address. To make sure your domain can route to your Nginx instance, you’ll need to set up an A record that points to the Droplet’s public IP address.

Benefits of Using DigitalOcean for Nginx Server Blocks

  • Scalability: DigitalOcean’s infrastructure enables you to scale up or down as your needs evolve. As your site expands, you can scale your server resources without any downtime, enabling you to run multiple sites on one server in a more efficient way.
  • Easy Installation: DigitalOcean blocks make Nginx easy to set up, as there are plenty of guides available on their website to get you started.
  • Optimized for Nginx: The servers of DigitalOcean are quite well optimized to run Nginx, and they also provide pre-configured images for quicker.

How to Work with Several Server Blocks on DigitalOcean

To make it easier to manage multiple server blocks in Nginx, DigitalOcean allows users to create, update, and manage a number of these configurations in separate files for each domain dynamically. This organization helps to manage the hundreds of sites and applications we may have on one Droplet and mitigates against configuration errors.

Managing Nginx Server Blocks Using CyberPanel

CyberPanel

CyberPanel is a powerful web hosting control panel for Nginx. It simplifies the management of your server with an easy-to-use interface for administering your many websites, server blocks, and a whole range of other configurations.

Using CyberPanel after setting up Nginx server blocks:

  • Manage different server blocks for each website via its GUI.
  • DNS settings for each domain on your Nginx server.
  • Performance and traffic, per-site-level monitoring.
  • One-click installation of SSL certificates for HTTPS.
  • Backup and restore automation for each single server block.

CyberPanel simplifies the use of multiple server blocks on your Nginx server with easy-to-use tools designed for developers and administrators with multiple sites or applications to maintain.

FAQs

1. Is it possible to have multiple server blocks with the same server_name in Nginx?

In Nginx, you cannot define two server blocks with different configurations in each but with the same server_name. There can’t be multiple server name matching server block (using server_name directive), because duplicates will create conflict. Make sure server_name is unique for proper routing.

2. How do they compare to Nginx server blocks versus Apache virtual hosts?

Nginx server blocks and Apache virtual hosts are essentially the same concepts: they enable you to serve multiple sites with one server. The configuration syntax is the biggest difference. Server blocks are used in Nginx, and they are defined in the Nginx configuration file, while Apache defines virtual hosts in a separate configuration file. Nginx is widely preferred for high-traffic websites because of its performance and scalability.

3. What are the common issues with Nginx Server Block Configuration?

Omitting semicolons at the end of directives.
Wrong paths for the root directory.
Having multiple server blocks with the same server_name.

Final Thoughts!

Setting the Nginx server block is an important task for all server administrators who host multiple websites or web applications tar on a single server. With a correct configuration of multiple server blocks Nginx, you can effectively run your domains and applications while ensuring they are routed and optimized for performance. For ease, you can also refer to browsing tools like CyberPanel, which allow you to manage the server block, DNS entries, and SSL configurations from a web interface.

Start optimizing your web hosting environment today by mastering the configuration of Nginx server blocks!

Hasib Iftikhar
I'm Hasib Iftikhar, a dedicated technical writer at CyberPanel, joining the team in July 2024. With three years of extensive experience in content writing, I specialize in copywriting, article writing, guest posting, affiliate content writing, and SEO. My expertise ensures that each piece of content I create is engaging, informative, and optimized for search engines, helping businesses enhance their online presence and reach their target audience effectively.
Unlock Benefits

Become a Community Member

SIMPLIFY SETUP, MAXIMIZE EFFICIENCY!
Setting up CyberPanel is a breeze. We’ll handle the installation so you can concentrate on your website. Start now for a secure, stable, and blazing-fast performance!