nginx

nginx upstream module - part2

꺼비72 2023. 6. 19. 13:15
반응형

server address [parameters];

 

server directive는 서버의 address및 기타 parameter를 정의합니다 . address는 port를 포함한 도메인 이름 또는 IP 주소로 지정하거나,  unix:” prefix 뒤에 UNIX-domain socket path 지정할 수 있습니다. port 를 지정하지 않으면 포트 80이 사용됩니다.  domain name lookup 시 다수의 서버가 lookup될 경우, 도메인 이름으로 지정하면 한 번에 여러 서버를 지정할 수 있습니다.

 

사용할 수 있는 parameter는 다음과 같습니다.

 

 weight=<number>

upstream backend {
    server backend1.example.com weight=5;
    server backend2.example.com;
}

서버의 가중치를 설정한다. 예시 코드에서 backend1.example.com은 backend1.example.com보다 5배 많은 요청을 분배받게 된다. 기본값은 1이다.

 

 max_conns=<number>

server backend1.example.com weight=5 max_conns=256;

nginx의 각 worker별로 동시  연결수를 설정한다. 기본값은 0이며, 제한이 없음을 의미한다. 

 

 max_fails=<number>

server backend1.example.com max_fails=3;

설정한 수 만큼 요청이 실패할 경우 다른 서버에게 요청이 넘어감. 기본값 1

 

 fail_timeout=<time(sec)>

server backend1.example.com max_fails=3 fail_timeout=30;

설정한 시간동안 서버가 응답하지 못하면 실패로 간주한다. 기본값 10s

 

 backup

server  backend1.example.com backup;

backup 파라미터는 해당 서버를 백업 서버로 지정한다. 주요 서버에 장애가 생기면 그때서야 요청이 전달된다.

이 매개변수는 hash, ip_hash, 와 random load balancing methods와 함께 사용할 수 없습니다.

 

down

server  backend1.example.com down;

서버를 영구적으로 사용할 수 없는 것으로 표시하고 요청을 보내지 않음

 

 

 

keepalive connections

upstream backend {
    hash $host$uri 
    server 192.168.1.100;
    server 192.168.1.101;

    keepalive 32;
}

keepalive connections매개변수는 각 worker 프로세스의 캐시에 보존되는 업스트림 서버에 대한 idle keepalive connections 수를 설정합니다. 이 숫자를 초과하면 최근에 가장 적게 사용된 연결부터 닫힘

keepalive지시문은 nginx 작업자 프로세스가 열 수 있는 업스트림 서버에 대한 총 연결 수를 제한하는 것이 아니므로, 새로운 연결을 처리할 수 있게 충분히 작게 설정해야함

기본 round-robin 방식 이외의 load balancing mothed을 사용하는 경우,  keepalive directive 이전에 load balancing mothed를 활성화해야함 

HTTP proxy로 사용될 경우 proxy_http_version 지시문을1.1로 설정하고 "Connection" 헤더 필드를 지워야 합니다.

server {
    ...

    location / {
        proxy_http_version 1.1;
        proxy_set_header Connection "";
    }
}

FastCGI server로 활용할 경우,  fastcgi_keep_conn on ; 이 필요함 

 

keepalive_time time;

하나의 keepalive 연결을 통해 요청을 처리할 수 있는 최대 시간을 제한

keepalive_timeout timeout;

업스트림 서버에 대한 유휴 keepalive 연결이 열린 상태로 유지되는 시간 제한을 설정

 

 

'nginx' 카테고리의 다른 글

nginx proxy hierarchy  (0) 2023.06.19
nginx upstream module - part 3 (dns lookup)  (0) 2023.06.19
Consistant hash  (0) 2023.06.16
nginx upstream module - part1 (load balancing)  (0) 2023.06.15
nginx cache status 확인  (0) 2023.06.15