How to enable keep-alive in haproxy?
HTTP 1.1 operates in keep-alive mode by default, see RFC7230. So you don't need to explicitely set the Connection header in order to use persistent connections (keep-alive mode).
The connection header is only needed when using HTTP 1.0 because it was designed to close the connection after every request.
Thus, to verify that HAProxy is operating in Keep-alive mode, you need to send multiple HTTP requests (and not just one) with curl, and check if there was only one connection established and used.
This serverfault post shows how to do that. (Notice the "Connecting to .." and "Closing connection" lines)
HAProxy Keep-Alive not working as expected
- you should ensure your server does not close the connection.
- you should enable the "option prefer-last-server", in your HAProxy's defaults section.
- you should re-read the "option http-keep-alive" documentation of HAProxy: http://cbonte.github.io/haproxy-dconv/configuration-1.6.html#option%20http-keep-alive.
It clearly states that:
If the client request has to go to another backend or another server due to content switching or the load balancing algorithm, the idle connection will immediately be closed and a new one re-opened. Option "prefer-last-server" is available to try optimize server selection so that if the server currently attached to an idle connection is usable, it will be used.
(hence point 2)
So your observations looks normal.
HAProxy closes long living TCP connections ignoring TCP keepalive
TCP keep alive is at the transport layer and is only used to do some traffic on the connection so intermediate systems like packet filters don't loose any states and that the end systems can notice if the connection to the other side broke (maybe because something crashed or a network cable broke).
TCP keep alive has nothing to do with the application level idle timeout which you have set explicitly to 200s:
timeout client 200000ms
timeout server 200000ms
This timeouts gets triggered if the connection is idle, that is if no data get transferred. TCP keep alive does not transport any data, the payload of these packets is empty.
Related Topics
Script Produces Different Result When Executed by Bash Than by Cron
How to Find Files with Same Size
Linux Split a Column into Two Different Columns in a Same CSV File
Why Fftw on Windows Is Faster Than on Linux
Fail If a Script Expects Input or Entering Passwords
Shuffle Output of Find with Fixed Seed
How to Install Rpy2 via Conda Using Default R Installation
Using Scanf into Global or Local Variables (On the Stack), 32-Bit Calling Convention
How to Move First Column to Last Column in Unix
How to Start a Process That Won't End When My Ssh Session Ends
Shell Script Issue with Filenames Containing Spaces
Where Is $Path Set? Specifically Where Is My MAC Port Path Being Set
Get and Use a Password with Special Characters in Bash Shell
Environment Variable Used in Shell Script Appear Blank in Log File When Run by Cron
How to Get Docker Commands to Run in the Background with Nohup
After Segfault: Is There a Way, to Check If Pointer Is Still Valid
Export_Symbol in Kernel Module | Undefined Symbol During Insmod