What is keep alive in apache & nginx
keep alive connections between browser and server for a certain amount of time seconds, defined amount of connections.
so server don’t need to establish a connection upon refreshing webpage in browser.
so we save CPU resources.
faster loading for client.
too many keepalive connections
a webpage requests 34-150 requests upoloan loading. based on. files for a wordpress 30-50 avg connections.
js,css,img,html etc. (here 4requests enough to load if you build static site like that)
Low requests low dns lookups, faster connection, lower page size.
How to enable keep alive connections in apache htaccess
apache allows allow rewritting rules, but nginx won’t.
So we can add a htaccess in domain top level directory.
but header modules has to enabled on the server.by default it will in shared, managed hosting.
In vps & cloud
Edit apache 2.conf
KeepAliveTimeout
KeepAlive on;
KeepAliveMaxConnections
reload apache server
service apache2 reload
How enable keep alive connections in nginx?
by default it will enable.
you can add max connections
keep alive timeouts
another directive called keepalivedisable.
which disables the keep alive.
Context location; http,server, location.
reload or restart nginx .
systemctl restart nginx
enabling keep alive on wordpress?
check it enabled at server level by curl on online curl tools to view headers.
through htaccess
add htaccess to web directory.
Through plugin:
php can do better job printing headers.
so browser can understand it , stops closing connection, encouraged to cache resources.
Verifying KeepAlive connections ?
curl -I https://yourdomain.com
or Chrome devoper tools, f12
reload the page.
select network tab, all>> click on static file like domain.com/dir/file.css
check request headers
there it will be appears.
note: you cannot use add_header directive to print keep alive, there is no variable out there Unless you code it.
like $upstream_cache_status.
also read if modified since, last modified, etag.
browser caching, or caching headers.
KeepAlive effect on performance?
keeping connection alive costs servers threads to use memory & cpu,
apache mpm event module uses seperate threadpool for KeepAlive connections.
nginx threadpool introduced later to fix non synchronous blocking.
values too high or to low depends upon server size, application type, visits type. Measure & set, monitor and fix.