WEBVTT

00:00.140 --> 00:00.710
Hey, everyone.

00:00.710 --> 00:01.700
And welcome back.

00:01.700 --> 00:06.280
Now in today's video, we will be discussing about the Nginx architecture.

00:06.290 --> 00:12.560
Now, one of the most important things specifically to tune Nginx according to your requirement, is

00:12.560 --> 00:14.690
the Nginx configuration file.

00:14.690 --> 00:19.610
And this is something that you will be playing around with a lot in your production environments.

00:19.610 --> 00:26.900
So Nginx configuration file basically consists of various settings based on which the Nginx will run

00:26.900 --> 00:28.640
and process the request.

00:28.670 --> 00:36.830
Now the primary Nginx configuration file is called as Nginx.conf and it is generally stored in the directory

00:36.830 --> 00:38.360
of Nginx.

00:38.360 --> 00:44.360
Now when we discuss about this directory of etcd, nginx, do note that depending upon the operating

00:44.360 --> 00:47.840
system that you use, this directory might change.

00:47.840 --> 00:55.100
So for example, you have a nginx.conf file here and within here there are two specific configuration

00:55.100 --> 00:56.600
parameters that are defined.

00:56.630 --> 01:02.400
First one is user is equal to nginx and second is log is equal to var log Nginx.

01:02.400 --> 01:05.940
So from the name itself, we can identify what it tries to do.

01:05.970 --> 01:12.720
User is equal to Nginx basically defines the user based on which the Nginx process should run and the

01:12.720 --> 01:18.270
log basically specifies the path where the nginx should store its logs.

01:18.270 --> 01:22.950
So all of these are basically stored in a file called as Nginx.conf.

01:22.950 --> 01:24.990
So let's quickly explore this.

01:24.990 --> 01:30.960
So currently I'm logged in to the server where Nginx is installed and if we'll quickly verify if Nginx

01:30.960 --> 01:37.380
is running or not, let's do a system Ctl status Nginx and as expected, here you see it is active and

01:37.380 --> 01:37.740
running.

01:37.740 --> 01:39.690
I'll press Q to exit.

01:39.720 --> 01:40.320
Great.

01:40.350 --> 01:47.340
Now as we were discussing, the Nginx configuration file is stored in the directory of etcd Nginx.

01:47.580 --> 01:52.050
So if I do an LS over here, you should see Nginx.conf.

01:52.050 --> 01:56.820
Now this is specifically this directory specifically related to RedHat based systems.

01:56.820 --> 01:57.990
For other systems.

01:57.990 --> 02:00.480
It can happen that this path might change.

02:00.480 --> 02:05.250
However, the main important thing is the nginx.conf understanding here.

02:05.250 --> 02:11.880
So let's quickly open up Nginx.conf and let's look into what exactly it contains.

02:11.910 --> 02:16.620
Now, if you look into this file, there are various directives that are available.

02:16.620 --> 02:18.780
First one is user of Nginx.

02:18.780 --> 02:25.230
This is something that we were discussing on how Nginx.conf can contain multiple configuration settings

02:25.230 --> 02:27.300
based on which Nginx will function.

02:27.300 --> 02:34.200
So the first one is user Nginx and along with that there are few more other parameters which we'll be

02:34.200 --> 02:35.700
discussing in just a moment.

02:35.730 --> 02:41.280
Now one important part to remember here, in fact, let me quickly show you so if we'll quickly do a

02:41.310 --> 02:49.830
ps F with hyphen hyphen forest with the grep of nginx over here, you should see that your nginx is

02:49.830 --> 02:55.320
referring to the nginx.conf file which is stored in this specific directory.

02:55.320 --> 03:00.750
So this is also one of the quick ways in case if you are not able to find nginx.conf in a different

03:00.750 --> 03:05.880
operating system, this is one of the ways in which you can identify where your primary configuration

03:05.880 --> 03:06.660
file lies.

03:06.690 --> 03:12.930
Now, one more important thing that you will see over here is that it is showing you that there is a

03:12.930 --> 03:18.210
Nginx master process and there is also a Nginx worker process.

03:18.210 --> 03:21.060
So let's quickly look into what exactly these are.

03:21.090 --> 03:28.020
Now do note that Nginx has one master process and can have one or more worker processes.

03:28.020 --> 03:35.040
So the primary aim of the master process is to read and evaluate the configuration files as well as

03:35.040 --> 03:38.310
maintain the appropriate number of worker processes.

03:38.310 --> 03:43.020
So here you have one master process and there can be multiple worker processes.

03:43.020 --> 03:44.870
And what does the worker process do?

03:44.880 --> 03:51.030
The worker process actually does the processing of requests, whatever requests that is being sent to

03:51.030 --> 03:56.070
your server, The actual processing of requests is something that the worker process does.

03:56.100 --> 04:03.600
Now, when we quickly do a cat on Nginx.conf, the user that you defined over here, you see the user

04:03.600 --> 04:04.620
is Nginx.

04:04.620 --> 04:10.740
So this basically defines what is the user that the Nginx worker process should use.

04:10.740 --> 04:17.220
So if you look into the highlighted area, the master process is running with the root user and the

04:17.220 --> 04:18.500
worker process here.

04:18.510 --> 04:23.940
This worker process is running with the user that is defined within the configuration file.

04:23.940 --> 04:26.340
Very important part to remember here.

04:26.370 --> 04:34.560
Now one more important thing, let's quickly open up the nginx.conf is that in case if you do a mistake

04:34.560 --> 04:38.100
in this specific file, let's intentionally do a mistake.

04:38.130 --> 04:38.700
All right.

04:38.700 --> 04:44.220
So we'll remove this curly braces and I'll save the Nginx configuration file.

04:44.220 --> 04:52.110
Now, at this stage, if we'll quickly do a systemctl status, nginx, you see, Nginx is still working

04:52.110 --> 04:59.520
perfectly well and the primary reason why it is working is because whenever you make change to the nginx

04:59.520 --> 04:59.850
config.

05:00.230 --> 05:00.440
File.

05:00.470 --> 05:02.850
So in this case, this is our configuration file.

05:02.870 --> 05:09.770
Whenever you make changes to this configuration file, this change is not immediately loaded to the

05:09.770 --> 05:11.960
running nginx web server.

05:11.960 --> 05:18.680
So in case if you have made some change in order for that change to come into effect, one of the ways

05:18.680 --> 05:21.090
which you can use is restart Enginex.

05:21.110 --> 05:29.600
So I'll say systemctl, restart nginx and immediately here you see, you got an error over here.

05:29.720 --> 05:37.430
So if we'll quickly do a systemctl status Nginx you see at this moment the nginx has failed.

05:37.430 --> 05:44.450
So that basically means any new request that we send to Nginx that request should also fail.

05:44.450 --> 05:51.200
So if I'll quickly send a request to the IP address of the server where Nginx is running, you see the

05:51.200 --> 05:53.510
request is getting connection refused.

05:53.540 --> 06:00.720
Now within the log you'll also see the exact reason why it is failing, which is stating unexpected

06:00.720 --> 06:03.240
end of file expecting a curly braces.

06:03.240 --> 06:09.210
So this is the curly braces that we had removed in this specific file of nginx nginx.conf.

06:09.210 --> 06:13.020
So let's go ahead and quickly edit the nginx.conf yet again.

06:13.530 --> 06:15.390
We'll add the curly brace.

06:15.390 --> 06:20.370
Let's save this and let's do a systemctl restart nginx.

06:20.400 --> 06:20.940
All right.

06:20.970 --> 06:27.900
Now if we'll quickly do a systemctl status nginx here you should see nginx is up and running and within

06:27.900 --> 06:28.470
the browser.

06:28.470 --> 06:31.020
Also our nginx page is being reflected.

06:31.020 --> 06:34.620
So this is the basics of what nginx.conf is.

06:34.650 --> 06:39.720
Now whenever you're working with Nginx.conf, it is important to be very careful.

06:39.720 --> 06:44.970
Any wrong configuration that you add might affect the production traffic altogether.

06:44.970 --> 06:48.330
So let's quickly open up the Nginx.conf yet again.

06:48.330 --> 06:51.090
We were discussing about the user of Nginx.

06:51.090 --> 06:53.940
The second important part is the worker process.

06:53.940 --> 06:59.400
Now we were already discussing that the worker process actually takes care of handling the requests

06:59.400 --> 07:00.510
that are being received.

07:00.510 --> 07:04.890
And in the worker process here it is mentioned as auto.

07:04.920 --> 07:12.240
Now if you run the PS command yet again, you see that you have one master process and only one worker

07:12.240 --> 07:13.830
process that is available.

07:13.830 --> 07:19.470
So the auto setting that we had specified is only launching one worker process.

07:19.470 --> 07:28.560
Now, in case if you need more worker process here, you can change the setting from auto to two.

07:28.920 --> 07:30.270
We'll save this.

07:30.570 --> 07:37.320
Let's restart our nginx and let's run the PCF command yet again.

07:37.320 --> 07:43.740
And now you see there are two worker processes that are up and running and both of these worker processes

07:43.740 --> 07:48.780
are using the same user that you have specified within the Nginx configuration file.

07:48.960 --> 07:49.560
Great.

07:49.560 --> 07:52.170
Let's open up the Nginx corner again.

07:52.200 --> 07:54.420
The next important part is the error log.

07:54.420 --> 07:58.950
So this basically specifies the path where the error log will be stored.

07:58.950 --> 08:02.130
So the path is var log nginx error dot log.

08:02.130 --> 08:04.080
So let's quickly open this up.

08:04.080 --> 08:13.320
So i'll do ls hyphen l var log nginx error dot log and you should see that we have one file of error

08:13.320 --> 08:14.940
dot log that is available.

08:14.940 --> 08:15.480
Now.

08:15.480 --> 08:25.170
Let's quickly do a less on var log nginx error dot log and one of the log entries that you will see

08:25.200 --> 08:27.570
is the unexpected end of file.

08:27.570 --> 08:31.440
Expecting a curly braces in this specific file over here.

08:31.440 --> 08:34.770
So error dot log is a very important log file.

08:34.770 --> 08:40.200
And specifically this becomes important when your nginx web server is not working at all.

08:40.200 --> 08:47.550
So you will see the appropriate reason why nginx is not really working in this specific error dot log

08:47.550 --> 08:48.030
file.

08:48.060 --> 08:51.480
So let's quickly open up our nginx.conf again.

08:51.480 --> 08:58.200
And the last important directive that we'll be discussing is the PID, which basically contains the

08:58.230 --> 09:03.060
file that has the appropriate process ID, which is why run nginx PID.

09:03.360 --> 09:06.720
So first let's quickly do a CF yet again.

09:06.720 --> 09:10.590
So basically the Nginx process has a specific PID.

09:10.770 --> 09:21.240
Now if you quickly do a cat on Var run nginx PID, it basically contains the process ID which is 30218,

09:21.240 --> 09:26.700
which is basically the process ID associated with the master Enginex process over here.

09:26.730 --> 09:27.480
All right.

09:27.510 --> 09:35.220
Now I hope at this specific stage you understood at a high level overview on the basics of what a Nginx

09:35.220 --> 09:42.390
configuration file is all about and how, depending upon the setting that we need for our production

09:42.390 --> 09:49.140
environment, you can modify various configuration parameters that are part of this nginx.conf file.

09:49.140 --> 09:52.950
So let's quickly revise the important directives that we were looking into.

09:52.950 --> 09:58.530
First one is the user, which basically defines the user credentials which are being used by the worker

09:58.530 --> 09:59.040
process.

09:59.040 --> 09:59.790
Second is the.

09:59.800 --> 10:04.130
A worker underscore processes which defines the number of worker processes.

10:04.150 --> 10:10.000
Now, in this case, setting it to the number of available CPU cores would be a good start.

10:10.000 --> 10:16.270
So in case if you have a CPU cores of two, then to begin with it is important to set the worker process

10:16.270 --> 10:16.810
to two.

10:16.810 --> 10:22.840
Now you also have a value of auto that we were discussing which will try to auto detect it.

10:22.840 --> 10:28.260
So in case if you have one CPU core, it will go ahead and launch one worker process.

10:28.270 --> 10:34.180
The second is error underscore log, which specifies the path to the error log file, and the next is

10:34.180 --> 10:40.030
the PID, which basically defines a file where the process ID of the main nginx process would be stored.

10:40.030 --> 10:44.110
So this is the high level overview about some of the base configuration directives.

10:44.110 --> 10:49.930
In the upcoming video, we'll look into the Nginx configuration file and some of the very interesting

10:49.930 --> 10:52.000
configuration directives in more detail.

10:52.000 --> 10:54.400
So with this, we'll conclude today's video.
