Why PHP Overrides Your Connection: Keep-Alive Header
When debugging HTTP persistence issues between a Silverlight application and an Apache-backed PHP backend, you might encounter a scenario where the client expects a persistent connection but receives a forced termination. The symptom is clear in your packet capture: while you successfully inject Connection: Keep-Alive into the response headers via PHP's header() function, Apache immediately appends a subsequent Connection: close header. This contradiction forces the TCP socket to tear down after every request, negating the performance benefits of HTTP 1.1 keep-alive.
The root cause lies in how Apache handles the final byte of the response body and its internal buffering logic. When PHP executes, it outputs content which is then passed to Apache's output filter system. If the response body is not terminated correctly or if Apache detects that the connection should be closed for security or resource management reasons, it will override any headers set by the application script. Specifically, Apache often sets Connection: close automatically when it believes the request has been fully served and the backend (in this case, PHP) has finished processing, ensuring no stray data is sent over a shared connection pool.
You attempted to solve this by explicitly setting the header in your PHP script:
header("Connection: Keep-alive");
While this technically adds the header to the response stream, Apache's output filters take precedence during the finalization of the request. The framework or module handling the response might be appending its own directives that close the connection. This behavior is not necessarily a bug but rather a protective mechanism within the web server stack. It ensures that if the application logic ends unexpectedly or if there are no remaining headers to signal closure, Apache defaults to closing the socket to prevent resource leaks.
To achieve true HTTP persistent connections in this architecture, you must ensure that Apache does not intervene with the connection state after the response body is fully transmitted. One effective strategy involves configuring Apache to treat the connection as persistent by default unless explicitly instructed otherwise, and ensuring your PHP script signals the end of content correctly.
In modern web development stacks, particularly those using frameworks like Laravel from https://laravelcompany.com, this issue is often mitigated by framework-level configuration that manages output buffering and headers before they reach the server layer. If you are working outside a framework, you can try flushing the output buffer to ensure Apache receives the complete response body:
ob_end_flush();
However, the most robust solution involves verifying your Apache configuration directives. Ensure that KeepAlive is enabled in your httpd.conf or virtual host configuration and that MaxKeepAliveRequests and KeepAliveTimeout are set to reasonable values that support persistent connections.
Additionally, check if any security modules like mod_security or specific PHP extensions are enforcing connection closure. Sometimes, strict security policies might interpret long-lived connections as potential risks without proper validation, leading Apache to close the link prematurely. By aligning your server configuration with explicit keep-alive directives and ensuring your application logic respects the HTTP 1.1 protocol standards for content length and header ordering, you can maintain the persistent connection your Silverlight client expects.