Skip to content
CYBER SECURE
SYSTEM
Let’s talk security

Compiling Hardened Mainline NGINX 1.31 from Source on Ubuntu LTS: PIE, Full RELRO, HTTP/3 QUIC, TLS 1.2/1.3, and APT Hold Guardrails

When high-severity HTTP/2 and HTTP/3 transport advisories drop, engineering teams on Ubuntu 24.04 LTS frequently decide to build upstream mainline NGINX 1.31.10 from source rather than wait on distribution feature freezes. Done carelessly, however, a source build strips away Ubuntu's compiler exploit mitigations, scatters configuration files into /usr/local/nginx, and gets silently overwritten the next time unattended-upgrades runs. In Part 2 of our 5-Part Linux & Full-Stack Hardening Series, here is the exact drop-in build and TLS hardening workflow we use in the field.

Blueprint for compiling hardened mainline NGINX 1.31.10 from source on Ubuntu 24.04 LTS with PIE, Full RELRO, FORTIFY_SOURCE=3, Intel CET, HTTP/3 QUIC, TLS 1.2/1.3, server_tokens off, and apt-mark hold
Figure 1: Drop-in mainline NGINX 1.31.10 source compilation pipeline preserving systemd layout, ELF binary hardening, and APT downgrade protection.

1. Audit the Existing Distribution Binary Before Compiling

Before replacing /usr/sbin/nginx, inspect the running package's compile-time flags and active dynamic modules so your custom binary never breaks an existing virtual host or systemd unit:

# Inspect current version, OpenSSL linkage, and configure arguments
/usr/sbin/nginx -V

# Check whether any dynamic modules in /etc/nginx/modules-enabled/ are loaded
ls -la /etc/nginx/modules-enabled/ /etc/nginx/sites-enabled/

# Verify systemd PIDFile and ExecStart expectations
systemctl cat nginx.service

If /etc/nginx/modules-enabled/ loads legacy dynamic modules compiled against 1.24.0 (such as ngx_http_geoip_module.so), either recompile matching dynamic modules with --with-compat or remove unused module symlinks so nginx -t passes cleanly on 1.31.10.

2. Match Ubuntu Paths and Enforce GCC 13 Exploit Mitigations

Why Plain ./configure Weakens Worker Security: NGINX workers parse untrusted network headers and TLS/QUIC frames in C before authentication. Compiling without Position Independent Executables (-fPIE -pie), Full RELRO (-Wl,-z,relro,-z,now), -D_FORTIFY_SOURCE=3, stack-clash protection, and Control-Flow Enforcement (-fcf-protection) makes any future memory-corruption bug dramatically easier to weaponize.

Install the minimal C library headers and compile upstream nginx-1.31.10 with explicit Ubuntu paths plus HTTP/2, HTTP/3 (QUIC), PCRE2 JIT, and full ELF hardening:

sudo apt-get update && sudo apt-get -y install build-essential libpcre2-dev zlib1g-dev libssl-dev

mkdir -p /tmp/nginx-src && cd /tmp/nginx-src
curl -fsSLO https://nginx.org/download/nginx-1.31.10.tar.gz
tar -xzf nginx-1.31.10.tar.gz && cd nginx-1.31.10

./configure \
  --prefix=/usr/share/nginx \
  --sbin-path=/usr/sbin/nginx \
  --modules-path=/usr/lib/nginx/modules \
  --conf-path=/etc/nginx/nginx.conf \
  --error-log-path=/var/log/nginx/error.log \
  --http-log-path=/var/log/nginx/access.log \
  --pid-path=/run/nginx.pid \
  --lock-path=/var/lock/nginx.lock \
  --http-client-body-temp-path=/var/lib/nginx/body \
  --http-proxy-temp-path=/var/lib/nginx/proxy \
  --http-fastcgi-temp-path=/var/lib/nginx/fastcgi \
  --http-uwsgi-temp-path=/var/lib/nginx/uwsgi \
  --http-scgi-temp-path=/var/lib/nginx/scgi \
  --user=www-data --group=www-data \
  --with-compat --with-threads --with-file-aio --with-pcre-jit \
  --with-http_ssl_module --with-http_v2_module --with-http_v3_module \
  --with-http_realip_module --with-http_addition_module --with-http_sub_module \
  --with-http_gunzip_module --with-http_gzip_static_module \
  --with-http_auth_request_module --with-http_secure_link_module \
  --with-http_slice_module --with-http_stub_status_module \
  --with-stream --with-stream_ssl_module --with-stream_realip_module --with-stream_ssl_preread_module \
  --with-cc-opt='-O2 -g -fPIE -fstack-protector-strong -fstack-clash-protection -fcf-protection -Wformat -Werror=format-security -D_FORTIFY_SOURCE=3' \
  --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -pie'

make -j"$(nproc)"

3. Atomic Binary Installation & APT Downgrade Protection

Copying directly over a running /usr/sbin/nginx executable with cp can fail with Text file busy (ETXTBSY) because the kernel maps the active binary inode. Using install unlinks the old directory entry and writes a fresh inode cleanly while backing up the prior binary and holding the APT packages:

# Back up prior binary & config, swap inode atomically, and hold APT packages
sudo cp -p /usr/sbin/nginx /usr/sbin/nginx.distro.bak
sudo cp -p /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
sudo install -o root -g root -m 0755 objs/nginx /usr/sbin/nginx
sudo apt-mark hold nginx nginx-common

4. Hardening TLS, Version Banners, Webroot Permissions & Conflicting Servers

Upgrading the binary is only half of edge hygiene. Our workstation audit caught three common configuration oversights alongside the 1.24.0 binary:

  • Legacy TLS Protocols Still Enabled: Older Ubuntu nginx.conf files carried forward across release upgrades often list ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;. Drop TLSv1 and TLSv1.1 immediately.
  • Exposed Version Headers: Uncomment server_tokens off; inside the http {} block so responses return Server: nginx instead of advertising exact patch numbers to automated scanners.
  • World-Writable Webroots & Conflicting Apache Units: Fix accidental 0777/0666 permissions on /var/www/html (chmod 0755 /var/www/html && chmod 0644 /var/www/html/*.html), and disable any dormant apache2.service failing on boot over port 80.
Hardening Check Verification Command Expected Output
Active Binary & Flags /usr/sbin/nginx -V nginx/1.31.10 with -D_FORTIFY_SOURCE=3 & relro
APT Hold Protection apt-mark showhold Lists nginx and nginx-common
Banner Suppression curl -sI http://localhost | grep -i '^Server:' Server: nginx (zero version digits leaked)
Config & TLS Syntax sudo /usr/sbin/nginx -t syntax is ok / test is successful

For HTTP/2 window underflow mitigations, header buffer bounds, and unsafe alias/rewrite virtual-host audits, pair this build guide with our NGINX HTTP/2 & Worker Memory Hardening Service.

Operational Questions on Source-Compiled NGINX

When should we use a source build versus the official nginx.org mainline APT repository?

Compile from source when you need same-hour upstream CVE patches, custom third-party modules, or stricter compiler flags than standard packages provide. For routine long-term maintenance without custom modules, transitioning onto the signed upstream nginx.org mainline repository lets unattended-upgrades deliver future 1.31.x security releases automatically.

How do we upgrade a live production NGINX binary with zero dropped connections?

After installing the new binary to /usr/sbin/nginx and verifying sudo nginx -t, send USR2 to the master PID (sudo kill -USR2 $(cat /run/nginx.pid)) to spawn new master and worker processes alongside the old ones, then send WINCH and QUIT to drain the old master gracefully.

Continue Through the 5-Part Linux Hardening Series

Next: why running apt upgrade still left PHP, Node.js, and Java vulnerable to November 2026's *2Shell exploits—and how to hunt shadow binaries across $PATH.

INTERACTIVE NGINX DATA-PLANE EXPOSURE LAB

Inspect HTTP/2 & worker heap posture

Select your NGINX build lineage and listener configuration to preview worker memory-safety risk and immediate directive mitigations.

01 / NGINX PACKAGE STATE
02 / LISTENER & REWRITE POSTURE
Runtime memory boundary and containment diagramUNTRUSTED INPUTPre-Auth StreamHTTP / SOCKETRUNTIME GATEUnpatched StubOVERFLOW PATHWORKER HEAPExposed MemoryACTION NEEDED
HIGH EXPOSURE • HTTP/2 WINDOW & WORKER HEAP VECTORS OPEN

Unpatched ngx_http_v2_module flow-control accounting and nested alias/rewrite rules risk SIGSEGV worker crashes or heap corruption.

nginx -V 2>&1 && nginx -T | grep -E "http2|alias |rewrite "
large_client_header_buffers 4 8k;