How to Log Apache Response Headers and Full Body Content for Debugging


3 views

When dealing with complex web infrastructures involving proxies like Novell BorderManager, it's essential to verify whether Apache is actually serving complete responses before they reach the client. The symptom you're describing - where PHP executes successfully but the browser receives truncated HTML - could originate at multiple points in the delivery chain.

Your current approach using mod_log_config is correct for headers. Let's expand on that configuration:

# In httpd.conf or virtual host configuration
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Content-Type}o\" \"%{Content-Length}o\"" debug_format
CustomLog /var/log/apache2/full_debug.log debug_format

This will log:

  • Client IP (%h)
  • Request time (%t)
  • Request line (%r)
  • Status code (%>s)
  • Bytes sent (%b)
  • Content-Type and Content-Length headers

mod_dumpio is indeed the right tool for capturing the full response body. Here's how to configure it properly:

# Load the module
LoadModule dumpio_module modules/mod_dumpio.so

# Configure dumping
DumpIOInput On
DumpIOOutput On
DumpIOLogLevel debug
DumpIOFile "/var/log/apache2/dumpio.log"

Important notes:

  • This will significantly impact performance - use only for debugging
  • Make sure the log directory is writable by Apache
  • Rotate logs frequently as they grow very quickly

If you can't modify Apache configuration, consider adding this to your PHP script:

function logFullResponse($content) {
    $logEntry = sprintf("[%s] Headers: %s\nBody: %s\n\n",
        date('Y-m-d H:i:s'),
        json_encode(headers_list()),
        $content
    );
    file_put_contents('/path/to/response.log', $logEntry, FILE_APPEND);
}

// At the end of your script
register_shutdown_function('logFullResponse', ob_get_contents());

Once you have logging in place, consider these diagnostic steps:

  1. Check if the logged body matches what PHP generated
  2. Compare Content-Length header with actual body size
  3. Test with different content types (HTML, plain text, JSON)
  4. Try disabling output compression temporarily

For complete end-to-end verification, capture the actual network traffic:

sudo tcpdump -i eth0 -s 0 -w /tmp/apache.pcap port 80 and host your.server.ip

Analyze the capture with Wireshark to see exactly what leaves your server.


When debugging web applications, you might encounter situations where responses appear truncated in the browser despite successful PHP script execution. This often occurs in complex environments with proxy servers (like Novell BorderManager) where you need to verify whether the issue originates from Apache or intermediate systems.

The mod_log_config module allows header logging through custom log formats. For example, to log specific headers:


LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Content-Length}o\" \"%{Content-Type}o\"" debug_format
CustomLog /var/log/apache2/debug.log debug_format

This configuration logs client IP, request line, status code, bytes sent, and specific response headers.

While mod_log_config can't log response bodies, mod_dumpio provides complete I/O logging:


# Enable the module
LoadModule dumpio_module modules/mod_dumpio.so

# Configure in httpd.conf or virtual host
DumpIOInput On
DumpIOOutput On
DumpIOLogLevel debug
DumpIOFile "/var/log/apache2/dumpio.log"

Remember to set appropriate permissions for the log file and restart Apache.

For a comprehensive debugging setup combining both approaches:


# Header logging
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Host}i\" \"%{User-Agent}i\" \"%{Content-Type}o\" \"%{Content-Length}o\"" debug_combined
CustomLog /var/log/apache2/debug_combined.log debug_combined

# Body logging
LoadModule dumpio_module modules/mod_dumpio.so
<IfModule mod_dumpio.c>
    DumpIOInput On
    DumpIOOutput On
    DumpIOLogLevel debug
    DumpIOFile "/var/log/apache2/dumpio_debug.log"
</IfModule>

After implementing these configurations:

  1. Check debug_combined.log for mismatched Content-Length headers
  2. Examine dumpio_debug.log for complete response bodies
  3. Compare logged output with what reaches the browser

Remember that:

  • Response bodies may contain sensitive data
  • Log files should have restricted permissions
  • mod_dumpio can significantly impact performance in production

For production debugging, consider using these techniques temporarily or implementing log rotation with sensitive data filtering.