简体   繁体   English

Apache:摆脱标题列表中的Keep-Alive条目

[英]Apache: Get rid of Keep-Alive entry in the headers list

I'm using LAMP (Linux, Apache, MySQL, PHP) server. 我正在使用LAMP(Linux,Apache,MySQL,PHP)服务器。

Currently the server sends the response with next Headers list. 目前,服务器使用下一个Headers列表发送响应。 I want to eliminate Keep-Alive entry for security reasons, to have Headers list without it. 我想出于安全原因消除Keep-Alive条目,没有它就有Headers列表。 Is it possible to prevent sending the Keep-Alive entry in the Headers list? 是否可以阻止在Headers列表中发送Keep-Alive条目?

Current Response Headers: 当前响应标题:

Cache-Control   private, no-cache, no-store, must-revalidate, post-check=0, pre-check=0
Connection  Keep-Alive
Content-Encoding    gzip
Content-Type    text/html; charset=UTF-8
Date    Thu, 13 Mar 2014 01:43:49 GMT
Expires Thu, 13 Mar 2014 01:43:49 GMT
Keep-Alive  timeout=5, max=200
Last-Modified   Thu, 13 Mar 2014 01:43:49 GMT
Pragma  no-cache
Server  Apache
Transfer-Encoding   chunked
Vary    Accept-Encoding
X-DNS-Prefetch-Control  off
X-Frame-Options sameorigin

Response Headers I Would Like Instead: 响应标题我想要反而:

Cache-Control   private, no-cache, no-store, must-revalidate, post-check=0, pre-check=0
Connection  Keep-Alive
Content-Encoding    gzip
Content-Type    text/html; charset=UTF-8
Date    Thu, 13 Mar 2014 01:43:49 GMT
Expires Thu, 13 Mar 2014 01:43:49 GMT
Last-Modified   Thu, 13 Mar 2014 01:43:49 GMT
Pragma  no-cache
Server  Apache
Transfer-Encoding   chunked
Vary    Accept-Encoding
X-DNS-Prefetch-Control  off
X-Frame-Options sameorigin
Is it possible to prevent sending the Keep-Alive entry in the Headers list?

To my knowledge, no. 据我所知,没有。 The whole purpose of the Keep-Alive header is to communicate the need for a persistent connection to the client. Keep-Alive标头的整个目的是传达对客户端持久连接的需求。 So getting rid of the headers gets rid of the main form of communication between the client & the server. 因此摆脱标题摆脱了客户端和服务器之间的主要通信形式。

That said, you might be able to get it unset by using unset in your Apache config or .htaccess as explained here . 也就是说,您可以通过在Apache配置或.htaccess使用unset 来解决此问题,如此处所述 I emphasize might since I have had header directives not behave as expected in some versions of Apache. 我强调可能因为我有一些header指令在某些版本的Apache中表现不如预期。 But assuming good faith, first be sure the headers module is enabled. 但假设有诚意,首先要确保已启用headers模块。 In Ubuntu 12.04 you would do this: 在Ubuntu 12.04中你会这样做:

sudo a2enmod headers

And then add this to your Apache config or .htaccess : 然后将其添加到Apache配置或.htaccess

<IfModule mod_headers.c>
  Header unset Keep-Alive
</IfModule>

Now restart Apache: 现在重启Apache:

sudo service apache2 restart

More details on the header directive are here . 关于header指令的更多细节在这里

There are a few ways to this in apache: 在apache中有几种方法:

  1. Server-wide using the KeepAlive directive ( KeepAlive ). 服务器范围内使用KeepAlive指令( KeepAlive )。 However you can not have this in per-directory configuration files, so setting KeepAlive Off will turn off keep alive for the entire server. 但是,您无法在每个目录的配置文件中使用此功能,因此设置KeepAlive Off将关闭整个服务器的保持活动状态。

  2. Using SetEnv or SetEnvIf with mod_env, and set the nokeepalive environmental variable. 将SetEnv或SetEnvIf与mod_env一起使用,并设置nokeepalive环境变量。 This will turn off keepalive for the location where the environmental is set, or the rule that is matched by SetEnvIf (depending with you use). 这将关闭设置环境的位置的keepalive,或SetEnvIf匹配的规则(取决于您的使用)。 eg 例如

    can be in HTACCESS 可以在HTACCESS中

    SetEnv nokeepalive 1

  3. Using mod_rewrite to again set the environmental for a specific rule, eg 使用mod_rewrite再次为特定规则设置环境,例如

    RewriteRule some-file.html - [E=nokeepalive:1] RewriteRule some-file.html - [E = nokeepalive:1]

  4. Using PHP (or any other server site language) and sending the header Connection: close . 使用PHP(或任何其他服务器站点语言)并发送标题Connection: close This will cause Apache to omit the Keep-Alive header, since the connection is no longer keepalive. 这将导致Apache省略Keep-Alive标头,因为连接不再是keepalive。 eg 例如

    php PHP

    header('Connection: close');

  5. Use mod_headers to set the connection header to close again, eg 使用mod_headers将连接头设置为再次关闭,例如

    Header set Connection "close"

I personally have not tested the last one, but it should work. 我个人没有测试过最后一个,但它应该工作。

KeepAlive behavior (availability and timeouts) is directly configurable: http://httpd.apache.org/docs/2.4/mod/core.html#keepalive KeepAlive行为(可用性和超时)可直接配置: http//httpd.apache.org/docs/2.4/mod/core.html#keepalive

Changing this is primarily an aspect of performance rather than security, but you're free to test the implications in your own environment. 更改这一点主要是性能而非安全性的一个方面,但您可以自由地测试您自己环境中的含义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM