WEBVTT

00:00.170 --> 00:01.630
Hi, everyone, and welcome back.

00:01.640 --> 00:07.460
Now, in today's video, we will look into how we can host multiple websites in a single Nginx server.

00:07.700 --> 00:14.180
So till this time we have been discussing about the default server block that comes with the Nginx installation.

00:14.180 --> 00:17.030
Now this server block seems to be good enough.

00:17.030 --> 00:19.940
Specifically if you have a single domain based website.

00:19.940 --> 00:25.970
So what you can do, you can go ahead and whatever website that you have, whatever HTML files associated

00:25.970 --> 00:31.250
with the website that you might have, you can store it in this specific root directory or you can even

00:31.250 --> 00:33.230
have a modified root directory.

00:33.230 --> 00:38.030
And whenever someone visits your server, the website will be loaded.

00:38.030 --> 00:39.080
So this is good.

00:39.110 --> 00:46.010
However, when you have multiple domains that are coming to the same server, let's say you have a labskin

00:46.010 --> 00:47.510
and you have a example.com.

00:47.540 --> 00:50.030
Two different domains are coming to the same server.

00:50.030 --> 00:55.580
Now then what will happen is in case if you have this single server block, only the contents that are

00:55.580 --> 00:58.850
part of this single server block will be loaded for both of them.

00:58.850 --> 01:01.290
And this is something that we do not really want.

01:01.320 --> 01:07.650
We want a different website for example.com, we want a different website for labskin and so on.

01:07.650 --> 01:13.080
So in today's video we'll precisely be looking into how we can have a different set of websites for

01:13.080 --> 01:16.380
multiple domains that are coming to the same server.

01:16.380 --> 01:23.070
Now when we discuss about hosting multiple domains, what you need to do, you need to have a different

01:23.070 --> 01:26.610
server block altogether each for the specific domain.

01:26.610 --> 01:30.510
For example, let's say you have a new domain of KP Labs dot internal.

01:30.510 --> 01:35.760
So this is a new domain and you do not want this default nginx page to be loaded there.

01:35.760 --> 01:42.270
So what you will do, you will create a new server block and in the server name you will specify the

01:42.270 --> 01:47.190
new domain name, which is KP labs dot internal and under the location as well.

01:47.190 --> 01:49.470
You will see this is a different location.

01:49.470 --> 01:54.120
So there is a new folder of KP Labs that is appended in this route.

01:54.120 --> 01:58.890
So the index dot HTML file will be loaded from user share.

01:58.890 --> 01:59.550
Nginx.

01:59.820 --> 02:06.420
HTML kp labs folder and this index dot HTML file will be specific for the KP labs dot internal.

02:06.420 --> 02:13.890
So for example, whenever a user makes a request to the KP labs dot internal nginx will first verify

02:13.890 --> 02:17.760
the host header whether it is KP labs dot internal or a different website.

02:17.760 --> 02:23.730
If it is KP Labs dot internal, then the request will be routed to this specific server block.

02:23.730 --> 02:31.440
If the domain is not KP Labs dot internal, then the request will be routed to the first default server

02:31.440 --> 02:33.450
block, whichever is available.

02:33.450 --> 02:36.960
So this is the basics on how you can host multiple domains.

02:36.960 --> 02:38.700
So let's try it out.

02:39.090 --> 02:47.220
So currently within the Nginx Conf.d directory, let's quickly open up the default dot conf and basically

02:47.220 --> 02:53.100
we have the base default dot conf file, whichever comes as a native installation.

02:53.100 --> 03:00.000
So what we'll be doing, this default configuration file will be associated with any other website.

03:00.030 --> 03:07.470
However, we'll want to create a new configuration file for a new domain college KP labs dot internal.

03:07.470 --> 03:13.620
So what we'll do, let's go ahead and copy this base configuration file here and we'll create a new

03:13.620 --> 03:20.100
configuration called as KP Labs dot conf and I'll paste the contents here.

03:20.100 --> 03:26.580
So since there are two curly braces that are opening, you also have to specify one more curly braces.

03:26.580 --> 03:28.680
So two curly braces that are closing.

03:28.680 --> 03:33.840
Now, the first thing as we were discussing is we'll have to modify the server name here.

03:33.840 --> 03:38.100
So let's change the server name to KP Labs dot Internal.

03:38.100 --> 03:45.030
So any time a request is made to this domain, then this specific location will be applied upon.

03:45.030 --> 03:51.960
Now, in the location as well, let's append a new folder name called as KP Labs and the index will

03:51.960 --> 03:53.280
let it be the same.

03:53.430 --> 03:55.380
We'll go ahead and save the change.

03:56.370 --> 04:00.150
Along with that, let's quickly go to user share Nginx.

04:00.390 --> 04:05.310
HTML and within here we'll create a new folder, let's call it as KP Labs.

04:05.310 --> 04:12.570
And inside this specific folder, let's also create a new file called as index dot HTML and let's just

04:12.570 --> 04:16.200
call it as this is the KP Labs website.

04:17.430 --> 04:19.140
All right, that is great.

04:19.140 --> 04:23.220
So let's quickly go back to Nginx conf.d directory.

04:23.220 --> 04:28.500
We should have two conf files that are available since we have modified the configuration.

04:28.500 --> 04:33.450
Always verify with nginx hyphen t and thing seems to be successful here.

04:33.450 --> 04:38.460
So now what we'll do, we'll go ahead and quickly restart the nginx over here.

04:38.490 --> 04:39.180
Perfect.

04:39.210 --> 04:45.960
Now what we want is we want a request to go to a domain called as KP labs dot internal.

04:45.960 --> 04:49.920
Now there is no public domain of KP labs dot internal that is available.

04:49.920 --> 04:55.920
So what we'll be doing, we'll be modifying the Etsy host directory of the Linux server itself and we'll

04:55.950 --> 04:58.380
be adding a local host for this domain.

04:58.380 --> 04:59.670
So let's modify.

04:59.990 --> 05:01.560
Etsy host directory.

05:01.580 --> 05:03.080
And here what we'll do.

05:03.110 --> 05:07.730
We'll specify 127001 CPU labs dot internal.

05:07.730 --> 05:14.000
So basically, any time we make a request to this domain, it should direct to this localhost IP address,

05:14.000 --> 05:15.410
which is the same server.

05:15.620 --> 05:17.360
I'll go ahead and save the change.

05:17.750 --> 05:18.350
Great.

05:18.350 --> 05:22.010
So first let's go ahead and make a curl request to localhost.

05:22.040 --> 05:25.670
This should basically show you the big index.html file.

05:25.670 --> 05:27.320
This is completely fine.

05:27.350 --> 05:32.210
The next important part is we'll make a curl request to CP labs or internal.

05:32.210 --> 05:36.770
And this time you see we are getting a different website altogether.

05:36.980 --> 05:37.670
Great.

05:37.670 --> 05:39.200
So this is one part.

05:39.230 --> 05:41.550
Second part that I wanted to show you.

05:41.570 --> 05:44.910
Let's quickly open up the CP labs dot conf.

05:44.930 --> 05:51.190
Now generally what really happens here is that if you have multiple websites, it is generally recommended

05:51.200 --> 05:57.830
you have a separate set of access logs, so it becomes much more easier to go ahead and look into the

05:57.830 --> 05:58.360
logs.

05:58.370 --> 06:03.060
So for CP labs dot internal, we'll have a separate logs altogether.

06:03.060 --> 06:08.970
So instead of access log, I'll just say CP labs, dot access dot log.

06:08.970 --> 06:14.660
So all of the logs associated with this specific domain will be stored here.

06:14.670 --> 06:16.800
I'll go ahead and I'll save the change.

06:16.800 --> 06:20.610
Let's do a systemctl restart nginx.

06:21.540 --> 06:22.440
Perfect.

06:22.440 --> 06:30.750
So now let's also make a curl request to CP labs dot internal and as expected, we are getting a website.

06:31.020 --> 06:36.390
So now to verify if the logs are appearing, let's go to var log nginx.

06:36.450 --> 06:43.560
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

06:43.560 --> 06:44.100
dot log.

06:44.100 --> 06:50.220
And if we quickly do a cat on CP labs dot access dot log, you should see that you have one request

06:50.220 --> 06:54.000
that is coming from the IP address of 127001.

06:54.000 --> 06:57.690
This is the timestamp and this is the client which is curl.

06:57.900 --> 06:58.530
Great.

06:58.530 --> 07:04.500
So I hope with this you understood the basics on how you can host multiple domain based websites in

07:04.500 --> 07:05.210
Nginx.

07:05.220 --> 07:11.100
Now also note that specifically when you go across multiple web server based learning.

07:11.100 --> 07:14.250
So in Nginx this is referred to as a server block.

07:14.250 --> 07:17.610
In Apache, this is referred to as a virtual host.

07:17.610 --> 07:20.250
So ultimately both of these things are the same.

07:20.250 --> 07:27.690
However, there are certain terminologies that would be different specifically in terms of naming definition

07:27.690 --> 07:29.730
between both of these web servers.

07:29.730 --> 07:35.370
And one primary one is the virtual host mapping to that of server blocks in Nginx.

07:35.370 --> 07:38.040
So with this, we'll conclude today's video.
