Updated February 10, 2023
Introduction to Nginx Include
Nginx include makes our configuration easier to maintain; include is used to split our configuration files into a set featuring specific files stored in the /etc/nginx/ nginx.conf configuration files directory. Using the nginx include directive, we can reference the content for the feature-specific files. Nginx includes a directive to include files or match the specified mask into our configuration files.
What is Nginx Include?
While using nginx include directive, the included file should consist of directives and blocks which was syntactically correct. We can use the included files anywhere in our configuration files to help to be more readable and enable us to reuse parts of our configuration. When using nginx includes, we need to ensure that our nginx configuration is syntactically correct for the configuration directives and blocks. By using nginx include, we are specifying the path of the included files, which we are including by using the include directive. It is essential and valuable to include files.
Why Use Nginx Include?
We will include a directive for adding the extra configuration files per server blocks we use in our project. For example, suppose we have placed the server block settings specific configuration files in a particular location that is readable and writable for the server block; if the configuration file’s location is correct, it will make the automatic changes.
The nginx configuration file default will include the /etc/nginx/conf.d/ if suppose we have placed our configuration files in that location, then we do not need to involve directives in our configuration files. However, suppose we do not include our configuration file in a specified location. In that case, we need to add the include directive into the configuration files to add or load the configuration files.
Suppose our default configuration file is too big; it isn’t easy to find the words and manage the files. The below example shows how we can include the file by using the include directive in nginx.
Code:
include conf.d/http;
include conf.d/stream;
include conf.d/exchange-enhanced;
Output:
We can use the different types of wildcard operators by using the include directive in nginx. In the below example, we can see that if suppose we want to specify only a single file, then we are giving the whole path of the file; also, we are providing the correct file name to find the file accurately if suppose we have not given the correct filename then it will not load the configuration files correctly it will show the syntax error to us.
Code:
include /etc/nginx/conf.d./include.types;
include /etc/nginx/conf.d/stream
include /etc/nginx/conf.d/http;
Output:
In the above example, we can see that nginx will search only that specified file into that location after giving the specified path and name into the configuration file. Hence, the syntax errors chances are very low, and we can easily find our file.
Suppose we don’t know the name of our specified configuration file, which we are adding to the configuration file by using the include directive. We can use the * before the configuration files. We can also use the asterisk (*) sign before the configuration filename, which we include in the configuration files. While using the asterisk sign before the filename, it will search from all the files containing the specified file, and if suppose they have found multiple files with the same name, then all the files will be added by using the include directive in nginx. The example below shows a user of an include directive using an asterisk sign in nginx.
Code:
include /etc/nginx/conf.d/*.conf;
Output:
Nginx Include Configuration
Below steps shows how we include the nginx configuration as follows:
In the example below, we are defining how to include the configuration files using the include directive. To use configuration, we need to install nginx in our system; we can install the nginx server using source, rpm, and source installation.
1. In the first step of nginx, including configuration, we install the nginx server. We can install the server of nginx by using the apt-get command in a ubuntu system. Suppose we use another Linux flavor; then, we can also use the rpm or yum command to install the nginx server.
Code:
apt-get install nginx
Output:
2. The nginx server is not started by default; after installing the same on the ubuntu system, we need to start it manually; we can start the nginx server by using the “service nginx start” command. After beginning the nginx server, we can check the status of the nginx server by using the “service nginx status” command. In the below example, we can see the version of the nginx server, and also, we can see the module which we included in the nginx server.
Code:
service nginx start
service nginx status
nginx –V
Output:
3. After starting and checking the status of the nginx server, we create the directory and the configuration file for including the same into the config file.
Code:
mkdir -p /var/www/include/html
vi /etc/nginx/sites-available/nginx_include.conf
Output:
4. After creating the directory and configuration files now, we open the configuration files for adding the configuration files by using the include directive.
5. After opening the file now, we include this file into the configuration file by using the include directive. In the example below, we are using the file’s full name; we are not using any asterisk sign to include the file.
Code:
include /etc/nginx/sites-available/nginx_include.conf
Output:
6. In the below example, we are using the asterisk sign to add our file into the configuration files as follows.
Code:
include /etc/nginx/sites-available/*.conf
Output:
7. After including the file using the include directive now, we are checking the config file’s syntax and restarting the nginx server.
Code:
nginx –t
service nginx restart
Output:
Conclusion
We will include a directive for adding the extra configuration files per server blocks we use in our project. Nginx includes directives to include files or match the specified mask into our configuration files.
Recommended Articles
This is a guide to Nginx Include. Here we discuss the introduction; why use nginx include? And configuration, respectively. You may also have a look at the following articles to learn more –