Support Online
Skip to main content

Nginx Server Block (Virtual Host) Installation – Rocky Linux 9

What will you learn in this guide?

This guide explains how to run multiple domains on a single server using Nginx on Rocky Linux 9.
You learn the server block logic and create a separate root directory and configuration for each site.

1. Creating Document Root Directories

  • A separate directory structure is required for each domain.
    This structure isolates the sites from each other.
sudo mkdir -p /usr/share/nginx/ornek-site.com/html
sudo mkdir -p /usr/share/nginx/test-site.com/html
  • These commands create the necessary folders for each site.

  1. Set directory ownership and permissions:

sudo chown -R $USER:nginx /usr/share/nginx/ornek-site.com/html
sudo chown -R $USER:nginx /usr/share/nginx/test-site.com/html
sudo chmod -R 775 /usr/share/nginx
  • This process allows Nginx to access the files.

2. Creating Sample HTML Pages

  1. Create a simple test page for each site.

nano /usr/share/nginx/ornek-site.com/html/index.html


<html>
<head>
<title>ornek-site.com</title>
</head>
<body>
<h1>ornek-site.com çalışıyor</h1>
</body>
</html>

  1. Copy and edit the file for the second site:

cp /usr/share/nginx/ornek-site.com/html/index.html /usr/share/nginx/test-site.com/html/
nano /usr/share/nginx/test-site.com/html/index.html
  • This page indicates that you have accessed the correct site.

3. Creating Nginx Server Block Files

  1. Nginx loads additional configurations from the conf.d directory in Rocky Linux 9.

Initial Domain Configuration


sudo nano /etc/nginx/conf.d/ornek-site.com.conf

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

root /usr/share/nginx/ornek-site.com/html;
index index.html;

server_name ornek-site.com www.ornek-site.com;

location / {
try_files $uri $uri/ =404;
}
}
  • This configuration satisfies the first domain name.

Second Domain Configuration


sudo cp /etc/nginx/conf.d/ornek-site.com.conf /etc/nginx/conf.d/test-site.com.conf
sudo nano /etc/nginx/conf.d/test-site.com.conf


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

root /usr/share/nginx/test-site.com/html;
index index.html;

server_name test-site.com www.test-site.com;

location / {
try_files $uri $uri/ =404;
}
}
  • This file manages the second site.

4. Testing Configuration and Restarting Nginx

  1. Check the syntax first:

sudo nginx -t
  • This command checks for configuration errors.

  1. If everything is OK, restart Nginx:

sudo systemctl restart nginx
  • Changes become active at this stage.

5. Local Testing with Hosts File (Optional)

  1. If your domain names are not DNS-oriented, you can do local testing.

sudo nano /etc/hosts


203.0.113.10 ornek-site.com www.ornek-site.com
203.0.113.10 test-site.com www.test-site.com
  • This process temporarily redirects domains to the server.

6. Final Check

  1. Visit these addresses from the browser:

Frequently Asked Questions (FAQ)

1. Should I use default_server? No. The default block is sufficient for unmatched requests.

2. How many sites can I run on the same server? There is no practical limit, as long as the hardware allows it.

3. What should I do if I want to add HTTPS? You must add SSL configuration in each server block.

4. Can I use sites-available instead of conf.d? The default structure in Rocky Linux is the conf.d directory.


Result

Multiple domain management using Nginx server block on Rocky Linux 9 is now ready. This structure provides a scalable and streamlined web server architecture. For production environments, you can easily implement this structure on GenixNode servers.