20140408

Nginx upstream 페일오버 설정

backend 두 대 띄워놓고 앞단 nginx에서 failover. 기본 설정으로도 되긴 되는데 세세하게 다시 봄.

upstream backend {
    server 10.0.1.11:8080 max_fails=2 fail_timeout=10s;
    server 10.0.1.12:8080 max_fails=2 fail_timeout=10s;
    server 10.0.1.13:8080 backup;
    keepalive 16;
}

server {
    listen 80;
    location / {
        proxy_pass http://backend;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        proxy_connect_timeout 2s;
        proxy_read_timeout 10s;
        proxy_next_upstream error timeout http_502 http_503 http_504;
        proxy_next_upstream_tries 2;
    }
}

포인트:

1. max_fails + fail_timeout — fail_timeout 내에 max_fails 만큼 실패하면 그 서버를 fail_timeout 동안 뺌. 기본값이 1/10s 라 너무 민감. 살짝 완화.

2. proxy_next_upstream 에 http_502~504 명시해둠. 기본은 error/timeout만. 502 뜨는 노드는 자동으로 다음 서버로 넘어감.

3. backup 키워드 — 앞의 두 대 다 죽어야 타는 서버. 평소엔 트래픽 안 감.

4. keepalive 16 — 각 워커가 upstream으로 유지할 keepalive conn. http 1.1 써야 효과. Connection "" 로 비워야 함 (그래야 close로 안 닫힘).

헬스체크는 nginx plus에 있는데 오픈소스엔 passive 헬스체크밖에 없다. 능동 체크 하려면 Tengine이나 nginx_upstream_check_module 패치가 필요. 회사에선 그냥 passive로도 충분해서 안 건드림.

추가로 proxy_connect_timeout 2s 는 짧게. 죽은 서버로 빨리 실패해서 다음으로 넘겨야 함. read는 상황 봐서.