Hey, everyone.
And welcome back.
Now, in today's video, we will be discussing about the include directive that is part of the Http
context in Nginx configuration file.
Now, if you look into the Http context, that is part of the Nginx.conf file.
It basically contains the default parameters over here.
Now when you work in a production environment, you can have multiple websites that are running on top
of Nginx.
Now when you have multiple websites, then you will have to specify the associated configuration in
this specific Http context.
Block And soon what will happen in production environment is that this context block becomes very big
and it also becomes a little difficult to manage.
And hence it is recommended that instead of adding new configuration in the default nginx.conf file
under the Http context, what you can do, you can basically make use of a include directive.
So include directive allows us to include another file into a configuration.
For example, you have include here followed by Nginx.conf dot de start conf.
So now what will happen is any configuration that you specify under this specific directory in a file
of star dot conf, those configurations will be added as part of this http context without manually
modifying this specific nginx.conf file.
So this is the basics of include directive.
Let's go ahead and explore this.
So let's do a quick cat on the nginx.conf.
And in this Http context here you have a directory of include followed by the path.
The path is e.t.c., nginx.conf dot d star conf.
So basically any file in the conf directory which has the extension of dot conf.
So if I quickly do an ls hyphen l.
So basically currently we are within the e.t.c. enginex directory and within this directory we have
a conf dot d directory.
So let's quickly go to conf directory and if I do an ls you have a default dot conf file.
So whenever a nginx starts it will also load the configuration that is part of this default dot conf.
Now if you quickly do a cat on default dot conf, one thing that you will see is a server block.
Now this server block is very important primarily because here you define various nginx configuration
specific data like what is the port where the nginx should listen from, where should it load the index.html
file?
For example, when you open up the website in the browser, this is the HTML file that is being loaded.
Now from where exactly is this HTML file coming from?
So that is what is defined here.
So under the location you have specified the directory which is usr share nginx.
HTML and under this directory this is the index.html file whose contents are being displayed over here.
So instead of adding all of these details under the main nginx.conf, the developers have added it under
the default dot conf and within here you can specify all of the required details.
For example, let's do one thing, let's go ahead and modify this default dot conf over here and let's
go ahead and remove this curly braces.
All right.
Now we have not really touched the Nginx.conf which is the main enginex configuration file.
Now if you do a nginx hyphen t immediately you see you got an error stating that a curly braces is missing
in the default dot conf.
So whenever you do a nginx hyphen t or whenever you restart nginx nginx will not only go ahead and load
the nginx.conf but also all of the configuration files that are part of the include directive.
So let's go ahead and change back and fix the file.
So now if you do a nginx hyphen t things seems to be working perfectly well in a similar way.
If you just create a random file, let's just call it as temp dot conf.
And I'll just add a curly brace here.
Now if I do a hyphen t again immediately you see you got an error associated with the temp dot conf
and this error was primarily because let's first go ahead and remove the temp conf here.
Let's go back and let's open up the nginx.conf.
And the error was primarily because all of the files that are ending with the dot conf extension under
this specific directory will be loaded.
And since we had a file called as temp dot conf, even the configurations that were part of it got loaded.
All right.
So I hope you understood the importance of the include directive in production environment.
You will not really be touching this specific section extensively.
What you will be doing.
You will be going ahead and going to the cornfield and depending upon the website and its associated
configuration you need, you will be creating your own configuration file or maybe modifying the default
conf accordingly.
All right.
So just to revise the workflow, you have a nginx.conf, you include multiple files, let's say custom
01.02. conf and so on.
And ultimately nginx final configuration would be the nginx.conf along with the custom zero one conf
custom zero two conf and depending upon the configuration nginx will go ahead and work accordingly.
So that's the basics of the include directive.
And with this we'll conclude today's video.