0
0
Nginxdevops~30 mins

Separate config files per site in Nginx - Mini Project: Build & Apply

Choose your learning style9 modes available
Separate Config Files Per Site in Nginx
📖 Scenario: You are managing a web server that hosts multiple websites. To keep things organized and easy to maintain, you want to create separate configuration files for each website instead of putting all settings in one big file.
🎯 Goal: Learn how to create individual Nginx configuration files for two different websites and enable them by including them in the main Nginx configuration.
📋 What You'll Learn
Create separate config files for two sites: site1.conf and site2.conf
Each config file must define a server block with the correct server_name and root directory
Add an include directive in the main Nginx config to load these site configs
Test the configuration by printing the included files
💡 Why This Matters
🌍 Real World
Web servers often host multiple websites. Keeping each site's configuration separate makes it easier to manage and update without affecting others.
💼 Career
System administrators and DevOps engineers use this technique daily to organize web server configurations and deploy multiple sites efficiently.
Progress0 / 4 steps
1
Create separate config files for two sites
Create two separate Nginx config files named site1.conf and site2.conf. In site1.conf, write a server block with server_name site1.example.com; and root /var/www/site1;. In site2.conf, write a server block with server_name site2.example.com; and root /var/www/site2;.
Nginx
Need a hint?

Each config file should have a server block with listen 80;, server_name, and root directives.

2
Add include directive in main Nginx config
In the main Nginx configuration file (e.g., nginx.conf), add an include directive to load all config files from the /etc/nginx/sites-enabled/ directory.
Nginx
Need a hint?

The include directive should point to the directory where your site config files are stored.

3
Simulate enabling site config files
Create symbolic links for site1.conf and site2.conf inside the /etc/nginx/sites-enabled/ directory to enable these sites.
Nginx
Need a hint?

Use the ln -s command to create symbolic links for each site config file.

4
Print included config files to verify
Print the list of files included by the include /etc/nginx/sites-enabled/*; directive by running ls /etc/nginx/sites-enabled/.
Nginx
Need a hint?

Use the ls command to list the files in the /etc/nginx/sites-enabled/ directory.