Complete configuration and deep-dive for Apache and IBM HTTP Server.
This guide details how to integrate the Edge CSP Nonce architecture with Apache HTTP Server and IBM HTTP Server (IHS) versions 8, 8.5, and 9.
There are two primary ways to deploy this solution depending on your infrastructure:
Cloudflare handles the nonce generation globally, allowing your Apache origin to serve 100% cached HTML securely at zero latency.
For on-premises environments without Edge-compute. Apache generates the nonce locally for every request.
In the standard architecture, the edge-nonce.com Service is responsible for generating the cryptographically secure nonce, setting the Content-Security-Policy header, and replacing the HTML placeholder (CF_EDGE_NONCE).
In this setup, your Apache/IHS servers (often running the Adobe AEM Dispatcher module or standard cache modules) act as the backend origin.
Because the Adobe AEM OSGi plugin outputs a static placeholder (nonce="CF_EDGE_NONCE"), the HTML is 100% cacheable by Apache's caching engines:
mod_dispatcher): Will cache the compiled HTML page with the static placeholder on disk.mod_cache / mod_disk_cache): Will cache responses natively without issues.Since the nonce is not generated at the origin, you do not need to implement cache-busting, preserving maximum caching efficiency.
To prevent conflicts between headers set by your origin Apache/IHS servers and the Cloudflare Edge Worker, apply the following settings in your Apache/IHS configuration (httpd.conf or virtual host configuration):
If your backend application or Apache configs set a default CSP, ensure that Cloudflare is the single source of truth for CSP headers. You can strip backend-generated CSP headers before they hit the edge:
<IfModule mod_headers.c>
# Unset default CSP headers so Cloudflare can generate and enforce the real one
Header unset Content-Security-Policy
Header unset X-Content-Security-Policy
</IfModule>
If developers or internal applications access the Apache/IHS server directly (bypassing Cloudflare), they will see the raw CF_EDGE_NONCE placeholder, which will break inline scripts.
To avoid this in internal dev environments, you can configure Apache/IHS to inject a fallback mock CSP header and replace the placeholder only if the request does not come from Cloudflare:
<IfModule mod_headers.c>
# If the request did NOT go through Cloudflare (missing CF-Ray header), set a loose CSP
SetEnvIf CF-Ray ^$ BYPASSING_CLOUDFLARE
Header set Content-Security-Policy "script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline';" env=BYPASSING_CLOUDFLARE
</IfModule>
If you must host the nonce generation and injection entirely on your own local web servers (without using Cloudflare Workers), you can configure Apache/IHS to dynamically:
Content-Security-Policy header.CF_EDGE_NONCE) with the token.[!WARNING] Running dynamic regex replacement on every HTML request in Apache/IHS consumes more server CPU compared to streaming edge Workers. Perform load testing under enterprise traffic.
IHS 9 supports the full Apache 2.4 feature set, including expressions and flexible output filters.
Ensure the following modules are uncommented in httpd.conf:
LoadModule unique_id_module modules/mod_unique_id.so
LoadModule headers_module modules/mod_headers.so
LoadModule substitute_module modules/mod_substitute.so
LoadModule filter_module modules/mod_filter.so
LoadModule include_module modules/mod_include.so
Configure your virtual host to generate the nonce from mod_unique_id (the environment variable UNIQUE_ID), set the header, and substitute the placeholder.
Because mod_substitute does not evaluate environment variables natively in its target string, we use Server Side Includes (SSI) (mod_include) as an evaluator wrapper.
<VirtualHost *:80>
ServerName www.mybrand.com
DocumentRoot "/www/htdocs"
# 1. Inject the nonce (UNIQUE_ID) into the CSP header
<IfModule mod_headers.c>
Header always set Content-Security-Policy "default-src 'self'; script-src 'self' 'nonce-%{UNIQUE_ID}e' 'strict-dynamic' https:; style-src 'self' 'nonce-%{UNIQUE_ID}e' 'unsafe-inline' https:;"
</IfModule>
# 2. Set up HTML filter chain for replacement
<Location "/">
# Enable Server Side Includes (SSI) execution
Options +Includes
# Declare the output filter chain:
# First substitute the placeholder with the SSI echo block
# Then let mod_include evaluate the SSI block to output the actual UNIQUE_ID
FilterDeclare CSP_NONCE_FILTER
FilterProvider CSP_NONCE_FILTER SUBSTITUTE "%{CONTENT_TYPE} =~ m|^text/html|"
FilterProvider CSP_NONCE_FILTER INCLUDES "%{CONTENT_TYPE} =~ m|^text/html|"
FilterChain CSP_NONCE_FILTER
# Substitute the placeholder with the SSI echo command
# n = normal regex, i = case insensitive
Substitute "s|CF_EDGE_NONCE|<!--#echo var=\"UNIQUE_ID\" -->|ni"
</Location>
</VirtualHost>
IHS 8 and 8.5 are based on Apache 2.2. The syntax is slightly different because Apache 2.2 does not support expressions (like %{CONTENT_TYPE} =~ ...) or <If> blocks natively.
Make sure these modules are loaded:
LoadModule unique_id_module modules/mod_unique_id.so
LoadModule headers_module modules/mod_headers.so
LoadModule substitute_module modules/mod_substitute.so
LoadModule include_module modules/mod_include.so
In Apache 2.2, we declare filters using AddOutputFilterByType and rely on default filter ordering:
<VirtualHost *:80>
ServerName www.mybrand.com
# 1. Set the CSP Header with UNIQUE_ID
Header always set Content-Security-Policy "default-src 'self'; script-src 'self' 'nonce-%{UNIQUE_ID}e'; style-src 'self' 'nonce-%{UNIQUE_ID}e';"
# 2. Substitute & Parse SSI
<Location "/">
# Enable SSI execution
Options +Includes
# Apply Substitute first, then INCLUDES (SSI parser) to the HTML stream
AddOutputFilterByType SUBSTITUTE;INCLUDES text/html
# Substitute placeholder
Substitute "s|CF_EDGE_NONCE|<!--#echo var=\"UNIQUE_ID\" -->|ni"
</Location>
</VirtualHost>
[!IMPORTANT] Filter Order Warning for IHS 8 (Apache 2.2): The order of
SUBSTITUTE;INCLUDESinAddOutputFilterByTypeis critical. IfINCLUDESruns beforeSUBSTITUTE, the SSI engine will not see the<!--#echo var="UNIQUE_ID" -->code because it hasn't been injected yet, leaving the raw SSI command string visible in the final HTML.
While mod_unique_id is built-in and easy to configure, it has some limitations:
UNIQUE_ID is constructed from IP, PID, current time, and a counter. While it is unique per request, it is conceptually predictable under certain circumstances and not cryptographically secure.@ or - which are valid in nonces but could trigger format parsing edge-cases in strict checkers.mod_lua (IHS 9 / Apache 2.4)If you require high-entropy, cryptographically secure nonces locally, you can use mod_lua to read directly from /dev/urandom:
Enable mod_lua in httpd.conf:
LoadModule lua_module modules/mod_lua.so
Register the Lua hook:
LuaHookFixups /etc/httpd/conf/csp_nonce.lua generate_csp_nonce
Create the Lua script (/etc/httpd/conf/csp_nonce.lua):
-- Simple base64 mapping table
local b = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
local function enc64(data)
return ((data:gsub('.', function(x)
local r,b='',x:byte()
for i=8,1,-1 do r=r..(b%2^i-b%2^(i-1)>0 and '1' or '0') end
return r;
end)..'0000'):gsub('%d%d%d%d%d%d', function(x)
if (#x < 6) then return '' end
local c=0
for i=1,6 do c=c+(x:sub(i,i)=='1' and 2^(6-i) or 0) end
return b:sub(c+1,c+1)
end)..({ '', '==', '=' })[#data%3+1])
end
function generate_csp_nonce(r)
-- Read 16 random bytes from /dev/urandom
local urandom = io.open("/dev/urandom", "rb")
if urandom then
local bytes = urandom:read(16)
urandom:close()
-- Encode to base64
local nonce = enc64(bytes)
-- Set request environment variable for mod_headers and SSI
r.subprocess_env["SECURE_CSP_NONCE"] = nonce
end
return apache.OK
end
Reference SECURE_CSP_NONCE instead of UNIQUE_ID in your configuration:
Header always set Content-Security-Policy "default-src 'self'; script-src 'self' 'nonce-%{SECURE_CSP_NONCE}e'; style-src 'self' 'nonce-%{SECURE_CSP_NONCE}e';"
Substitute "s|CF_EDGE_NONCE|<!--#echo var=\"SECURE_CSP_NONCE\" -->|ni"