Nginx

Nginx Include Directive

NGINX, pronounced as (Engine-X), is a powerful and widely-used web server and reverse proxy server software. It is designed to handle high-concurrency and high-performance scenarios with its efficient event-driven architecture, advanced load balancing capabilities, and reverse proxy functionality.

The “include” directive is one of the most valuable and powerful directives when configuring NGINX. In this tutorial, we will explore what this directive entails and how to use it to extend and configure the power of NGINX.

Requirements:
To follow along with this tutorial, you need to have the following:

  1. Basic understanding of Linux terminal commands.
  2. Installed NGINX Web Server on your system.

Let’s dive in!

NGINX Include Directive

The “include” directive is a handy feature that allows us to break down the NGINX configuration into smaller, reusable parts. This can drastically improve the readability of the configuration and make it easier to manage.

Using the “include” directive, we can insert the content of another file into the NGINX configuration file. It is good to keep in mind that the file that you wish to include should contain valid NGINX configuration and directives.

The syntax of the “include” directive is as shown in the following:

include path/to/file;

Where the path/to/file can be an absolute or relative file path within the filesystem.

How to Use the NGINX Include Directive

Suppose we have several server blocks in our NGINX configuration, each using the same list of directives for logging.

Instead of writing the logging directives in each server block, we can write them once in a separate file and include that file in each server block.

We can start by creating the “logging.conf” file which stores the logging directives in the /etc/nginx/conf.d/ directory.

$ sudo touch /etc/nginx/conf.d/logging.conf
$ sudo nano /etc/nginx/conf.d/logging.conf

We can then add the logging blocks as follows:

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

Once we define our configuration blocks, we can save the file and close the editor.

Then, we can include logging in the main configuration file or any conf file as shown in the following:

server {
    listen 80;
    server_name example.com;
   
    include /etc/nginx/conf.d/logging.conf;
   
    location / {
        root /var/www/html;
    }
}

Using the given syntax, we can access the “access” and “error” log directives which are defined in the “logging.conf” file.

Including Multiple Files

We can also use the wildcard characters in the “include” directive. This allows us to include multiple files that match a specific pattern.

For example:

include /etc/nginx/conf.d/*.conf;

The previous entry allows us to include all files that end with “.conf” in the /etc/nginx/conf.d/ directory.

Once you made changes to the NGINX configuration, we can verify it for syntax errors using the following command:

sudo nginx -t

If the configuration is valid, you should see an output as follows:

nginx: configuration file /etc/nginx/nginx.conf test is successful

Finally, reload the Nginx server to apply the changes using the following command:

sudo systemctl reload nginx

Conclusion

This tutorial explored how to work with the “include” directive in NGINX. The “include” directive is a powerful feature in NGINX that can make your configuration more manageable and readable.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list