Nginx Dynamic Modules

In version 1.9.11 , nginx introduced a new feature “Dynamic modules”.  With dynamic modules, you can optionally load separate shared object files at runtime as modules – both third-party modules and some native NGINX modules.

In earlier versions of nginx, modules are not dynamically loadable, so they must be selected and compiled into the core software.  The module was loaded with NGINX every time, whether you need it or not. For many users, this made Nginx much less flexible, especially for users who are not comfortable maintaining their own compiled software outside of their distribution’s conventional packaging system. While distributions’ packages tend to include the most commonly used modules, if you require a non-standard module , you will have to build the server from source yourself .

In the first release of nginx dynamic module, we still need to recompile nginx also along with the needed modules , but we can decide if we want to enable it on runtime. This is a good start , now  users can compile nginx with all required modules and enable it on runtime whenever its needed. But in future , Nginx.org  will  add the ability to compile modules after the NGINX binary has been compiled. This will make nginx modules portable . In near future we can expect OS packages for each nginx module , which can be loaded on runtime. I am sure this will enhance the growth and  popularity of nginx.

How to compile nginx with Dynamic modules?

Let us see how to build and install nginx  with dynamic modules enabled, on a Centos machine. We will download nginx source from nginx.org and build it with geoip module loaded dynamically

First we will install geoip library on the machine

1 2 3 4 5 6 7   yum install geoip geoipdevel wget http://nginx.org/download/nginx-1.11.8.tar.gz tar xzf nginx1.11.8.tar.gz cd nginx1.11.8 ./configure withhttp_geoip_module=dynamic  

If configure script is successful, you will see following on the screen

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20   Configuration summary + using system PCRE library + OpenSSL library is not used + using system zlib library   nginx path prefix: “/usr/local/nginx” nginx binary file: “/usr/local/nginx/sbin/nginx” nginx modules path: “/usr/local/nginx/modules” nginx configuration prefix: “/usr/local/nginx/conf” nginx configuration file: “/usr/local/nginx/conf/nginx.conf” nginx pid file: “/usr/local/nginx/logs/nginx.pid” nginx error log file: “/usr/local/nginx/logs/error.log” nginx http access log file: “/usr/local/nginx/logs/access.log” nginx http client request body temporary files: “client_body_temp” nginx http proxy temporary files: “proxy_temp” nginx http fastcgi temporary files: “fastcgi_temp” nginx http uwsgi temporary files: “uwsgi_temp” nginx http scgi temporary files: “scgi_temp”  

You can see that modules will be stored under the folder ”

1 /usr/local/nginx/modules

Now build and install nginx using following commands

1 2 3 4 make make install  

If everything goes well you should see nginx installed at

1 2 3 /usr/local/nginx/sbin/nginx  

And geoip module at

1 2 3 /usr/local/nginx/modules/ngx_http_geoip_module.so  

Now we can load this module on Runtime , by adding following line in the nginx.conf file

1 2 3 load_module “/usr/local/nginx/modules/ngx_http_geoip_module.so”;  

Make sure you add this line outside “http” block and reload nginx once done.

So we have loaded this module dynamically, and we can disable this module at anytime by commenting that line and reloading nginx.

The Future of Dynamic Modules

As of now this feature is not of much use as we need to recompile nginx to
create the dynamic modules. But in future , Nginx.org  will  add the ability to compile modules after the NGINX binary has been compiled. This will make nginx modules portable . In near future we can expect OS packages for each nginx module , which can be loaded on runtime. Am sure this will enhance the increasing  popularity of nginx.

 
Author: , 0000-00-00