How to override Apache keep-alive header using php

2019-06-27 04:51发布

问题:

in my httpd.conf there is:

KeepAliveTimeout 1

I'm trying to override Apache KeepAliveTimeout setting in a single php script (not in the entire server) so I have a php script with:

header("Keep-Alive: timeout=60, max=100"); 

but it doesn't seem to make any difference. still I get in the response:

Keep-Alive:timeout=1, max=50

any ideas how to solve this?

回答1:

You can't do that. It's there in place for a valid reason.

Apache v2.2 Core Features

KeepAliveTimeout Directive

The number of seconds Apache will wait for a subsequent request before closing the connection. Once a request has been received, the timeout value specified by the Timeout directive applies.

Setting KeepAliveTimeout to a high value may cause performance problems in heavily loaded servers. The higher the timeout, the more server processes will be kept occupied waiting on connections with idle clients.



回答2:

Try setting it in the .htaccess file combined with a FilesMatch directive. See this post.



回答3:

<?php
header('Connection: close');
// other php code here...
// ...

From RFC 2616, Section 14.10:

HTTP/1.1 defines the "close" connection option for the sender to
signal that the connection will be closed after completion of the
response. For example,

   Connection: close

in either the request or the response header fields indicates that the connection SHOULD NOT be considered `persistent' (section 8.1)
after the current request/response is complete.

HTTP/1.1 applications that do not support persistent connections MUST include the "close" connection option in every message.

Related: What does “Connection: close” mean when used in the response message?