Table of contents
1.
Introduction
2.
Dynamic Modules
3.
Dynamic Modules Repository
4.
Important Dynamic Modules
5.
Example for Dynamic Modules
5.1.
Obtain the NGINX Open Source Release
5.2.
Obtain the Module Sources
5.3.
Compile the Dynamic Module
5.4.
Load and Use the Module
6.
Uninstalling a dynamic module
7.
FAQs
8.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

NGINX Dynamic Modules

Author Toohina Barua
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Dynamic modules include functionality to Nginx. Nginx is a web server that can also be used as a reverse proxy, load balancer, mail proxy, and HTTP cache. Nginx is free and open-source software. To make a complex website, we need functionalities that are complex as much. Dynamic Modules are loaded dynamically into NGINX Plus at runtime to add specialized functionality, including features authored by third parties. 
In this article, we will learn about dynamic modules in NGINX. So let us dive in. 

Also see, Must Do Coding Questions

Dynamic Modules

A modular design is used by NGINX Plus. Software modules that can be put into a running NGINX Plus system on demand can be used to provide new features and functionality. NGINX Plus dynamic modules offer features like geolocation by IP address, picture scaling, and the ability to integrate NGINX JavaScript njs or Lua scripts into the event processing paradigm. NGINX and third-party developers collaborate to create modules.

Also Read -  Ibegin TCS

Dynamic Modules Repository

Standard package management tools like apt and yum can be used to access and download modules from the NGINX Plus dynamic modules repository. Run the following command to install njs dynamic modules for Debian and Ubuntu:

  $ apt-get install nginx-plus-module-njs


The load_module directive is then added to each dynamic module's NGINX Plus configuration file. For example, in the top-level ("main") context of the main NGINX Plus configuration file (nginx.conf), specify the load_module directives to enable njs dynamic modules:

  load_module modules/ngx_http_js_module.so;
  load_module modules/ngx_stream_js_module.so;

Recommended Topic, 8085 Microprocessor Pin Diagram

Important Dynamic Modules

The following NGINX modules and module bundles can be dynamically created in the first release:

  • GeoIP (ngx_http_geoip_module): Using the MaxMind GeoIP databases and the GeoIP dynamic module offered by NGINX, Inc., capture information from the client IP address in variables.
  • Image-Filter (ngx_http_image_filter_module): With the Image-Filter dynamic module from NGINX, Inc., you may crop, resize, rotate, and perform other changes on GIF, JPEG, and PNG pictures.
  • Mail (includes all Mail modules: ngx_mail_{core, auth, imap, pop3, proxy, smtp}_module) 
  • Stream (includes all Stream modules: ngx_stream_{core, access, limit_conn, proxy, ssl, upstream}_module)
  • XSLT (ngx_http_xslt_module): With the XSLT dynamic module, provided by NGINX, Inc., modify XML code in response bodies using one or more XSLT stylesheets.

Example for Dynamic Modules

This example shows how to update the source for a module and load it into NGINX Plus using a simple Hello World module. The "Hello World" module includes a simple directive (hello_world) that sends a simple message in response to queries.

Obtain the NGINX Open Source Release

Determine which version of NGINX Open Source corresponds to your NGINX Plus installation. It's NGINX 1.11.5 in this case.

$ nginx -v
nginx version: nginx/1.11.5 (nginx-plus-r11)


Visit nginx.org/download to get the matching NGINX Open Source package:

$ wget https://nginx.org/download/nginx-1.11.5.tar.gz
$ tar -xzvf nginx-1.11.5.tar.gz


Obtain the Module Sources

Get the 'Hello World' NGINX module's source code from GitHub:

$ git clone https://github.com/perusio/nginx-hello-world-module.git


The format of a dynamic module's config shell file differs from that of static modules included into an NGINX Open Source binary. Replace the following in the nginx-hello-world-module/config file:

ngx_addon_name=ngx_http_hello_world_module

if test -n "$ngx_module_link"; then
    ngx_module_type=HTTP
    ngx_module_name=ngx_http_hello_world_module
    ngx_module_srcs="$ngx_addon_dir/ngx_http_hello_world_module.c"

    . auto/module
else
    HTTP_MODULES="$HTTP_MODULES ngx_http_hello_world_module"
    NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_hello_world_module.c"
fi


Compile the Dynamic Module

Compile the module by running the configure script with the --with-compat argument, which produces a standard build environment that NGINX Open Source and NGINX Plus both support. Then, to build the module, run make modules:

$ cd nginx-1.11.5/
$ ./configure --with-compat --add-dynamic-module=../nginx-hello-world-module
$ make modules


In /etc/nginx/modules, copy the module library (.so file):

$ sudo cp objs/ngx_http_hello_world_module.so /etc/nginx/modules/


Load and Use the Module

Add the load_module directive to the top-level (main) context of your nginx.conf configuration file (not the http or stream context) to load the module into NGINX Plus:

load_module modules/ngx_http_hello_world_module.so;


Add a location block in the http context using the Hello World module's hello_world directive. Hello world is the response to requests to the location.

server {
    listen 80;

    location / {
         hello_world;
    }
}


Reload your NGINX Plus configuration and test it with a simple request:

$ nginx -s reload
$ curl http://localhost/
hello world


You can also read about mock interview.

Uninstalling a dynamic module

The following steps instructs how to uninstall a dynamic module:

In the http context, add a location block with the hello_world directive given by the Hello World module. Requests to the location return the response hello world.

$ yum remove <dynamic_module_name>


For Debian and Ubuntu:

$ apt-get remove <dynamic_module_name>


For SLES:

$ zypper remove <dynamic_module_name>


In the NGINX Plus configuration file, remove the load module directive. Then Remove any directives relating to the dynamic module from the NGINX Plus configuration file. Finally, reload NGINX Plus:

$ nginx -t && nginx -s reload

FAQs

  1. What is NGINX?
    Nginx, pronounced "engine-ex," is an open-source web server that is now being used as a reverse proxy, HTTP cache, and load balancer, thanks to its original popularity as a web server.
     
  2. What is a web server?
    A web server is computer software and hardware that handles HTTP and HTTPS requests. A user agent, such as a web browser or a web browser, initiates communication by sending an HTTP request for a web page or other resource. The server responds with the resource's content or an error message.
     
  3. What is a reverse proxy?
    A reverse proxy is an application that sits in front of back-end applications on a computer network and forwards client (e.g., browser) requests to them. Scalability, performance, resilience, and security are aided by reverse proxies. The resources returned to the client appear to have come from the reverse proxy.
     
  4. What is an example scenario for reverse proxy?
    Example scenario: A reverse proxy server receives a request from a client on the internet. The proxy examines the request and finds that it is valid and that the requested resource is not in its cache. The request is subsequently forwarded to an internal web server. The requested resource is returned by the internal server to the proxy, which then returns it to the client.
     
  5. What is a load balancer?
    The technique of distributing a set of jobs over a set of resources (computing units) with the goal of making their overall processing more efficient is known as load balancing in computing. 

Key Takeaways

From this article, we learned what is NGINX and its dynamic modules, an example for it and how to uninstall it. 
But this is not enough; you need something extra to excel in web development truly. If you want to learn more about web development, you can read our articles or take our highly curated Web Development course.

Live masterclass