How can I enable keep-alive?

夙愿已清 提交于 2019-11-30 02:50:29

Keep-alive is using the same tcp connection for HTTP conversation instead of opening new one with each new request. You basically need to set HTTP header in your HTTP response

Connection: Keep-Alive

Read more here

Configure Apache KeepAlive settings

Open up apache’s configuration file and look for the following settings. On Centos this file is called httpd.conf and is located in /etc/httpd/conf. The following settings are noteworthy:

  • KeepAlive: Switches KeepAlive on or off. Put in “KeepAlive on” to turn it on and “KeepAlive off” to turn it off.

  • MaxKeepAliveRequests: The maximum number of requests a single persistent connection will service. A number between 50 and 75 would be plenty.

  • KeepAliveTimeout: How long should the server wait for new requests from connected clients. The default is 15 seconds which is way too high. Set it to between 1 and 5 seconds to avoid having processes wasting RAM while waiting for requests.

Read more about benefits of keep alive connection here: http://abdussamad.com/archives/169-Apache-optimization:-KeepAlive-On-or-Off.html

I had the same problem and after a bit of research I found that the two most popular ways to do it are:

  1. If you do not have access to your webserver config file you can add HTTP headers yourself using a .htaccess file by adding this line of code:

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

  2. If you are able to access your Apache config file, you can turn on keep-alive there by changing these 3 lines in httpd.conf file found here /etc/httpd/conf/

    KeepAlive On

    MaxKeepAliveRequests 0

    KeepAliveTimeout 100

You can read more from this source which explains it better than me https://varvy.com/pagespeed/keep-alive.html

To enable keep-alive through .htaccess you need to add the following code to your .htaccess file:

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

When you have "keep-alive" enabled you tell the browser of your user to use one TCP/IP connection for all the files(images, scripts,etc.) your website loads instead of using a TCP/IP connection for every single file. So it keeps a single connection "alive" to retrieve all the website files at once. This is much faster as using a multitude of connections. There are various ways to enable keep-alive. You can enable it by

  • Using/Editing the .htaccess file
  • Enabling it through access to your web server(Apache, Windows server, etc.)

Go here for more detailed information about this.

With the "Enable Compression" part they mean you should enable GZIP compression (if your web host hasn't already enabled it, as it's pretty much the default nowadays). The GZIP compression technique makes it possible for your web files to be compressed before they're being sent to your users browser. This means your user has to download much smaller files to fully load your web pages.

To enable KeepAlive configuration, Go to conf/httpd.conf in Apache configuration and set the below property : KeepAlive On

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!