Hey, everyone.
And welcome back.
Now in today's video, we will be discussing about the server blocks.
Now, server block is a very important area which you will be extensively working with in Nginx.
So let's quickly get started.
So at a high level overview, Nginx makes use of server block to group the configuration detail and
also host multiple domains on a single server.
Now by default, let's look into the default server block that comes with Nginx.
So this is how the server block looks like.
And this is basically a default dot conf, which is part of the conf directory and this gets added to
the http context using the include directive.
Now this server block basically contains multiple different directives.
First one is listen and you have a port.
So this is very easy to understand, which basically means on which port the nginx should listen.
And along with that you have a server underscore name.
We'll be discussing more on this in detail at a later part.
However, let's focus on location.
So location basically means from which location, the index.html or any website file that you might
have should be loaded.
So whenever a user opens up the domain or whenever a user opens up the IP, some HTML file or some kind
of a web file should load.
So from which location the web file should load is part of the location directive under the server block.
So let's do one thing.
Let's go ahead and look into it.
So currently I am within the Nginx conf directory and we have a default dot conf file that is available.
So if I'll quickly do a cat on default dot conf if you go a bit up you basically have a server block
and along with that you have various other configuration which is commented out.
So this is primarily for the documentation purpose.
If you want to see on how things can work, they have added a sample snippet as well.
Great.
So as we were discussing, first one under the server block is the listen directive and the port is
80.
Now if you quickly do a netstat a.l.p.
All right.
So it basically states that the netstat command not found.
So let's quickly look into how we can download it.
So I'll say yum.
What provides netstat and netstat is coming from a base OS over here.
So this is the repo and this is basically the part of the networking tool.
So let's go ahead and also download net hyphen tools here.
Perfect.
So now let's quickly do a netstat hyphen.
P And currently you see that you have a Nginx that is running on port of 80.
Now it is not always essential that it should run on port 80.
Many organizations, they make use of a custom port as well.
So how you can modify it.
We know we can go ahead and change the defaults dot conf.
Let's change this from port 80 to port 8080.
I'll go ahead and save it.
Let's quickly verify our configuration and let's quickly restart our nginx here.
All right.
Now if we run the netstat command yet again, you should see that nginx is now listening on port 80.
Now whenever a web server is listening on a port, which is other than 80, then you'll have to explicitly
specify the port within the browser.
For example, when we add the IP address of our Nginx server, currently it is loaded, but let's do
a hard refresh here to ensure that there is no caching that happens here immediately.
Here you basically see it states that the site cannot be reached.
Connection refused.
All right, so now in order for the website to work what you need to do, you will have to specify the
IP address where the engine is listening, which is 80, 80.
And as soon as you do that, you see our default page is coming up over here.
Great.
So this is one part.
Second important part.
Let's quickly do a cat on the defaults dot conf.
The second important part that I wanted to show you is this location directory and within near the root
is usr share nginx.
HTML.
So let's quickly go to this directory.
I'll say usr share nginx.
HTML.
All right.
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
file that is available.
Now if you look into the configuration under the location directory, the first path is basically the
location from which the HTML files will be loaded or your website files will be loaded.
And the second important part is the index.
So basically whenever a user opens up your website, what is the default file that should load?
And the default file is something that is specified at an index level.
So one of the files that is specified here is index dot HTML.
And this is one of the reasons why this index dot HTML file will be loaded.
And if you quickly do a cat on index dot HTML file, it is nothing but the HTML file here.
And this is the exact thing that we see within the browser.
So let's go ahead and also modify this to see how Nginx would behave.
Let's create a new file called as default dot HTML.
And I'll say this is a custom index file.
All right, I'll save this.
And along with this, let's also modify our default dot conf file and within the index here let's change
it to default dot HTML.
All right.
So by default this specific file will be loaded whenever someone visits the website.
So let's quickly verify our configuration and let's quickly do a systemctl restart Nginx.
So now let's refresh the page with Ctrl F5 and this time you see our custom index file is loaded.
Great.
So I hope with this you understood the basics of server block.
Along with that, we were discussing about the lesson and the importance of this location block under
the default dot conf.
So that's the high level overview about the server block.
In the later part of the videos.
We'll be discussing about this in more detail.
However, with this, we'll conclude today's video.