How to set up NGINX virtual hosts (server blocks) on AlmaLinux

Reading Time: 5 minutes

Like an Apache virtual host, a NGINX virtual host (server block) allows you to host multiple domains or websites on your server. The configuration of NGINX virtual hosts — or server blocks as they are referred to on NGINX — will be covered in this article. The Linux distribution documented in our scenario discussed below will be AlmaLinux.

Key takeaways

By reading this article you will learn about:

  • NGINX virtual hosts and how they function.
  • The advantages and disadvantages of the NGINX platform.
  • Varied functionality related to NGINX in addition its web server features.
  • Typical NGINX problems that an admin may experience.
  • Setting up NGINX virtual hosts (server blocks) on AlmaLinux.

What are NGINX virtual hosts, and how do they function?

Although the concept of virtual hosts originates from the Apache HTTP Server, NGINX users also frequently use the same concept. In the NGINX setup, a virtual host is called is a server block.

When it is started, a server running web server software, such as NGINX, will bind itself to one or more network ports. For a web server, those are typically ports 80 and 443. When a computer accesses a website, it sends a request to the server through the internet, and the server responds. Initially, though, the server would examine the port to which the request was made as its destination. It will then use that destination port to find the application for which the data should be delivered. With this kind of standard setup, just one application on your web server can receive requests.

The server must first identify which website is being requested when it receives an incoming packet request before determining what data to provide. Based only on the limited port information, the server cannot manage more than a single website. To overcome this limitation, NGINX server blocks are helpful. They can be used to serve you difference audiences multiple websites, depending on the domain name specified. Or, they can deliver users other websites based on the requested IP address.

Consequently, NGINX examines the HTTP headers to determine the requested server name when a user types in a website name, such as www.example.com. The server then decides which directory contains the website files, depending on how the NGINX virtual hosts that have been configured. The server name is used to send the client the appropriate data on a per website basis.

Advantages of the NGINX platform

There are various advantages to using NGINX, such as the following:

  • Performance. Reduces the time that it takes for a website to load. Enhances performance by increasing the total speed of traffic routing to web servers. Your users will have a pleasant and positive browsing experience without worrying about excessive latency.
  • Load balancing. NGINX also can operate as a reliable and affordable load balancer.
  • Scalability. It provides the capacity to manage several requests at once, allowing for scalability.
  • Updates without downtime. Enables real-time updates without any interruptions.

Disadvantages of the NGINX platform

NGINX offers numerous benefits, but it also has a few drawbacks. It receives less support from the development community and community, which results in fewer features and upgrades. Setup and maintenance both call for specialized skills.

Any of the following configurations can be applied to an NGINX instance:

  • A web host. Because of its reliability and scalability, this is the most widely used.
  • A reverse proxy server. To accomplish this, NGINX routes the client's request to the relevant backend server.
  • Facilitator of an equilibrium of loads. Your network traffic burden is automatically distributed without the need for manual configuration.
  • A gateway for APIs. Request routing, authentication, and exception handling can all benefit from this.
  • Web application firewall (WAF). Screening incoming and outgoing network requests on your server, a WAF for website security safeguards your application.
  • A data store. In order to assist in storing your data for upcoming requests, NGINX serves as a cache.
  • A defense mechanism. NGINX can be implemented for its protection against attacks known as distributed denial-of-service (DDoS).

Typical NGINX problems

The following are a few of the typical NGINX problems sometimes experienced with a NGINX implementation:

  • Timeout failures in the upstream environment. They may be represented as "502 Bad Gateway" errors and occur when a different server sends an incorrect response to one of your servers. Another possible reason is an overcrowded server. It may create a poor user experience if visitors are asked to delete their browser's cookies and cache in order to resolve these server-side problems.
  • CPU power usage. Even when there isn't much traffic, the NGINX server can consume an unexpectedly high volume of CPU power.
  • Memory issues. Your server's memory shortage causes NGINX worker crashes. This is due to inadequate memory and high CPU usage.

Steps to set up NGINX virtual hosts (server blocks) on AlmaLinux

This guide will teach you how to set up NGINX Virtual hosts(Server Blocks) on AlmaLinux.

Requirements

The following requirements should be met before starting:

  • Having the A record for the domain you own. An A record is a DNS entry that points the domain name to the server's public IP address — in this example, the NGINX web server.
  • Having the necessary NGINX software available on your server. This requirement would be met by an installed LEMP Stack or NGINX Server.
  • Having a system account on the AlamaLinux server. For example, you must have a root system account or a login user with sudo privileges.

Step #1. Creating new document root directories

A custom web root directory must be created for the domain you wish to host. In this instance, we will use the mkdir -p option to create the directory as indicated, which will include all required parent directories:

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

After that, use the $USER environment variable to assign the directory permissions. Ensure you are logged in as a regular user and not the root user while doing this:

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

Next, assign the correct directory permissions recursively, as shown:

$ sudo chmod -R 755 /var/www/example.com/html

Step #2. Create a sample page for each site

Upon a request, the index.html file that the domain will serve will then be created and placed inside the custom web root directory.

Create an index.html file in your first domain using Nano or any text editor:

$ sudo vim /var/www/example.com/html/index.html

Paste the following inside the file: 
<html>
    <head>
        <title>Welcome to your_domain!</title>
    </head>
    <body>
  <h1>Success!  The example.com server block is working!
</h1>
    </body>
</html>

Save and exit the configuration file.

Step #3. Create an NGINX virtual host in AlmaLinux

Next, we need to construct a server block file with the necessary directives so that the NGINX web server can serve the content in the index.html file we prepared in the previous step. As a result, we will establish a new server block at the following location:

$ sudo vim /etc/nginx/conf.d/example.com.conf

Then, paste the configuration details as shown in the example below:

server {
        listen 80;
        listen [::]:80;

        root /var/www/example.com/html;
        index index.html index.htm index.nginx-debian.html;

        server_name example.com www.example.com;

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

		
    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;

}

Save your modifications and close the configuration file when you have finished. Run the following command to verify that every NGINX configuration is good and error-free:

$ sudo nginx -t

If no issues were found, restart NGINX to enable your changes:

$ sudo systemctl restart nginx

Then, confirm the status using the following command:

$ sudo systemctl status nginx

Step #4. Testing the NGINX server block in AlmaLinux

With the setup portion of the steps, we are all finished. Next is to verify whether the material served by our server block is located in the web root directory previously specified in the index.html file. To do this, open your browser and go to your server's domain, accessing the URL (for example, http://example.com).

Step #5. Enable HTTPS for a domain hosted on NGINX

Consider encrypting your domain using Let's Encrypt SSL to ensure secure traffic to and from the web server. To make sure that your domain has been correctly configured with SSL, open your browser, go to the server's domain using the HTTPS protocol (for example, https://example.com/), and confirm that the lock icon appears in the URL bar. The the rendering of this lock icon may vary by web browser.

Wrapping up the topic of NGINX virtual host setup

Using the NGINX virtual host setup steps provided in the sections above, you can build multiple server blocks using the same server for every domain you want to host. The amount of server blocks you can create is unlimited, provided your hardware can manage the load.

Contact our team if you have questions or want to learn more about hosting your websites with us at Liquid Web on our hardware found in 10 different data centers around the globe. We make cloud-based hosting a breeze. Explore the flexibility and value we provide.

Avatar for Amritha Varshini

About the Author: Amritha Varshini

With a Bachelor of Engineering in Information Science, Amritha Varshini has been working as a Linux Admin for Liquid Web since 2018. Varshini has worked on multiple operating systems, such as CentOS, AlmaLinux, and Ubuntu and on servers, like VPS, cloud, shared, reseller, and dedicated.

Latest Articles

In-place CentOS 7 upgrades

Read Article

How to use kill commands in Linux

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article

Change cPanel password from WebHost Manager (WHM)

Read Article

Change the root password in WebHost Manager (WHM)

Read Article