Hi, everyone, and welcome back.
Now, in today's video, we will look into how we can host multiple websites in a single Nginx server.
So till this time we have been discussing about the default server block that comes with the Nginx installation.
Now this server block seems to be good enough.
Specifically if you have a single domain based website.
So what you can do, you can go ahead and whatever website that you have, whatever HTML files associated
with the website that you might have, you can store it in this specific root directory or you can even
have a modified root directory.
And whenever someone visits your server, the website will be loaded.
So this is good.
However, when you have multiple domains that are coming to the same server, let's say you have a labskin
and you have a example.com.
Two different domains are coming to the same server.
Now then what will happen is in case if you have this single server block, only the contents that are
part of this single server block will be loaded for both of them.
And this is something that we do not really want.
We want a different website for example.com, we want a different website for labskin and so on.
So in today's video we'll precisely be looking into how we can have a different set of websites for
multiple domains that are coming to the same server.
Now when we discuss about hosting multiple domains, what you need to do, you need to have a different
server block altogether each for the specific domain.
For example, let's say you have a new domain of KP Labs dot internal.
So this is a new domain and you do not want this default nginx page to be loaded there.
So what you will do, you will create a new server block and in the server name you will specify the
new domain name, which is KP labs dot internal and under the location as well.
You will see this is a different location.
So there is a new folder of KP Labs that is appended in this route.
So the index dot HTML file will be loaded from user share.
Nginx.
HTML kp labs folder and this index dot HTML file will be specific for the KP labs dot internal.
So for example, whenever a user makes a request to the KP labs dot internal nginx will first verify
the host header whether it is KP labs dot internal or a different website.
If it is KP Labs dot internal, then the request will be routed to this specific server block.
If the domain is not KP Labs dot internal, then the request will be routed to the first default server
block, whichever is available.
So this is the basics on how you can host multiple domains.
So let's try it out.
So currently within the Nginx Conf.d directory, let's quickly open up the default dot conf and basically
we have the base default dot conf file, whichever comes as a native installation.
So what we'll be doing, this default configuration file will be associated with any other website.
However, we'll want to create a new configuration file for a new domain college KP labs dot internal.
So what we'll do, let's go ahead and copy this base configuration file here and we'll create a new
configuration called as KP Labs dot conf and I'll paste the contents here.
So since there are two curly braces that are opening, you also have to specify one more curly braces.
So two curly braces that are closing.
Now, the first thing as we were discussing is we'll have to modify the server name here.
So let's change the server name to KP Labs dot Internal.
So any time a request is made to this domain, then this specific location will be applied upon.
Now, in the location as well, let's append a new folder name called as KP Labs and the index will
let it be the same.
We'll go ahead and save the change.
Along with that, let's quickly go to user share Nginx.
HTML and within here we'll create a new folder, let's call it as KP Labs.
And inside this specific folder, let's also create a new file called as index dot HTML and let's just
call it as this is the KP Labs website.
All right, that is great.
So let's quickly go back to Nginx conf.d directory.
We should have two conf files that are available since we have modified the configuration.
Always verify with nginx hyphen t and thing seems to be successful here.
So now what we'll do, we'll go ahead and quickly restart the nginx over here.
Perfect.
Now what we want is we want a request to go to a domain called as KP labs dot internal.
Now there is no public domain of KP labs dot internal that is available.
So what we'll be doing, we'll be modifying the Etsy host directory of the Linux server itself and we'll
be adding a local host for this domain.
So let's modify.
Etsy host directory.
And here what we'll do.
We'll specify 127001 CPU labs dot internal.
So basically, any time we make a request to this domain, it should direct to this localhost IP address,
which is the same server.
I'll go ahead and save the change.
Great.
So first let's go ahead and make a curl request to localhost.
This should basically show you the big index.html file.
This is completely fine.
The next important part is we'll make a curl request to CP labs or internal.
And this time you see we are getting a different website altogether.
Great.
So this is one part.
Second part that I wanted to show you.
Let's quickly open up the CP labs dot conf.
Now generally what really happens here is that if you have multiple websites, it is generally recommended
you have a separate set of access logs, so it becomes much more easier to go ahead and look into the
logs.
So for CP labs dot internal, we'll have a separate logs altogether.
So instead of access log, I'll just say CP labs, dot access dot log.
So all of the logs associated with this specific domain will be stored here.
I'll go ahead and I'll save the change.
Let's do a systemctl restart nginx.
Perfect.
So now let's also make a curl request to CP labs dot internal and as expected, we are getting a website.
So now to verify if the logs are appearing, let's go to var log nginx.
I'll do a LS and this time you should see a access dot log and you should also see a CP labs dot access
dot log.
And if we quickly do a cat on CP labs dot access dot log, you should see that you have one request
that is coming from the IP address of 127001.
This is the timestamp and this is the client which is curl.
Great.
So I hope with this you understood the basics on how you can host multiple domain based websites in
Nginx.
Now also note that specifically when you go across multiple web server based learning.
So in Nginx this is referred to as a server block.
In Apache, this is referred to as a virtual host.
So ultimately both of these things are the same.
However, there are certain terminologies that would be different specifically in terms of naming definition
between both of these web servers.
And one primary one is the virtual host mapping to that of server blocks in Nginx.
So with this, we'll conclude today's video.