0
0
Nginxdevops~5 mins

Include directive for modular config in Nginx - Commands & Configuration

Choose your learning style9 modes available
Introduction
Managing large configuration files can be hard. The include directive lets you split your nginx settings into smaller files. This makes it easier to organize and update parts without touching the whole file.
When you want to separate your server settings from your main nginx configuration for easier updates.
When you have multiple websites and want each site’s config in its own file.
When you want to reuse common settings like security rules across different configs.
When you want to keep your main config clean and simple by moving complex parts to separate files.
When you want to quickly enable or disable parts of your config by adding or removing include lines.
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 /etc/nginx/conf.d/*.conf;
}

This is the main nginx configuration file.

The include /etc/nginx/conf.d/*.conf; line tells nginx to load all configuration files ending with .conf from the /etc/nginx/conf.d/ directory.

This allows you to keep site-specific or modular configs separate from the main file.

Commands
Check the nginx configuration syntax to make sure the include directive and all configs are valid before restarting.
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 configuration without stopping the server.
Terminal
sudo systemctl reload nginx
Expected OutputExpected
No output (command runs silently)
List the files in the conf.d directory to see which modular config files are included.
Terminal
ls /etc/nginx/conf.d/
Expected OutputExpected
example-site.conf security.conf
Key Concept

If you remember nothing else from this pattern, remember: the include directive lets nginx load extra config files so you can organize settings in smaller, manageable pieces.

Common Mistakes
Forgetting to test the config with 'nginx -t' after adding include files.
Nginx will fail to reload if there are syntax errors in any included file, causing downtime.
Always run 'sudo nginx -t' to check syntax before reloading nginx.
Using incorrect file paths or wildcards in the include directive.
Nginx won't find the files and will ignore them or throw errors.
Use absolute paths and correct wildcards like '*.conf' to include the right files.
Placing include directives inside wrong blocks like 'events' instead of 'http'.
Nginx expects certain directives only in specific blocks; wrong placement causes errors.
Put include directives for site configs inside the 'http' block.
Summary
Use the include directive in nginx.conf to load extra config files from a directory.
Test your configuration with 'nginx -t' before reloading to avoid errors.
Reload nginx to apply changes without downtime.