WEBVTT

00:00.170 --> 00:00.650
Hey, everyone.

00:00.650 --> 00:01.510
And welcome back.

00:01.520 --> 00:04.970
Now in today's video, we will be discussing about the server blocks.

00:05.000 --> 00:10.100
Now, server block is a very important area which you will be extensively working with in Nginx.

00:10.100 --> 00:11.750
So let's quickly get started.

00:11.930 --> 00:18.170
So at a high level overview, Nginx makes use of server block to group the configuration detail and

00:18.170 --> 00:21.410
also host multiple domains on a single server.

00:21.410 --> 00:26.960
Now by default, let's look into the default server block that comes with Nginx.

00:26.960 --> 00:29.300
So this is how the server block looks like.

00:29.300 --> 00:36.470
And this is basically a default dot conf, which is part of the conf directory and this gets added to

00:36.470 --> 00:39.790
the http context using the include directive.

00:39.800 --> 00:44.630
Now this server block basically contains multiple different directives.

00:44.630 --> 00:47.030
First one is listen and you have a port.

00:47.030 --> 00:52.910
So this is very easy to understand, which basically means on which port the nginx should listen.

00:52.910 --> 00:55.670
And along with that you have a server underscore name.

00:55.670 --> 00:58.430
We'll be discussing more on this in detail at a later part.

00:58.460 --> 01:00.870
However, let's focus on location.

01:00.870 --> 01:07.380
So location basically means from which location, the index.html or any website file that you might

01:07.380 --> 01:08.700
have should be loaded.

01:08.700 --> 01:16.080
So whenever a user opens up the domain or whenever a user opens up the IP, some HTML file or some kind

01:16.080 --> 01:17.430
of a web file should load.

01:17.430 --> 01:24.690
So from which location the web file should load is part of the location directive under the server block.

01:24.690 --> 01:25.770
So let's do one thing.

01:25.770 --> 01:27.840
Let's go ahead and look into it.

01:27.840 --> 01:36.750
So currently I am within the Nginx conf directory and we have a default dot conf file that is available.

01:36.750 --> 01:44.310
So if I'll quickly do a cat on default dot conf if you go a bit up you basically have a server block

01:44.310 --> 01:49.920
and along with that you have various other configuration which is commented out.

01:49.920 --> 01:53.520
So this is primarily for the documentation purpose.

01:53.520 --> 01:59.010
If you want to see on how things can work, they have added a sample snippet as well.

01:59.040 --> 01:59.730
Great.

01:59.730 --> 02:05.670
So as we were discussing, first one under the server block is the listen directive and the port is

02:05.670 --> 02:06.060
80.

02:06.090 --> 02:08.980
Now if you quickly do a netstat a.l.p.

02:09.240 --> 02:09.720
All right.

02:09.720 --> 02:13.530
So it basically states that the netstat command not found.

02:13.530 --> 02:17.190
So let's quickly look into how we can download it.

02:17.190 --> 02:18.240
So I'll say yum.

02:18.240 --> 02:24.480
What provides netstat and netstat is coming from a base OS over here.

02:24.480 --> 02:29.250
So this is the repo and this is basically the part of the networking tool.

02:29.250 --> 02:34.950
So let's go ahead and also download net hyphen tools here.

02:36.000 --> 02:36.600
Perfect.

02:36.930 --> 02:39.570
So now let's quickly do a netstat hyphen.

02:40.170 --> 02:46.500
P And currently you see that you have a Nginx that is running on port of 80.

02:46.530 --> 02:50.160
Now it is not always essential that it should run on port 80.

02:50.190 --> 02:53.760
Many organizations, they make use of a custom port as well.

02:53.760 --> 02:55.290
So how you can modify it.

02:55.320 --> 02:58.950
We know we can go ahead and change the defaults dot conf.

02:58.980 --> 03:02.880
Let's change this from port 80 to port 8080.

03:03.300 --> 03:04.580
I'll go ahead and save it.

03:04.590 --> 03:10.500
Let's quickly verify our configuration and let's quickly restart our nginx here.

03:10.920 --> 03:11.640
All right.

03:11.790 --> 03:17.910
Now if we run the netstat command yet again, you should see that nginx is now listening on port 80.

03:18.210 --> 03:25.620
Now whenever a web server is listening on a port, which is other than 80, then you'll have to explicitly

03:25.620 --> 03:27.640
specify the port within the browser.

03:27.660 --> 03:34.740
For example, when we add the IP address of our Nginx server, currently it is loaded, but let's do

03:34.740 --> 03:39.900
a hard refresh here to ensure that there is no caching that happens here immediately.

03:39.910 --> 03:43.960
Here you basically see it states that the site cannot be reached.

03:43.960 --> 03:45.370
Connection refused.

03:45.400 --> 03:52.000
All right, so now in order for the website to work what you need to do, you will have to specify the

03:52.000 --> 03:55.140
IP address where the engine is listening, which is 80, 80.

03:55.150 --> 04:00.190
And as soon as you do that, you see our default page is coming up over here.

04:00.370 --> 04:00.850
Great.

04:00.850 --> 04:02.170
So this is one part.

04:02.200 --> 04:03.790
Second important part.

04:03.820 --> 04:07.840
Let's quickly do a cat on the defaults dot conf.

04:07.870 --> 04:14.140
The second important part that I wanted to show you is this location directory and within near the root

04:14.140 --> 04:16.240
is usr share nginx.

04:16.510 --> 04:17.240
HTML.

04:17.260 --> 04:19.540
So let's quickly go to this directory.

04:19.540 --> 04:22.000
I'll say usr share nginx.

04:22.210 --> 04:22.870
HTML.

04:23.500 --> 04:24.220
All right.

04:24.220 --> 04:32.980
And if i do an ls hyphen l you should see that we have an index dot HTML file and we have A50X dot HTML

04:32.980 --> 04:34.280
file that is available.

04:34.300 --> 04:40.240
Now if you look into the configuration under the location directory, the first path is basically the

04:40.240 --> 04:45.250
location from which the HTML files will be loaded or your website files will be loaded.

04:45.250 --> 04:47.920
And the second important part is the index.

04:47.920 --> 04:55.000
So basically whenever a user opens up your website, what is the default file that should load?

04:55.000 --> 04:59.230
And the default file is something that is specified at an index level.

04:59.230 --> 05:03.700
So one of the files that is specified here is index dot HTML.

05:03.700 --> 05:08.530
And this is one of the reasons why this index dot HTML file will be loaded.

05:08.530 --> 05:13.830
And if you quickly do a cat on index dot HTML file, it is nothing but the HTML file here.

05:13.840 --> 05:17.170
And this is the exact thing that we see within the browser.

05:17.170 --> 05:21.850
So let's go ahead and also modify this to see how Nginx would behave.

05:21.880 --> 05:26.770
Let's create a new file called as default dot HTML.

05:26.770 --> 05:30.700
And I'll say this is a custom index file.

05:31.480 --> 05:32.890
All right, I'll save this.

05:33.010 --> 05:41.830
And along with this, let's also modify our default dot conf file and within the index here let's change

05:41.830 --> 05:44.620
it to default dot HTML.

05:45.040 --> 05:45.490
All right.

05:45.490 --> 05:50.320
So by default this specific file will be loaded whenever someone visits the website.

05:50.320 --> 05:56.020
So let's quickly verify our configuration and let's quickly do a systemctl restart Nginx.

05:56.020 --> 06:02.800
So now let's refresh the page with Ctrl F5 and this time you see our custom index file is loaded.

06:02.830 --> 06:03.370
Great.

06:03.370 --> 06:07.150
So I hope with this you understood the basics of server block.

06:07.180 --> 06:13.300
Along with that, we were discussing about the lesson and the importance of this location block under

06:13.300 --> 06:14.710
the default dot conf.

06:14.740 --> 06:17.470
So that's the high level overview about the server block.

06:17.470 --> 06:19.000
In the later part of the videos.

06:19.000 --> 06:21.220
We'll be discussing about this in more detail.

06:21.250 --> 06:23.950
However, with this, we'll conclude today's video.
