Documentation: Build & Install Nginx Dynamic Module (headers-more-nginx-module)
This document covers building and installing the headers-more-nginx-module for an existing Nginx installation. The module allows hiding/removing Nginx headers (e.g., Server) for better security and compliance.
Note : This guide is tailored for Amazon Linux 2023 (amz2023) but can be adapted for other distributions with minor changes.
Step 1: Install Development Tools
sudo yum groupinstall "Development Tools" -y
sudo yum install pcre pcre-devel zlib zlib-devel openssl openssl-devel wget gcc make -y- Installs all the required compilers, libraries, and tools to build Nginx modules from source.
- Dependencies include
PCRE,zlib, andOpenSSL.
Step 2: Get Current Nginx Version
nginx -v
NGINX_VERSION=$(nginx -v 2>&1 | awk -F/ '{print $2}')
echo "Detected Nginx version: $NGINX_VERSION"- Detects the installed Nginx version.
- Stores it in a variable (
NGINX_VERSION) for downloading the exact source.
Step 3: Download Nginx Source Code
cd /usr/local/src
wget http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz
tar -xvzf nginx-$NGINX_VERSION.tar.gz
cd nginx-$NGINX_VERSION- Downloads the exact source code of your Nginx version.
- Extracts it and navigates inside the source directory.
⚠️ Important: To build dynamic modules, the source version must match the installed Nginx version.
Step 4: Download headers-more-nginx-module
git clone https://github.com/openresty/headers-more-nginx-module.git- Clones the
headers-more-nginx-modulefrom OpenResty’s official repo. - This module allows clearing or modifying response headers.
Step 5: Configure Nginx with the Module
./configure --with-compat --add-dynamic-module=./headers-more-nginx-module- Prepares the Nginx build system with dynamic module support.
--with-compatensures compatibility with your installed Nginx.--add-dynamic-moduleincludes theheaders-moremodule in the build process.
Step 6: Build the Module
make modules
ls -la objs/ngx_http_headers_more_filter_module.so- Compiles only the module (not the full Nginx binary).
- The compiled
.somodule will appear insideobjs/.
Step 7: Install the Module
sudo mv objs/ngx_http_headers_more_filter_module.so /usr/share/nginx/modules/- Moves the built module into Nginx’s modules directory (
/usr/share/nginx/modules/). - This is the location from which Nginx loads dynamic modules.
Step 8: Configure Nginx to Load the Module
nano /etc/nginx/nginx.confInside the nginx.conf, add:
load_module modules/ngx_http_headers_more_filter_module.so;
http {
server_tokens off; # Hides Nginx version info
more_clear_headers Server; # Removes the 'Server' header entirely
}load_moduletells Nginx to use the newly built module.server_tokens offhides version info in error pages.more_clear_headers Serverstrips theServerheader for security hardening.
Step 9: Validate & Restart Nginx
nginx -t
systemctl restart nginx- Tests the new configuration for syntax errors.
- Restarts Nginx to apply changes.
Key Notes
- Always build the module against the same version of Nginx installed.
- If Nginx is upgraded, you must recompile this module.
- The module works only in dynamic mode (
.so), so Nginx itself does not need to be rebuilt.