Hey, everyone.
And welcome back.
In today's video, we will be discussing about building dynamic modules.
Now, generally modules that you might be referencing within the engine is typically the third party
modules.
So these modules needs to be compiled before you reference them within your production engine X.
Now one very important part to remember that whenever you are compiling a module, you need to make
sure that it is compiled against the same engine X version which is running in your production.
So let's say that you have a third party module for web application firewall, so you will have to compile
it.
Now you will compile it against the same version of your production.
So if your production engine X is running, let's say 1.16, then you compile that module against 1.16
version only.
If you compile it with a different version, then most probably when you reference that within your
production engine x, the module will not really work perfectly.
All right.
So that is one important part to remember.
Now, there are also certain repositories where you can get out of the box modules which does not need
compilation.
So that is also something that you can use.
But ideally it is recommended that if you are running Nginx in production, just compile the modules
instead of referencing to some third party repository.
So in order to build dynamic modules, there are four steps which are required.
The first step is fetch the Nginx source.
So let's say your production engineering's version is 1.16, you fetch the 1.16 source.
All right, Second is fetch the module source.
So many of the modules are available in GitHub, so you fetch the source of them.
Third step is to build the dynamic module.
And once the module is built, you can reference that module within the Nginx configuration.
So this is something that we have already seen that once the module is built.
So in the step three, your module will be built.
So let's say your GZip module is built.
So now you reference it within your Nginx configuration with the load module directive.
So these are the four steps which are required.
So let's go ahead and start from scratch and look into the steps which are involved in a practical manner.
So currently if I do a nginx hyphen V we are running the Nginx 1.16 version and we will be using a sample
Nginx hello world module for our demo purposes.
So in order for us to build this module.
So this is currently you see, this is a C file, this is not a module, so we have to compile it.
So in order for us to compile it, we need to fetch the same nginx source.
So since we have Nginx version 1.16 installed within the Nginx download page, I'll fetch the Nginx
1.16 source.
So let's call it as nginx hyphen demo and let's go inside the directory and we'll fetch the latest source.
Once we have the source, let's go ahead and uncompress it.
Great.
So now we have the nginx source here.
Now our step one is completed.
So the step two is to fetch the module source.
So in our case, since we will be using the Hello world module, I'll clone the path here.
And before you do that, make sure you have the git installed with yum install git command.
So in my case git package is already installed.
So now I'll do a git clone and I'll paste the link here.
Great.
So now if I do ls, you will see there is a directory and this directory basically has the module.
So let's open this up and if you do ls hyphen L There are basically two important directories here.
One is the config file and second is the c file.
So c file is where the module code is written.
So this is what we'll be compiling.
And if you look into the config file.
So this basically contains a lot of configuration, including the module name as well as the module
path.
So anyways, we are not really focusing on the module development right now, so let's keep it simple.
So just make sure that generally modules that you have.
So this is the simplest module.
So that's why you just have a single config file which basically contains the name, the path of the
file, and this is the actual C file.
So if you want to open this up.
So this is the actual C file that will be compiled to a module.
All right.
So that's about it.
Let's go a bit up and let's go inside the Nginx directory.
And as expected, we have the configure option.
So let's run the configure option here.
And if you look into the installation steps, you have two installation.
Step one is for the static module, second is for the dynamic module.
So dynamic module is something that we are planning to use, so I'll just copy.
Hyphen hyphen.
Add hyphen dynamic hyphen module.
So this will remain the same and you will have to basically specify the path.
All right, so this is the path of my module here, so let's go ahead and execute it.
Great.
And the next command that you need to use is make modules.
Great.
So once this is done, you should be able to see the modules dot so file within your objects directory.
So let me open this up and this is the built module over here.
So this is what we need.
So now let's copy this up.
Let's copy this file to Etsy Nginx directory and let's go to the Etsy Nginx directory here.
So this is the file.
So let's do one thing.
Let's create a new directory called as modules and we'll move the module to the modules directory.
Great.
So now if you look into the step, we have step two completed.
We have step three completed and the next thing that is required is the step four.
So in the step four, within the Nginx configuration, we have to specify the load module directive
and we have to specify the path where the module will be stored.
So in our case the Nginx configuration file is nginx.conf.
Now remember that the load module directive that you might be using, it has to be out of the Http section,
so it needs to be at the global section.
You cannot put it inside the Http section here, so just specify the path.
I'll say C nginx modules and let's find the exact name.
I'll say modules then here Nginx.
So this is the exact name.
So let's copy this up so that we do not have a typo here.
All right, I'll paste it here.
So let's save this and if you want to do nginx hyphen t, All right, so it says load module is not
terminated.
Let's quickly verify and yes, great.
So just verify with nginx hyphen t whether the test is successful.
Now generally, if there are any issues within the module, your test will not go successful.
This is important part to remember.
You can get various errors like binary incompatible errors specifically.
That happens when you build a module with a different nginx version than what is been running within
your environment.
So this is an important part.
So once this is successful, that means everything is perfect.
Now the last thing is to verify whether everything is working.
So they have a simple example for a location and here they are basically specifying the Hello World
module.
So let's try this out.
So I'll open up the nginx conf and let's create a new server directory.
So I'll call it as server.
Let's listen on port 8080.
And for the location of route, we'll say Hello world and we'll close our server block.
All right, let's quickly verify the test is successful.
And let's go ahead and restart our Nginx.
Great.
Now let's do a curl on localhost colon 8080 and you should see Hello world.
If everything is working fine.
So this Hello world basically is coming from the module that we had referenced.
All right.
So basically, if you look into this module page, it basically says that this should return Hello world
in the response body.
So that's the high level overview about building dynamic modules.
Now, again, if you do not want your module, you can just remove the configuration file from your
Nginx and that's about it.
Your module will no longer be loaded.
So that's about it.
I hope this video has been informative for you and I look forward to see you in the next video.