HAProxy를 이용해서 Load Balancing을 할 때 설정파일에 다음과 같이 추가한다.
물론 사용 언어의 세션 아이디 값에 따라 다르다. 타임아웃은 밀리 세컨드 (1/1000 초)이며 request-learn으로 설정하면
appsession PHPSESSID len 100 timeout 3600000 request-learn mode query-string
메뉴얼 참조...
appsession <cookie> len <length> timeout <holdtime>
[request-learn] [prefix] [mode <path-parameters|query-string>]
Define session stickiness on an existing application cookie.
May be used in sections : defaults | frontend | listen | backend
no | no | yes | yes
Arguments :
<cookie> this is the name of the cookie used by the application and which
HAProxy will have to learn for each new session.
<length> this is the max number of characters that will be memorized and
checked in each cookie value.
<holdtime> this is the time after which the cookie will be removed from
memory if unused. If no unit is specified, this time is in
milliseconds.
request-learn
If this option is specified, then haproxy will be able to learn
the cookie found in the request in case the server does not
specify any in response. This is typically what happens with
PHPSESSID cookies, or when haproxy's session expires before
the application's session and the correct server is selected.
It is recommended to specify this option to improve reliability.
prefix When this option is specified, haproxy will match on the cookie
prefix (or URL parameter prefix). The appsession value is the
data following this prefix.
Example :
appsession ASPSESSIONID len 64 timeout 3h prefix
This will match the cookie ASPSESSIONIDXXXX=XXXXX,
the appsession value will be XXXX=XXXXX.
mode This option allows to change the URL parser mode.
2 modes are currently supported :
- path-parameters :
The parser looks for the appsession in the path parameters
part (each parameter is separated by a semi-colon), which is
convenient for JSESSIONID for example.
This is the default mode if the option is not set.
- query-string :
In this mode, the parser will look for the appsession in the
query string.
When an application cookie is defined in a backend, HAProxy will check when
the server sets such a cookie, and will store its value in a table, and
associate it with the server's identifier. Up to <length> characters from
the value will be retained. On each connection, haproxy will look for this
cookie both in the "Cookie:" headers, and as a URL parameter (depending on
the mode used). If a known value is found, the client will be directed to the
server associated with this value. Otherwise, the load balancing algorithm is
applied. Cookies are automatically removed from memory when they have been
unused for a duration longer than <holdtime>.
The definition of an application cookie is limited to one per backend.
Note : Consider not using this feature in multi-process mode (nbproc > 1)
unless you know what you do : memory is not shared between the
processes, which can result in random behaviours.
Example :
appsession JSESSIONID len 52 timeout 3h
See also : "cookie", "capture cookie", "balance", "stick", "stick-table",
"ignore-persist", "nbproc" and "bind-process".