apache 在 .htaccess 中使用 keep-alive 功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2200566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using keep-alive feature in .htaccess
提问by webkul
I want to use the keep-alivefeature in Apache. How can I do this with my host (.htaccess file), and what are the best values for the parameters like KeepAliveTimeout?
我想keep-alive在 Apache 中使用该功能。我怎样才能用我的主机(.htaccess 文件)做到这一点,参数的最佳值是KeepAliveTimeout什么?
采纳答案by bobince
You can't control keepalive behaviour in an .htaccess. Keepalives are a host-level feature, not one where different directories can behave differently depending on the per-directory htaccess info.
您无法控制.htaccess. Keepalives 是一种主机级别的功能,而不是根据每个目录的 htaccess 信息,不同目录的行为不同的功能。
If you are on the kind of basic shared hosting that only gives you .htaccessto configure your sites, you can't change the keepalive settings. Presumably the hosting company will have set them appropriately, or just left them on the default settings, which are usually fine.
如果您使用的是只允许您.htaccess配置站点的基本共享主机,则无法更改 keepalive 设置。据推测,托管公司会适当地设置它们,或者只是将它们保留为默认设置,这通常很好。
回答by pronskiy
If Keep-alive is turned on in the Apache configuration, all you need is just set an HTTP header Connection: keep-alive. E.g. add following lines to your .htaccess file:
如果在 Apache 配置中开启了 Keep-alive,您只需要设置一个 HTTP 头连接:keep-alive。例如,将以下行添加到您的 .htaccess 文件中:
<ifModule mod_headers.c>
Header set Connection keep-alive
</ifModule>
回答by anil kumar
Yes Keep-alive behavior can be controlled in .htaccessfile.
First check the server setting by printing $_SERVERand if
是的,可以在.htaccess文件中控制保持活动行为。首先通过打印检查服务器设置$_SERVER,如果
[HTTP_CONNECTION] => keep-alive
is there then you just have to include the setting in your .htaccessfile. Add the following line at the end of .htaccessfile in your project's root directory.
那么你只需要在你的.htaccess文件中包含设置。在项目根目录中的.htaccess文件末尾添加以下行。
<ifModule mod_headers.c>
Header set Connection keep-alive
</ifModule>
回答by William Dresker
If you have SSH access to your server you should edit the Apache config file. Use these settings as a starter:
如果您可以通过 SSH 访问您的服务器,您应该编辑 Apache 配置文件。使用这些设置作为入门:
- KeepAlive: on
- KeepAliveTimeout: 3 seconds
- MaxKeepAliveRequests: 60
- 保持活动:开启
- KeepAliveTimeout:3 秒
- 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 设置可能不起作用。
回答by William Dresker
you can't control keep-alive behavior in .htaccess
你无法控制 .htaccess 中的保持活动行为
回答by tambler
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. This can really help with overhead.
这在很大程度上取决于您的网站及其接收的流量。如果用户访问您的站点,然后单击 KeepAliveTimeout 设置(默认为 15)内的另一个页面,则不必创建新的 TCP。这真的可以帮助减少开销。
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. So you may have to increase the total number of Apache processes that are available.
另一方面,当前与现有访问者绑定的任何 Apache 进程将无法与新访问者对话。因此,您可能需要增加可用的 Apache 进程总数。
In short... it requires tweaking.
简而言之......它需要调整。
回答by Baba
Paste the following code in your .htaccessfile:
将以下代码粘贴到您的.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/检查它是否已启用。

