From ffc5c20fee204be50101e813f91f44f5d043753d Mon Sep 17 00:00:00 2001
From: Alexey Berezhok
Date: Sun, 21 Jun 2026 01:15:00 +0300
Subject: [PATCH] Added ability build for custom nginx version for distributive
build
---
BUILD.md | 8 ++++
BUILD.ru.md | 8 ++++
package_preparer.sh | 71 +++++++++++++++++++++++++++++
packages/deb/control.sh | 33 ++++++++++++++
packages/deb/debian/control | 14 ++++++
packages/deb/rules | 9 +++-
packages/rpm/nginx-mod-rewrite.spec | 9 ++--
7 files changed, 147 insertions(+), 5 deletions(-)
create mode 100644 packages/deb/control.sh
create mode 100644 packages/deb/debian/control
diff --git a/BUILD.md b/BUILD.md
index fd09676..b465488 100644
--- a/BUILD.md
+++ b/BUILD.md
@@ -29,6 +29,14 @@ To build the package, run the command:
bash package_preparer.sh prepare "almalinux:9"
```
+or if a specific nginx version is required for the distribution, use this command:
+
+```
+bash package_preparer.sh prepare "almalinux:9" "1.30.2"
+```
+
+in this case the module will be built for nginx=1.30.2.
+
The build was tested on the following images:
* almalinux:9
diff --git a/BUILD.ru.md b/BUILD.ru.md
index d3f983a..de9b677 100644
--- a/BUILD.ru.md
+++ b/BUILD.ru.md
@@ -29,6 +29,14 @@ docker run --name nginx-mod_rewrite -p 8080:80 -p 8081:8081 nginx-mod_rewrite
bash package_preparer.sh prepare "almalinux:9"
```
+или если для дистрибутива нужна конкретная версия nginx, то такая команда:
+
+```
+bash package_preparer.sh prepare "almalinux:9" "1.30.2"
+```
+
+в этом случае будет собран модуль, для nginx=1.30.2.
+
Сборка проверялась на образах:
* almalinux:9
diff --git a/package_preparer.sh b/package_preparer.sh
index 2db16ab..4b13fd9 100644
--- a/package_preparer.sh
+++ b/package_preparer.sh
@@ -47,6 +47,7 @@ function download_nginx() {
exit 1
fi
fi
+ echo "Built for nginx ${NGINX_VER}" > BUILDINFO.txt
}
COMMAND="$1"
@@ -56,6 +57,7 @@ case "$COMMAND" in
prepare)
DOCKER_IMAGE="$2"
+ NGINX_CUSTOM_VER="$3"
if [ -z "$DOCKER_IMAGE" ]; then
echo "Set docker image for build"
@@ -153,6 +155,9 @@ EOF
# Copy Dockerfile into tmpbuild and replace placeholder image name
cp packages/Dockerfile tmpbuild/Dockerfile
sed -i "s|^FROM image|FROM ${DOCKER_IMAGE}|g" tmpbuild/Dockerfile
+ if [ -n "$NGINX_CUSTOM_VER" ]; then
+ sed -i "s|^ENTRYPOINT \[ \"bash\", \"package_preparer.sh\", \"packageprep\" \]|ENTRYPOINT [ \"bash\", \"package_preparer.sh\", \"packageprep\", \"$NGINX_CUSTOM_VER\" ]|g" tmpbuild/Dockerfile
+ fi
# Build a temporary Docker image based on the provided base image
docker build -t tmpbuild_image -f tmpbuild/Dockerfile .
@@ -169,6 +174,8 @@ download)
;;
packageprep)
PKG_MGR=""
+ NGINX_CUSTOM_VER="$2"
+
# Determine package manager and install nginx
if command -v dnf >/dev/null 2>&1; then
PKG_MGR="dnf"
@@ -185,9 +192,63 @@ packageprep)
exit 1
fi
+ # Auto-detect nginx version if not specified by user
+ DETECTED_NGINX_VERSION=""
+ if [ -z "$NGINX_CUSTOM_VER" ] || [ "$NGINX_CUSTOM_VER" == "." ]; then
+ echo "Auto-detecting nginx version..."
+
+ if command -v rpm >/dev/null 2>&1; then
+ # RPM-based systems: detect via package manager and runtime
+ PKG_VERSION=$(rpm -q nginx --queryformat '%{VERSION}' 2>/dev/null)
+ RUNTIME_VERSION=$(nginx -V 2>&1 | grep 'version:' | head -n 1 | sed -n 's/.*nginx\/\(.*\).*/\1/p')
+
+ if [ -n "$RUNTIME_VERSION" ]; then
+ DETECTED_NGINX_VERSION="$RUNTIME_VERSION"
+ elif [ -n "$PKG_VERSION" ]; then
+ # Fallback to package version (strip release part)
+ DETECTED_NGINX_VERSION=$(echo "$PKG_VERSION" | cut -d'-' -f1)
+ fi
+ elif command -v dpkg >/dev/null 2>&1; then
+ # Debian-based systems
+ PKG_VERSION=$(dpkg-query -W -f='${Version}\n' nginx 2>/dev/null | head -n 1)
+ RUNTIME_VERSION=$(nginx -V 2>&1 | grep 'version:' | head -n 1 | sed -n 's/.*nginx\/\(.*\).*/\1/p')
+
+ if [ -n "$RUNTIME_VERSION" ]; then
+ DETECTED_NGINX_VERSION="$RUNTIME_VERSION"
+ elif [ -n "$PKG_VERSION" ]; then
+ # Handle Debian version format (e.g., "1.24.0-1ubuntu1.1" -> "1.24.0")
+ DETECTED_NGINX_VERSION=$(echo "$PKG_VERSION" | cut -d'-' -f1)
+ fi
+ else
+ echo "Error: Unsupported package manager. Unable to auto-detect nginx version."
+ exit 1
+ fi
+
+ if [ -n "$DETECTED_NGINX_VERSION" ]; then
+ echo "Detected nginx version: $DETECTED_NGINX_VERSION"
+ else
+ echo "Warning: Could not detect nginx version. Using generic download."
+ fi
+ else
+ echo "Using custom nginx version: $NGINX_CUSTOM_VER"
+ fi
+
if [ "$PKG_MGR" == "yum" -o "$PKG_MGR" == "dnf" ]; then
mkdir -p rpmbuild/BUILD rpmbuild/BUILDROOT rpmbuild/RPMS rpmbuild/SOURCES rpmbuild/SPECS rpmbuild/SRPMS
cp packages/rpm/nginx-mod-rewrite.spec rpmbuild/SPECS
+
+ # Determine nginx version to use in spec file
+ if [ -n "$NGINX_CUSTOM_VER" ]; then
+ NGINX_VER_FOR_SPEC="$NGINX_CUSTOM_VER"
+ elif [ -n "$DETECTED_NGINX_VERSION" ]; then
+ NGINX_VER_FOR_SPEC="$DETECTED_NGINX_VERSION"
+ fi
+
+ # Modify spec file to use detected/custom nginx version
+ if [ -n "$NGINX_VER_FOR_SPEC" ]; then
+ sed -i "s|%global nginx_version .*|%global nginx_version $NGINX_VER_FOR_SPEC|" rpmbuild/SPECS/nginx-mod-rewrite.spec
+ fi
+
cp nginx-mod-rewrite-*.tar.gz rpmbuild/SOURCES
rpmbuild --define='_topdir /app/rpmbuild' -ba rpmbuild/SPECS/nginx-mod-rewrite.spec
cp rpmbuild/RPMS/x86_64/* /app/tmpbuild
@@ -213,6 +274,16 @@ packageprep)
# Extract, stripping the top-level directory
tar --strip-components=1 -xzf "$(basename "$TARBALL")"
+ # Use custom nginx version if specified, otherwise use auto-detected
+ NGINX_VER="${NGINX_CUSTOM_VER:-${DETECTED_NGINX_VERSION:-1.24.0}}"
+
+ # Generate debian/control with correct nginx version
+ bash control.sh "$NGINX_VER"
+
+ if [ -n "$NGINX_CUSTOM_VER" ]; then
+ sed -i "s|bash package_preparer.sh download . system|bash package_preparer.sh download \"$NGINX_CUSTOM_VER\"|g" rules
+ fi
+
dpkg-buildpackage -us -uc
cp ../nginx-mod-rewrite* /app/tmpbuild
diff --git a/packages/deb/control.sh b/packages/deb/control.sh
new file mode 100644
index 0000000..c22e1bc
--- /dev/null
+++ b/packages/deb/control.sh
@@ -0,0 +1,33 @@
+#!/bin/bash
+
+# This script generates the debian/control file with correct nginx version dependency
+# Usage: control.sh [NGINX_VERSION]
+# If NGINX_VERSION is not specified, it uses DETECTED_NGINX_VERSION from environment or default "1.24.0"
+
+NGINX_VER="${1:-${DETECTED_NGINX_VERSION:-1.24.0}}"
+
+cat > debian/control << EOF
+Source: nginx-mod-rewrite
+Section: web
+Priority: optional
+Maintainer: Alexey Berezhok
+Build-Depends: debhelper-compat (=13),
+ dh-autoreconf,
+ libssl-dev,
+ libpcre2-dev,
+ zlib1g-dev,
+ nginx (=${NGINX_VER})
+Standards-Version: 4.7.0
+Homepage: https://docs.brepo.ru/nginx-mod_rewrite
+
+Package: nginx-mod-rewrite
+Architecture: any
+Depends: \${shlibs:Depends}, nginx (=${NGINX_VER})
+Provides: nginx-http-module, nginx-apache-rewrite
+Description: Nginx rewrite module – dynamic module adding mod_rewrite functionality
+ Dynamic Nginx module implementing Apache mod_rewrite functionality.
+ The package is built from Nginx sources and the module's own files, and then
+ installs only the compiled shared object into /etc/nginx/modules directory.
+EOF
+
+echo "Generated debian/control for nginx version: $NGINX_VER"
diff --git a/packages/deb/debian/control b/packages/deb/debian/control
new file mode 100644
index 0000000..86e618a
--- /dev/null
+++ b/packages/deb/debian/control
@@ -0,0 +1,14 @@
+Source: nginx-mod-rewrite
+Section: web
+Priority: optional
+Maintainer: Alexey Berezhok
+Build-Depends: debhelper (>= 13), bash, openssl-dev, libssl-dev
+Standards-Version: 4.7.0
+Homepage: https://docs.brepo.ru/nginx-mod_rewrite
+
+Package: nginx-mod-rewrite
+Architecture: any
+Depends: ${shlibs:Depends}, nginx (>= 1.4.0)
+Provides: nginx-http-module, nginx-apache-rewrite
+Description: Nginx rewrite module – dynamic module adding mod_rewrite functionality
+ Dynamic Nginx module implementing Apache mod_rewrite functionality.
diff --git a/packages/deb/rules b/packages/deb/rules
index 255b982..2bc06d9 100755
--- a/packages/deb/rules
+++ b/packages/deb/rules
@@ -7,21 +7,26 @@ export DH_COMPAT=$(DEBHELPER_COMPAT)
export BUILD_DIR = debian/build
export INSTALDIR = debian/nginx-mod-rewrite/usr/share/nginx/modules
export CONFFDIR = debian/nginx-mod-rewrite/etc/nginx/modules
+export DOCSDIR = debian/nginx-mod-rewrite/usr/share/info
+
+# Package metadata (matches debian/control)
+PACKAGE_NAME = nginx-mod-rewrite
-# Set build options
override_dh_auto_build:
bash package_preparer.sh download . system
bash package_preparer.sh build
override_dh_auto_install:
- mkdir -p $(INSTALDIR) $(CONFFDIR)
+ mkdir -p $(INSTALDIR) $(CONFFDIR) $(DOCSDIR)
cp *.so $(INSTALDIR)/
echo 'load_module "$(INSTALDIR)/ngx_http_apache_rewrite_module.so";' > \
$(CONFFDIR)/ngx_http_apache_rewrite_module.conf
+ cp BUILDINFO.txt $(DOCSDIR)/${PACKAGE_NAME}-BUILDINFO.txt
override_dh_auto_clean:
rm -rf debian/build
rm -f *.so
+ rm -f BUILDINFO.txt
rm -rf nginx-*
%:
diff --git a/packages/rpm/nginx-mod-rewrite.spec b/packages/rpm/nginx-mod-rewrite.spec
index 1414bdd..d84a088 100644
--- a/packages/rpm/nginx-mod-rewrite.spec
+++ b/packages/rpm/nginx-mod-rewrite.spec
@@ -1,6 +1,8 @@
%define name nginx-mod-rewrite
%define version 0.1
%define release 1%{?dist}
+%global nginx_version ${NGINX_CUSTOM_VER:-$(rpm -q nginx --queryformat '%{VERSION}' | cut -d'-' -f1)}
+
%define summary "Nginx rewrite module – dynamic module adding mod_rewrite functionality"
%define license Apache-2.0
%define url https://docs.brepo.ru/nginx-mod-rewrite
@@ -21,7 +23,7 @@ BuildRequires: pcre-devel
BuildRequires: zlib-devel
BuildRequires: bash
BuildRequires: wget
-Requires: nginx
+Requires: nginx=%{nginx_version}
%description
Dynamic Nginx module implementing Apache mod_rewrite functionality.
@@ -37,7 +39,8 @@ bash package_preparer.sh build
%install
# Install only the dynamic module
-mkdir -p %{buildroot}%{nginx_moduledir} %{buildroot}%{nginx_moduleconfdir}
+mkdir -p %{buildroot}%{nginx_moduledir} %{buildroot}%{nginx_moduleconfdir} \
+%{buildroot}%{_datarootdir}/info/
cp *.so %{buildroot}%{nginx_moduledir}
echo 'load_module "%{nginx_moduledir}/ngx_http_apache_rewrite_module.so";' \
> %{buildroot}%{nginx_moduleconfdir}/ngx_http_apache_rewrite_module.conf
@@ -49,7 +52,7 @@ fi
%files
-%license LICENSE
+%license LICENSE BUILDINFO.txt
%attr(0640,root,root) %{nginx_moduleconfdir}/ngx_http_apache_rewrite_module.conf
%attr(0755,root,root) %{nginx_moduledir}/ngx_http_apache_rewrite_module.so