簡體   English   中英

Apache:擺脫標題列表中的Keep-Alive條目

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

我正在使用LAMP(Linux,Apache,MySQL,PHP)服務器。

目前,服務器使用下一個Headers列表發送響應。 我想出於安全原因消除Keep-Alive條目,沒有它就有Headers列表。 是否可以阻止在Headers列表中發送Keep-Alive條目?

當前響應標題:

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

響應標題我想要反而:

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?

據我所知,沒有。 Keep-Alive標頭的整個目的是傳達對客戶端持久連接的需求。 因此擺脫標題擺脫了客戶端和服務器之間的主要通信形式。

也就是說,您可以通過在Apache配置或.htaccess使用unset 來解決此問題,如此處所述 我強調可能因為我有一些header指令在某些版本的Apache中表現不如預期。 但假設有誠意,首先要確保已啟用headers模塊。 在Ubuntu 12.04中你會這樣做:

sudo a2enmod headers

然后將其添加到Apache配置或.htaccess

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

現在重啟Apache:

sudo service apache2 restart

關於header指令的更多細節在這里

在apache中有幾種方法:

  1. 服務器范圍內使用KeepAlive指令( KeepAlive )。 但是,您無法在每個目錄的配置文件中使用此功能,因此設置KeepAlive Off將關閉整個服務器的保持活動狀態。

  2. 將SetEnv或SetEnvIf與mod_env一起使用,並設置nokeepalive環境變量。 這將關閉設置環境的位置的keepalive,或SetEnvIf匹配的規則(取決於您的使用)。 例如

    可以在HTACCESS中

    SetEnv nokeepalive 1

  3. 使用mod_rewrite再次為特定規則設置環境,例如

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

  4. 使用PHP(或任何其他服務器站點語言)並發送標題Connection: close 這將導致Apache省略Keep-Alive標頭,因為連接不再是keepalive。 例如

    PHP

    header('Connection: close');

  5. 使用mod_headers將連接頭設置為再次關閉,例如

    Header set Connection "close"

我個人沒有測試過最后一個,但它應該工作。

KeepAlive行為(可用性和超時)可直接配置: http//httpd.apache.org/docs/2.4/mod/core.html#keepalive

更改這一點主要是性能而非安全性的一個方面,但您可以自由地測試您自己環境中的含義。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM