Nginx is a powerful web server that allows you to host multiple domains on a single server by using server blocks. Similar to virtual hosts in Apache, server blocks encapsulate configuration details for different domains. In this guide, we will explore how to configure server blocks in Nginx on a Rocky Linux 9 server.
Before we begin, ensure that you have a non-root user with sudo privileges. If not, follow the Rocky Linux 9 initial server setup guide to create one. Additionally, make sure Nginx is installed on your server. You can install it by following the guide on “How To Install Nginx on Rocky Linux 9.”
Step 1: Setting Up New Document Root Directories
To set up multiple domains, we need to create new document root directories for each site. By default, Nginx serves documents from the /usr/share/nginx/html directory. However, we need additional directories for hosting multiple sites. We’ll consider the /usr/share/nginx/html directory as the default directory if a client request doesn’t match any specific site.
To create the necessary directories, run the following commands:
sudo mkdir -p /usr/share/nginx/example.com/html sudo mkdir -p /usr/share/nginx/test.com/html
These commands create the directory structure for our example.com and test.com domains. Adjust the domain names according to your requirements. The -p flag ensures that parent directories are created if they don’t exist.
Next, we need to assign ownership of the web directories to our user account. Run the following commands to do so:
sudo chown -R $USER:nginx /usr/share/nginx/example.com/html sudo chown -R $USER:nginx /usr/share/nginx/test.com/html
Replace $USER
with your username. The nginx group is automatically created for Nginx on Rocky Linux, allowing the web server to create new files if necessary.
Finally, adjust the permissions of the directories using the following command:
sudo chmod -R 775 /usr/share/nginx
These permissions ensure that your user and the nginx group have full permissions, while other users have read-only access.
Step 2: Creating Sample Pages for Each Site
Now that we have our directory structure set up, let’s create default pages for each site. These pages will be displayed when we visit the respective domains.
Using your preferred text editor, create an index.html file for your example.com domain:
nano /usr/share/nginx/example.com/html/index.html
Inside the file, create a basic web landing page that indicates the site you are accessing. You can use the following template:
<html>
<head>
<title>Welcome to Example.com!</title>
</head>
<body>
<h1>Success! The example.com server block is working!</h1>
</body>
</html>
Save and close the file.
Since the file for the test.com domain will be the same for demonstration purposes, you can copy it over:
cp /usr/share/nginx/example.com/html/index.html /usr/share/nginx/test.com/html/
Open the new file in your text editor:
nano /usr/share/nginx/test.com/html/index.html
Modify the file to reflect your second domain:
<html>
<head>
<title>Welcome to Test.com!</title>
</head>
<body>
<h1>Success! The test.com server block is working!</h1>
</body>
</html>
Save and close the file. Now we have sample pages for both domains.
Step 3: Creating Server Block Files for Each Domain
To instruct Nginx on how to serve the content, we need to create server block configuration files for each domain. The main configuration file, nginx.conf, contains a default server block.
Create the server block file for your first domain in the /etc/nginx/conf.d directory:
sudo nano /etc/nginx/conf.d/example.com.conf
Paste the following server block into the file:
server { listen 80; listen [::]:80; root /usr/share/nginx/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; } }
This server block listens on port 80, specifies the root directory, and sets the servername to match your first domain. Adjust the servername and root directory according to your requirements.
Save and close the file.
Now, let’s create the server block file for the second domain by copying the existing one:
sudo cp /etc/nginx/conf.d/example.com.conf /etc/nginx/conf.d/test.com.conf
Open the new file:
sudo nano /etc/nginx/conf.d/test.com.conf
Modify the file to match your second domain:
server { listen 80; listen [::]:80; root /usr/share/nginx/test.com/html; index index.html index.htm index.nginx-debian.html; server_name test.com www.test.com; location / { try_files $uri $uri/ =404; } }
Save and close the file.
Step 4: Enabling Server Blocks and Restarting Nginx
We now have three server blocks: example.com, test.com, and the default block.
Check the Nginx configuration files for syntax errors:
sudo nginx -t
If no errors are found, restart Nginx to apply the changes:
sudo systemctl restart nginx
Nginx should now be serving both of your domain names.
Step 5: Modifying Your Local Hosts File for Testing (Optional)
If you have been using placeholder domain names for testing instead of actual domains that point to your server’s IP address, you can modify your local computer’s hosts file to test the Nginx server block configuration.
On Linux or Mac, open the hosts file with sudo privileges:
sudo nano /etc/hosts
On Windows, open Notepad as an administrator and navigate to C:WindowsSystem32driversetchosts.
Add the following lines to the file, replacing the IP address with your server’s public IP address:
127.0.0.1 localhost
...
203.0.113.5 example.com www.example.com
203.0.113.5 test.com www.test.com
Save and close the file.
Modifying the hosts file allows you to test your Nginx server block configuration locally. Remember to remove these entries once you have finished testing.
Step 6: Testing Your Results
With everything set up, it’s time to test your server blocks. Open your web browser and visit the following URLs:
If both sites display the respective success messages, you have successfully configured two independent server blocks with Nginx.
Remember to remove the hosts file modifications if you made any.
Conclusion
Congratulations! You have learned how to configure Nginx server blocks on Rocky Linux 9, allowing you to host multiple domains on a single server. Server blocks are a powerful feature that provides flexibility and scalability for your web hosting needs.
By following the steps outlined in this guide, you have set up separate document root directories, created sample pages for each domain, and configured server block files. You have also tested your configuration to ensure that both domains are served correctly.
Nginx’s server blocks enable you to efficiently manage and serve multiple websites from a single server. This capability is particularly useful for businesses and developers who need to host multiple domains on a single infrastructure.
For more advanced configurations and optimizations, you can explore additional Nginx features and modules. Nginx offers a wide range of options to enhance performance, security, and scalability.
If you’re looking for a reliable cloud hosting provider to complement your Nginx server blocks, consider Shape.host. With their Cloud VPS solutions, you can enjoy the benefits of high-performance hosting and excellent customer support. Shape.host delivers efficient and scalable hosting services, ensuring that your websites are always available and perform optimally.