简体   繁体   English

在.htaccess中使用keep-alive功能

[英]Using keep-alive feature in .htaccess

I want to use the keep-alive feature in Apache. 我想在Apache中使用keep-alive功能。 How can I do this with my host (.htaccess file), and what are the best values for the parameters like KeepAliveTimeout ? 如何使用我的主机(.htaccess文件)执行此操作,以及KeepAliveTimeout参数的最佳值是什么?

If Keep-alive is turned on in the Apache configuration, all you need is just set an HTTP header Connection: keep-alive. 如果在Apache配置中打开了Keep-alive,则只需设置HTTP标头Connection:keep-alive。 Eg add following lines to your .htaccess file: 例如,在.htaccess文件中添加以下行:

<ifModule mod_headers.c>
    Header set Connection keep-alive
</ifModule>

You can't control keepalive behaviour in an .htaccess . 您无法控制.htaccess keepalive行为。 Keepalives are a host-level feature, not one where different directories can behave differently depending on the per-directory htaccess info. Keepalive是一个主机级功能,而不是根据每个目录的htaccess信息,不同目录可以表现不同的功能。

If you are on the kind of basic shared hosting that only gives you .htaccess to configure your sites, you can't change the keepalive settings. 如果您使用的是只提供.htaccess配置站点的基本共享主机,则无法更改keepalive设置。 Presumably the hosting company will have set them appropriately, or just left them on the default settings, which are usually fine. 据推测,托管公司将适当地设置它们,或者只是将它们保留在默认设置上,这通常很好。

Yes Keep-alive behavior can be controlled in .htaccess file. 是可以在.htaccess文件中控制保持活动的行为。 First check the server setting by printing $_SERVER and if 首先通过打印$_SERVER和if来检查服务器设置

[HTTP_CONNECTION] => keep-alive

is there then you just have to include the setting in your .htaccess file. 那么你只需要在.htaccess文件中包含该设置。 Add the following line at the end of .htaccess file in your project's root directory. 在项目根目录中的.htaccess文件末尾添加以下行。

<ifModule mod_headers.c>
    Header set Connection keep-alive
</ifModule>

If you have SSH access to your server you should edit the Apache config file. 如果您具有对服务器的SSH访问权限,则应编辑Apache配置文件。 Use these settings as a starter: 将这些设置用作启动器:

  • KeepAlive: on KeepAlive:开启
  • KeepAliveTimeout: 3 seconds KeepAliveTimeout:3秒
  • MaxKeepAliveRequests: 60 MaxKeepAliveRequests:60

This should work for most basic server setups with average traffic. 这适用于具有平均流量的大多数基本服务器设置。 You can always tweak the settings to suit your own needs. 您可以随时调整设置以满足您自己的需要。 See here for more detailed info about this: http://www.giftofspeed.com/enable-keep-alive/ 有关此内容的详细信息,请参见此处: http//www.giftofspeed.com/enable-keep-alive/

If you don't have access to your server you should contact your host. 如果您无法访问服务器,则应与主持人联系。 Changing the keepalive settings on your own by editing the .htaccess file will probably don't work. 通过编辑.htaccess文件来自行更改keepalive设置可能不起作用。

It very much depends on your site and the amount of traffic it receives. 这在很大程度上取决于您的网站及其收到的流量。 If a user comes to your site, then clicks through to another page within the KeepAliveTimeout setting (default is 15), a new TCP does not have to be created. 如果用户访问您的站点,然后单击KeepAliveTimeout设置中的另一个页面(默认值为15),则不必创建新的TCP。 This can really help with overhead. 这真的有助于开销。

On the other hand, any Apache processes that are currently tied up w/ existing visitors will not be able to talk to the new ones. 另一方面,目前与现有访问者捆绑在一起的任何Apache进程都无法与新进程通信。 So you may have to increase the total number of Apache processes that are available. 因此,您可能必须增加可用的Apache进程总数。

In short... it requires tweaking. 简而言之......它需要调整。

你无法控制.htaccess中的保持活动行为

Paste the following code in your .htaccess file: 将以下代码粘贴到.htaccess文件中:

<ifModule mod_headers.c>
    Header set Connection keep-alive
</ifModule>

Then use this website: https://varvy.com/pagespeed/ to check if it's enabled. 然后使用此网站: https//varvy.com/pagespeed/检查是否已启用。

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

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