0
0
Nginxdevops~5 mins

Separate config files per site in Nginx - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you host multiple websites on one server, managing all settings in one big file gets confusing. Separating configuration files for each site helps keep things organized and easier to update.
When you want to host multiple websites on the same server without mixing their settings.
When you need to update one website's settings without affecting others.
When you want to enable or disable a website quickly by adding or removing its config file.
When you want to keep your server configuration clean and easy to understand.
When you want to share configuration files with others or back them up separately.
Config File - nginx.conf
nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';

    access_log /var/log/nginx/access.log main;

    sendfile on;
    keepalive_timeout 65;

    # Include separate site config files
    include /etc/nginx/sites-enabled/*;
}

This is the main nginx configuration file.

The key part is include /etc/nginx/sites-enabled/*; which tells nginx to load all configuration files inside the sites-enabled folder.

Each site will have its own config file in that folder, making management easier.

Commands
Create directories to store separate site config files and enable them.
Terminal
sudo mkdir -p /etc/nginx/sites-available /etc/nginx/sites-enabled
Expected OutputExpected
No output (command runs silently)
Create a config file for the site example.com with its own settings.
Terminal
sudo nano /etc/nginx/sites-available/example.com
Expected OutputExpected
No output (command runs silently)
Enable the site by linking its config file into the sites-enabled folder.
Terminal
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
Expected OutputExpected
No output (command runs silently)
Test nginx configuration for syntax errors before reloading.
Terminal
sudo nginx -t
Expected OutputExpected
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Reload nginx to apply the new site configuration without downtime.
Terminal
sudo systemctl reload nginx
Expected OutputExpected
No output (command runs silently)
Key Concept

If you remember nothing else from this pattern, remember: use separate config files per site and include them in the main nginx.conf for clean and easy management.

Common Mistakes
Editing the main nginx.conf directly for each site instead of using separate files.
It makes the file large and hard to manage, increasing risk of errors.
Create separate config files for each site in sites-available and enable them via sites-enabled.
Forgetting to test nginx configuration with 'nginx -t' before reloading.
Reloading with errors can cause nginx to fail and websites to go down.
Always run 'sudo nginx -t' to check for syntax errors before reloading.
Not creating a symbolic link in sites-enabled to enable the site config.
Nginx will not load the site config if it's only in sites-available.
Create a symbolic link from sites-available to sites-enabled to activate the site.
Summary
Create separate config files for each website in /etc/nginx/sites-available.
Enable sites by linking their config files into /etc/nginx/sites-enabled.
Include the sites-enabled folder in the main nginx.conf to load all site configs.
Test configuration syntax with 'nginx -t' before reloading nginx.
Reload nginx to apply changes without downtime.