In this tutorial, I will explain how to compile an external module for Nginx in order to add additional functionality to the web server.
To illustrate this tutorial, I will take as an example the module http-headers-more-filter which allows you to modify the headers (hearders) sent by HTTP requests.
Before you begin, make sure you have the compiler installed:
sudo apt install gcc
The first step is to check if the module is present, enter the command below to check.
nginx -V 2>&1 | grep --color 'http-headers-more-filter'
If the command returns nothing, the module is not present.
To compile the module, we will need the Nginx sources, retrieve the installed version using the command:
nginx-V

As we can see in the screenshot below, Nginx 1.21.0 is installed.
Download the sources of the installed Nginx version, in the example I will use the following command:
wget https://nginx.org/download/nginx-1.21.0.tar.gz

Unpack the archive using the tar command:
tar -xzvf nginx-1.21.0.tar.gz
Using the ll command, we can see that the archive is uncompressed.

You must now download the module sources. the sources of the hearders-more module are available here : openresty/headers-more-nginx-module: Set, add, and clear arbitrary output headers in NGINX http servers (github.com).
We clone the repository using the git command:
git clone https://github.com/openresty/headers-more-nginx-module.git
Then list to verify that the repository is cloned.

Depending on the modules you want, dependencies may be necessary, here you need to install the following packages:
- libpcre3-dev
- zlib1g
sudo apt install libpcre3-dev zlib1g
Go to the folder that contains the Nginx sources:
cd nginx-1.21.0
Now we will configure the compilation of the module using the command below:
./configure --add-dynamic-module=../headers-more-nginx-module


Adapt the path of the –add-dynamic-module= parameter you want to compile.
Once the compilation is configured, compile it with the command:
make modules

The module is compiled, the module’s .so file is located in the objs folder of the Nginx source folder.

You must now move the .so file to the /etc/nginx/modules folder. Once the file has been moved, you must tell Nginx to load the module.
Open the nginx.conf file and add the load_module instruction.
load_module modules/ngx_http_header_more_filter_modules.so
Then restart Nginx
sudo systemctl restart nginx
You now know how to compile an external module for Nginx.