Apache HTTP Server & IBM HTTP Server (IHS) 8/9 Integration Guide

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.


Architectural Options

There are two primary ways to deploy this solution depending on your infrastructure:

Recommended

Option A: Edge-Native CDN Architecture

Cloudflare handles the nonce generation globally, allowing your Apache origin to serve 100% cached HTML securely at zero latency.

🧑‍💻
User Browser
1. Request ➝ 4. Final HTML
⚡
edge-nonce.com
Generates Nonce & Injects
2. Fetch ➝ 3. Cached HTML
🏢
Apache / IHS
Origin & Dispatcher

Option B: Standalone Web Server Architecture

For on-premises environments without Edge-compute. Apache generates the nonce locally for every request.

🧑‍💻
User Browser
1. Request ➝ 2. Local HTML Rewrite
🖥️
Apache / IHS Web Server
Generates Nonce & Mod_Substitute

Option A: Apache/IHS as the Caching Layer (edge-nonce.com at Edge)

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.

1. Caching Behavior (0% Latency Impact)

Because the Adobe AEM OSGi plugin outputs a static placeholder (nonce="CF_EDGE_NONCE"), the HTML is 100% cacheable by Apache's caching engines:

Since the nonce is not generated at the origin, you do not need to implement cache-busting, preserving maximum caching efficiency.

2. Header and Rewrite Rules Configuration

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):

A. Strip Incoming CSP Headers from Backend to Avoid Duplication

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>

B. Handle Direct Traffic (VPC/Bypass)

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>

Option B: Local/On-Premises Integration (Apache/IHS as Generator & Injector)

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:

  1. Generate a request-unique token.
  2. Add it to the Content-Security-Policy header.
  3. Rewrite the response HTML stream to replace the placeholder (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.

Implementation for IHS 9 (Apache 2.4)

IHS 9 supports the full Apache 2.4 feature set, including expressions and flexible output filters.

1. Enable Required Modules

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

2. Configure Virtual Host

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>

Implementation for IHS 8 / 8.5 (Apache 2.2)

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.

1. Enable Required Modules

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

2. Configure Output Filters

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;INCLUDES in AddOutputFilterByType is critical. If INCLUDES runs before SUBSTITUTE, 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.


Security Recommendations for Standalone Deployments

While mod_unique_id is built-in and easy to configure, it has some limitations:

  1. Entropy/Predictability: 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.
  2. Character Set: It may contain characters like @ or - which are valid in nonces but could trigger format parsing edge-cases in strict checkers.

Production-Grade Secure Nonce with 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:

  1. Enable mod_lua in httpd.conf:

    LoadModule lua_module modules/mod_lua.so
    
  2. Register the Lua hook:

    LuaHookFixups /etc/httpd/conf/csp_nonce.lua generate_csp_nonce
    
  3. 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
    
  4. 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"