Nginx server blocks — equivalent to Apache’s virtual hosts — allow a single Nginx instance to serve multiple websites from one server. Each server block defines a distinct configuration scope that responds to specific domain names or IP addresses. On RHEL 8, additional server block configuration files are stored under /etc/nginx/conf.d/ and automatically loaded by the main nginx.conf. This tutorial guides you through creating and testing a Nginx server block for a custom domain.

Prerequisites

  • Nginx installed and running on RHEL 8 (see How to Install Nginx on RHEL 8)
  • A domain name pointing to your server’s public IP address, or a local /etc/hosts entry for testing
  • Root or sudo access on the server
  • HTTP traffic allowed through firewalld

Step 1 — Create the Website Document Root

Create a directory to hold your website’s files and set appropriate ownership:

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

Create a test index page:

echo "

example.com is working

" > /var/www/example.com/html/index.html

Step 2 — Apply the Correct SELinux Context

Because the document root is outside Nginx’s default path, you must label it with the correct SELinux file context so Nginx can read the files:

sudo semanage fcontext -a -t httpd_sys_content_t "/var/www/example.com(/.*)?"
sudo restorecon -Rv /var/www/example.com

Step 3 — Create the Server Block Configuration File

Create a new configuration file for your domain in /etc/nginx/conf.d/:

sudo nano /etc/nginx/conf.d/example.com.conf

Add the following server block:

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

    server_name example.com www.example.com;

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

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

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

Key directives explained:

  • listen 80 — accepts connections on port 80 for IPv4; [::]:80 for IPv6
  • server_name — the domain names this block responds to
  • root — the document root directory for this site
  • index — default files to serve when a directory is requested
  • try_files — serves the requested URI or returns a 404 if not found

Step 4 — Test and Reload Nginx

Always test the configuration syntax before reloading:

sudo nginx -t

If you see syntax is ok and test is successful, reload Nginx to apply the new server block:

sudo systemctl reload nginx.service

Step 5 — Test the Server Block

If you do not have DNS configured, add a temporary /etc/hosts entry to test locally:

echo "127.0.0.1   example.com www.example.com" | sudo tee -a /etc/hosts

Then verify the server block is serving the correct content:

curl http://example.com

You should see <h1>example.com is working</h1> in the output.

Step 6 — Adding a Second Server Block and Setting a Default

To host a second site, repeat steps 1–4 for site2.com. When Nginx receives a request that does not match any server_name, it falls back to the first server block defined or the one marked as default_server. To designate a specific block as the default:

listen 80 default_server;
listen [::]:80 default_server;

Only one server block may carry the default_server flag. After adding or editing any server block, run sudo nginx -t and then sudo systemctl reload nginx.service.

Conclusion

You have created a working Nginx server block on RHEL 8, configured the correct document root with proper SELinux labels, and tested it with curl. You can now host multiple independent websites on a single Nginx server by repeating this pattern for each domain.

Next steps: How to Secure Nginx with Let’s Encrypt and Certbot on RHEL 8, How to Set Up Nginx as a Reverse Proxy on RHEL 8, and How to Configure HTTP/2 on Nginx on RHEL 8.