Varnish 스티키 세션 설정

1 개요[ | ]

Varnish 스티키 세션 설정
  • 클라이언트 웹브라우저에 'sticky'라는 쿠키변수를 만들어 추후 같은 백엔드로 연결되게 함
  • 아래 예시에는 쿠키 만료기간 Expires값이 60m으로 되어 있음
즉, 최초 접속으로부터 60분 후에는 다른 백엔드로 연결되어 세션데이터가 사라질 수 있음
쿠키 만료기간은 취향(?)에 따라 조정하면 됨
vcl 4.0;
import std;
import directors;
import cookie;
import header;

backend server1 {
    .host = "192.168.0.10";
    .port = "8080";
}
backend server2 {
    .host = "192.168.0.11";
    .port = "8080";
}

sub vcl_init {
	new hdir = directors.hash();
	hdir.add_backend(server1, 1);
	hdir.add_backend(server2, 1);
}

sub vcl_recv {
	cookie.parse(req.http.cookie);
	if(!cookie.isset("sticky")) {
		set req.http.sticky = std.random(1, 4);
		cookie.set("sticky", req.http.sticky);
	}
	set req.http.X-Forwarded-For = client.ip;
	set req.backend_hint = hdir.backend(cookie.get("sticky"));
}

sub vcl_deliver {
	if (req.http.sticky) {
		header.append(resp.http.Set-Cookie, "sticky=" + req.http.sticky
			+ "; Expires=" + cookie.format_rfc1123(now, 60m) + ";");
	}
}

2 같이 보기[ | ]

3 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}