How to enable keep alive

How to enable keep alive

Tutorial How to enable keep alive

What is keep alive?

In this tutorial we will discuss about How to enable keep alive and what is it.

  1. Keep alive is a method to allow the same tcp connection for HTTP conversation instead of opening a new one with each new request.
  2. More simply put, it is a communication between the web server and the web browser that says "you can grab more than just one file at a time".
  3. Keep alive is also known as a persistant connection

How to enable keep alive ?

  1. Enable keep-alive using .htaccess
    Place .htaccess file with this content

    <ifModule mod_env.c>
    SetEnv KeepAlive On
    SetEnv KeepAliveTimeout 100
    SetEnv MaxKeepAliveRequests 500

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

    Header unset Keep-Alive
    Header set Keep-Alive timeout=100,max=500

  2. Enable keep-alive in Apache
    If you are able to access your Apache config file, you can turn on keep-alive there. The applicable sections are shown below. Open file /etc/httpd/conf/httpd.conf and change following values as shown below :
    [root@Myserver]# nano /etc/httpd/conf/httpd.conf

    KeepAlive On
    MaxKeepAliveRequests 100
    KeepAliveTimeout 15

    And then save and restart Apache service using following command and you have successfully enabled Keep-Alive in your Apache server.
    [root@Myserver]# service httpd restart
    [root@Myserver]# /etc/init.d/httpd restart

  3. Enable keep-alive in NGINX
    Keepalive connections can have a major impact on performance by reducing the CPU and network overhead needed to open and close connections. NGINX terminates all client connections and creates separate and independent connections to the upstream servers. NGINX supports keepalives for both clients and upstream servers. The following directives relate to client keepalives:
    keepalive_requests – The number of requests a client can make over a single keepalive connection. The default is 100, but a much higher value can be especially useful for testing with a load-generation tool, which generally sends a large number of requests from a single client.
    keepalive_timeout – How long an idle keepalive connection remains open.
    keepalive – The number of idle keepalive connections to an upstream server that remain open for each worker process. There is no default value.
    To enable keepalive connections to upstream servers you must also include the following directives in the configuration:

    proxy_http_version 1.1;
    proxy_set_header Connection "";
    keepalive_timeout 65;
    keepalive_requests 100000;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;

Why is keep-alive used?

In order to display webpages a browser must request files from a web server somewhere. There is a brief communication where the browser asks for a file and the web server says yes or no.

The browser gets the HTML file and reads it. The browser will then request the other things that the HTML references like CSS, javascript or images.
Webpages are often a collection of many files and if a new connection (the brief communication) has to be opened for each and everyone of those files it could take significantly longer to display that webpage.
When keep alive is not enabled this process can increase the time it takes to download the page and waste server resources.