WEBVTT

00:00.080 --> 00:00.590
Hey, everyone.

00:00.590 --> 00:01.520
And welcome back.

00:01.520 --> 00:06.410
In today's video, we will be discussing about building dynamic modules.

00:06.530 --> 00:13.040
Now, generally modules that you might be referencing within the engine is typically the third party

00:13.040 --> 00:13.790
modules.

00:13.790 --> 00:20.250
So these modules needs to be compiled before you reference them within your production engine X.

00:20.300 --> 00:26.450
Now one very important part to remember that whenever you are compiling a module, you need to make

00:26.450 --> 00:33.190
sure that it is compiled against the same engine X version which is running in your production.

00:33.200 --> 00:40.250
So let's say that you have a third party module for web application firewall, so you will have to compile

00:40.250 --> 00:40.640
it.

00:40.670 --> 00:45.320
Now you will compile it against the same version of your production.

00:45.320 --> 00:53.780
So if your production engine X is running, let's say 1.16, then you compile that module against 1.16

00:53.780 --> 00:54.980
version only.

00:54.980 --> 01:01.980
If you compile it with a different version, then most probably when you reference that within your

01:01.980 --> 01:07.140
production engine x, the module will not really work perfectly.

01:07.140 --> 01:07.500
All right.

01:07.500 --> 01:09.600
So that is one important part to remember.

01:09.720 --> 01:17.310
Now, there are also certain repositories where you can get out of the box modules which does not need

01:17.310 --> 01:18.210
compilation.

01:18.210 --> 01:20.910
So that is also something that you can use.

01:20.910 --> 01:27.570
But ideally it is recommended that if you are running Nginx in production, just compile the modules

01:27.570 --> 01:31.170
instead of referencing to some third party repository.

01:31.320 --> 01:36.300
So in order to build dynamic modules, there are four steps which are required.

01:36.450 --> 01:40.320
The first step is fetch the Nginx source.

01:40.320 --> 01:46.920
So let's say your production engineering's version is 1.16, you fetch the 1.16 source.

01:46.950 --> 01:50.310
All right, Second is fetch the module source.

01:50.310 --> 01:55.230
So many of the modules are available in GitHub, so you fetch the source of them.

01:55.380 --> 01:58.770
Third step is to build the dynamic module.

01:58.770 --> 02:04.920
And once the module is built, you can reference that module within the Nginx configuration.

02:04.920 --> 02:09.270
So this is something that we have already seen that once the module is built.

02:09.270 --> 02:12.480
So in the step three, your module will be built.

02:12.480 --> 02:15.000
So let's say your GZip module is built.

02:15.000 --> 02:20.150
So now you reference it within your Nginx configuration with the load module directive.

02:20.160 --> 02:22.620
So these are the four steps which are required.

02:22.620 --> 02:29.370
So let's go ahead and start from scratch and look into the steps which are involved in a practical manner.

02:29.820 --> 02:40.530
So currently if I do a nginx hyphen V we are running the Nginx 1.16 version and we will be using a sample

02:40.530 --> 02:44.250
Nginx hello world module for our demo purposes.

02:44.250 --> 02:48.030
So in order for us to build this module.

02:48.030 --> 02:53.700
So this is currently you see, this is a C file, this is not a module, so we have to compile it.

02:53.700 --> 02:59.160
So in order for us to compile it, we need to fetch the same nginx source.

02:59.160 --> 03:06.270
So since we have Nginx version 1.16 installed within the Nginx download page, I'll fetch the Nginx

03:06.270 --> 03:07.530
1.16 source.

03:07.530 --> 03:17.190
So let's call it as nginx hyphen demo and let's go inside the directory and we'll fetch the latest source.

03:18.000 --> 03:22.890
Once we have the source, let's go ahead and uncompress it.

03:25.530 --> 03:25.950
Great.

03:26.040 --> 03:28.650
So now we have the nginx source here.

03:28.680 --> 03:31.440
Now our step one is completed.

03:31.440 --> 03:35.370
So the step two is to fetch the module source.

03:35.370 --> 03:41.820
So in our case, since we will be using the Hello world module, I'll clone the path here.

03:41.820 --> 03:47.730
And before you do that, make sure you have the git installed with yum install git command.

03:48.330 --> 03:51.180
So in my case git package is already installed.

03:51.180 --> 03:56.640
So now I'll do a git clone and I'll paste the link here.

03:57.930 --> 03:58.500
Great.

03:58.500 --> 04:04.440
So now if I do ls, you will see there is a directory and this directory basically has the module.

04:04.440 --> 04:12.030
So let's open this up and if you do ls hyphen L There are basically two important directories here.

04:12.060 --> 04:17.130
One is the config file and second is the c file.

04:17.130 --> 04:20.160
So c file is where the module code is written.

04:20.160 --> 04:22.440
So this is what we'll be compiling.

04:22.440 --> 04:24.990
And if you look into the config file.

04:24.990 --> 04:31.170
So this basically contains a lot of configuration, including the module name as well as the module

04:31.170 --> 04:31.620
path.

04:31.620 --> 04:37.260
So anyways, we are not really focusing on the module development right now, so let's keep it simple.

04:37.260 --> 04:40.980
So just make sure that generally modules that you have.

04:41.010 --> 04:42.450
So this is the simplest module.

04:42.450 --> 04:48.120
So that's why you just have a single config file which basically contains the name, the path of the

04:48.120 --> 04:50.460
file, and this is the actual C file.

04:50.760 --> 04:53.310
So if you want to open this up.

04:53.310 --> 04:59.160
So this is the actual C file that will be compiled to a module.

04:59.460 --> 04:59.940
All right.

05:00.120 --> 05:01.890
So that's about it.

05:01.920 --> 05:06.480
Let's go a bit up and let's go inside the Nginx directory.

05:07.530 --> 05:11.790
And as expected, we have the configure option.

05:11.790 --> 05:14.340
So let's run the configure option here.

05:14.340 --> 05:20.310
And if you look into the installation steps, you have two installation.

05:20.310 --> 05:24.620
Step one is for the static module, second is for the dynamic module.

05:24.630 --> 05:29.430
So dynamic module is something that we are planning to use, so I'll just copy.

05:29.460 --> 05:30.240
Hyphen hyphen.

05:30.240 --> 05:32.460
Add hyphen dynamic hyphen module.

05:33.120 --> 05:39.270
So this will remain the same and you will have to basically specify the path.

05:40.110 --> 05:45.660
All right, so this is the path of my module here, so let's go ahead and execute it.

05:47.070 --> 05:47.670
Great.

05:47.670 --> 05:51.300
And the next command that you need to use is make modules.

05:53.160 --> 05:53.580
Great.

05:53.580 --> 06:01.060
So once this is done, you should be able to see the modules dot so file within your objects directory.

06:01.060 --> 06:06.390
So let me open this up and this is the built module over here.

06:06.400 --> 06:08.710
So this is what we need.

06:08.710 --> 06:10.960
So now let's copy this up.

06:10.960 --> 06:19.330
Let's copy this file to Etsy Nginx directory and let's go to the Etsy Nginx directory here.

06:19.780 --> 06:20.950
So this is the file.

06:20.950 --> 06:21.940
So let's do one thing.

06:21.940 --> 06:28.180
Let's create a new directory called as modules and we'll move the module to the modules directory.

06:30.160 --> 06:30.850
Great.

06:30.850 --> 06:34.700
So now if you look into the step, we have step two completed.

06:34.720 --> 06:41.160
We have step three completed and the next thing that is required is the step four.

06:41.170 --> 06:48.130
So in the step four, within the Nginx configuration, we have to specify the load module directive

06:48.130 --> 06:52.600
and we have to specify the path where the module will be stored.

06:52.600 --> 06:57.490
So in our case the Nginx configuration file is nginx.conf.

06:57.520 --> 07:04.870
Now remember that the load module directive that you might be using, it has to be out of the Http section,

07:04.870 --> 07:07.000
so it needs to be at the global section.

07:07.000 --> 07:12.970
You cannot put it inside the Http section here, so just specify the path.

07:12.970 --> 07:19.600
I'll say C nginx modules and let's find the exact name.

07:19.840 --> 07:22.810
I'll say modules then here Nginx.

07:22.810 --> 07:25.240
So this is the exact name.

07:25.240 --> 07:30.400
So let's copy this up so that we do not have a typo here.

07:30.400 --> 07:32.080
All right, I'll paste it here.

07:32.230 --> 07:39.580
So let's save this and if you want to do nginx hyphen t, All right, so it says load module is not

07:39.580 --> 07:40.480
terminated.

07:41.140 --> 07:46.690
Let's quickly verify and yes, great.

07:46.690 --> 07:52.630
So just verify with nginx hyphen t whether the test is successful.

07:52.630 --> 07:58.510
Now generally, if there are any issues within the module, your test will not go successful.

07:58.510 --> 08:00.160
This is important part to remember.

08:00.330 --> 08:04.240
You can get various errors like binary incompatible errors specifically.

08:04.240 --> 08:10.780
That happens when you build a module with a different nginx version than what is been running within

08:10.780 --> 08:11.740
your environment.

08:11.740 --> 08:13.750
So this is an important part.

08:13.750 --> 08:18.190
So once this is successful, that means everything is perfect.

08:18.220 --> 08:21.640
Now the last thing is to verify whether everything is working.

08:21.640 --> 08:28.570
So they have a simple example for a location and here they are basically specifying the Hello World

08:28.570 --> 08:29.170
module.

08:29.170 --> 08:30.830
So let's try this out.

08:31.610 --> 08:38.030
So I'll open up the nginx conf and let's create a new server directory.

08:38.300 --> 08:40.340
So I'll call it as server.

08:40.460 --> 08:43.040
Let's listen on port 8080.

08:46.080 --> 08:55.770
And for the location of route, we'll say Hello world and we'll close our server block.

08:58.200 --> 09:03.390
All right, let's quickly verify the test is successful.

09:03.510 --> 09:07.230
And let's go ahead and restart our Nginx.

09:08.730 --> 09:09.300
Great.

09:09.330 --> 09:14.580
Now let's do a curl on localhost colon 8080 and you should see Hello world.

09:14.850 --> 09:17.220
If everything is working fine.

09:17.280 --> 09:23.310
So this Hello world basically is coming from the module that we had referenced.

09:23.880 --> 09:24.210
All right.

09:24.210 --> 09:30.180
So basically, if you look into this module page, it basically says that this should return Hello world

09:30.180 --> 09:31.780
in the response body.

09:31.800 --> 09:36.180
So that's the high level overview about building dynamic modules.

09:36.210 --> 09:43.260
Now, again, if you do not want your module, you can just remove the configuration file from your

09:43.260 --> 09:45.300
Nginx and that's about it.

09:45.300 --> 09:48.520
Your module will no longer be loaded.

09:48.550 --> 09:50.010
So that's about it.

09:50.020 --> 09:54.880
I hope this video has been informative for you and I look forward to see you in the next video.
