Hey, everyone.
And welcome back.
Now in today's video, we will be discussing about the Nginx architecture.
Now, one of the most important things specifically to tune Nginx according to your requirement, is
the Nginx configuration file.
And this is something that you will be playing around with a lot in your production environments.
So Nginx configuration file basically consists of various settings based on which the Nginx will run
and process the request.
Now the primary Nginx configuration file is called as Nginx.conf and it is generally stored in the directory
of Nginx.
Now when we discuss about this directory of etcd, nginx, do note that depending upon the operating
system that you use, this directory might change.
So for example, you have a nginx.conf file here and within here there are two specific configuration
parameters that are defined.
First one is user is equal to nginx and second is log is equal to var log Nginx.
So from the name itself, we can identify what it tries to do.
User is equal to Nginx basically defines the user based on which the Nginx process should run and the
log basically specifies the path where the nginx should store its logs.
So all of these are basically stored in a file called as Nginx.conf.
So let's quickly explore this.
So currently I'm logged in to the server where Nginx is installed and if we'll quickly verify if Nginx
is running or not, let's do a system Ctl status Nginx and as expected, here you see it is active and
running.
I'll press Q to exit.
Great.
Now as we were discussing, the Nginx configuration file is stored in the directory of etcd Nginx.
So if I do an LS over here, you should see Nginx.conf.
Now this is specifically this directory specifically related to RedHat based systems.
For other systems.
It can happen that this path might change.
However, the main important thing is the nginx.conf understanding here.
So let's quickly open up Nginx.conf and let's look into what exactly it contains.
Now, if you look into this file, there are various directives that are available.
First one is user of Nginx.
This is something that we were discussing on how Nginx.conf can contain multiple configuration settings
based on which Nginx will function.
So the first one is user Nginx and along with that there are few more other parameters which we'll be
discussing in just a moment.
Now one important part to remember here, in fact, let me quickly show you so if we'll quickly do a
ps F with hyphen hyphen forest with the grep of nginx over here, you should see that your nginx is
referring to the nginx.conf file which is stored in this specific directory.
So this is also one of the quick ways in case if you are not able to find nginx.conf in a different
operating system, this is one of the ways in which you can identify where your primary configuration
file lies.
Now, one more important thing that you will see over here is that it is showing you that there is a
Nginx master process and there is also a Nginx worker process.
So let's quickly look into what exactly these are.
Now do note that Nginx has one master process and can have one or more worker processes.
So the primary aim of the master process is to read and evaluate the configuration files as well as
maintain the appropriate number of worker processes.
So here you have one master process and there can be multiple worker processes.
And what does the worker process do?
The worker process actually does the processing of requests, whatever requests that is being sent to
your server, The actual processing of requests is something that the worker process does.
Now, when we quickly do a cat on Nginx.conf, the user that you defined over here, you see the user
is Nginx.
So this basically defines what is the user that the Nginx worker process should use.
So if you look into the highlighted area, the master process is running with the root user and the
worker process here.
This worker process is running with the user that is defined within the configuration file.
Very important part to remember here.
Now one more important thing, let's quickly open up the nginx.conf is that in case if you do a mistake
in this specific file, let's intentionally do a mistake.
All right.
So we'll remove this curly braces and I'll save the Nginx configuration file.
Now, at this stage, if we'll quickly do a systemctl status, nginx, you see, Nginx is still working
perfectly well and the primary reason why it is working is because whenever you make change to the nginx
config.
File.
So in this case, this is our configuration file.
Whenever you make changes to this configuration file, this change is not immediately loaded to the
running nginx web server.
So in case if you have made some change in order for that change to come into effect, one of the ways
which you can use is restart Enginex.
So I'll say systemctl, restart nginx and immediately here you see, you got an error over here.
So if we'll quickly do a systemctl status Nginx you see at this moment the nginx has failed.
So that basically means any new request that we send to Nginx that request should also fail.
So if I'll quickly send a request to the IP address of the server where Nginx is running, you see the
request is getting connection refused.
Now within the log you'll also see the exact reason why it is failing, which is stating unexpected
end of file expecting a curly braces.
So this is the curly braces that we had removed in this specific file of nginx nginx.conf.
So let's go ahead and quickly edit the nginx.conf yet again.
We'll add the curly brace.
Let's save this and let's do a systemctl restart nginx.
All right.
Now if we'll quickly do a systemctl status nginx here you should see nginx is up and running and within
the browser.
Also our nginx page is being reflected.
So this is the basics of what nginx.conf is.
Now whenever you're working with Nginx.conf, it is important to be very careful.
Any wrong configuration that you add might affect the production traffic altogether.
So let's quickly open up the Nginx.conf yet again.
We were discussing about the user of Nginx.
The second important part is the worker process.
Now we were already discussing that the worker process actually takes care of handling the requests
that are being received.
And in the worker process here it is mentioned as auto.
Now if you run the PS command yet again, you see that you have one master process and only one worker
process that is available.
So the auto setting that we had specified is only launching one worker process.
Now, in case if you need more worker process here, you can change the setting from auto to two.
We'll save this.
Let's restart our nginx and let's run the PCF command yet again.
And now you see there are two worker processes that are up and running and both of these worker processes
are using the same user that you have specified within the Nginx configuration file.
Great.
Let's open up the Nginx corner again.
The next important part is the error log.
So this basically specifies the path where the error log will be stored.
So the path is var log nginx error dot log.
So let's quickly open this up.
So i'll do ls hyphen l var log nginx error dot log and you should see that we have one file of error
dot log that is available.
Now.
Let's quickly do a less on var log nginx error dot log and one of the log entries that you will see
is the unexpected end of file.
Expecting a curly braces in this specific file over here.
So error dot log is a very important log file.
And specifically this becomes important when your nginx web server is not working at all.
So you will see the appropriate reason why nginx is not really working in this specific error dot log
file.
So let's quickly open up our nginx.conf again.
And the last important directive that we'll be discussing is the PID, which basically contains the
file that has the appropriate process ID, which is why run nginx PID.
So first let's quickly do a CF yet again.
So basically the Nginx process has a specific PID.
Now if you quickly do a cat on Var run nginx PID, it basically contains the process ID which is 30218,
which is basically the process ID associated with the master Enginex process over here.
All right.
Now I hope at this specific stage you understood at a high level overview on the basics of what a Nginx
configuration file is all about and how, depending upon the setting that we need for our production
environment, you can modify various configuration parameters that are part of this nginx.conf file.
So let's quickly revise the important directives that we were looking into.
First one is the user, which basically defines the user credentials which are being used by the worker
process.
Second is the.
A worker underscore processes which defines the number of worker processes.
Now, in this case, setting it to the number of available CPU cores would be a good start.
So in case if you have a CPU cores of two, then to begin with it is important to set the worker process
to two.
Now you also have a value of auto that we were discussing which will try to auto detect it.
So in case if you have one CPU core, it will go ahead and launch one worker process.
The second is error underscore log, which specifies the path to the error log file, and the next is
the PID, which basically defines a file where the process ID of the main nginx process would be stored.
So this is the high level overview about some of the base configuration directives.
In the upcoming video, we'll look into the Nginx configuration file and some of the very interesting
configuration directives in more detail.
So with this, we'll conclude today's video.