nginx에는 proxy_pass지시자로 원본서버(upstream서버)로 traffic을 보낼 수 있는데 upstream 모듈을 사용하면 다양한 형태로 upstream을 구성할 수 있다.
server {
listen 80;
server_name my;
location / {
proxy_cache my_zone;
proxy_cache_key $host$uri$is_args$args;
proxy_pass http://org;
add_header X-CACHE-STATUS $upstream_cache_status;
}
}
위 구성은 proxy_pass로 http://org을 지정하고 있고, 이는 nginx 가 loading될 때 dns lookup을 통해 획득한 IP로 트래픽을 보내게 된다. 현재 서버구성은 localhost로 proxy서버도메인(my)와 origin도메인(org) 모두 localhost로 설정되어 있다.
$ cat /etc/hosts
127.0.0.1 my
127.0.0.1 org
nginx upsteream 모듈 사용하기
이를 host에 지정하지 않고, 아래와 같이 upstream모듈을 이용해서도 구성할 수도 있다.
proxy_pass의 lookup순서는 upstream name > hosts > dns lookup 순이다.
server {
listen 80;
server_name my;
location / {
proxy_cache my_zone;
proxy_cache_key $host$uri$is_args$args;
proxy_pass http://org;
add_header X-CACHE-STATUS $upstream_cache_status;
}
}
upstream org {
server 127.0.0.1;
}
만약 upstream서버가 여러대일 경우 아래와 같이 다중 서버로 요청을 분산시킬 수 있다. 어느서버로 요청되는지를 확인하기 위해 X-CACHE-UPSTREAM이라는 해더를 추가설정하고, upstream으로 모두 트래픽이 갈 수 있게 cache가 되지 않게 설정하였다. (proxy_no_cache 지시문)
* 참고사항 : proxy_cache_bypass는 bypass로 가져온 contents는 cache되어 이후 HIT될 수 있음
server {
listen 80;
server_name my;
location / {
proxy_cache my_zone;
proxy_cache_key $host$uri$is_args$args;
proxy_pass http://org;
add_header X-CACHE-STATUS $upstream_cache_status;
add_header X-CACHE-UPSTREAM $upstream_addr;
proxy_no_cache 1;
}
}
upstream org {
server 192.168.1.100;
server 192.168.1.101;
server 192.168.1.102;
}
예전에 만든 purge툴로 이전 cache된 contents를 퍼지 후 curl로 확인해보면 위 upstream 이 순차적으로 요청됨을 알 수 있다. 기본값은 weighted round-robin balancing 으로 분배된다.
$ purge my/1.json
/var/cache/nginx/5/69/1f0419833724d7ff705cd5204bcd1695 (KEY: my/1.json) file is deleted
아래와 같이 요청이 순차적으로 분배되는 것을 확인할 수 있다
$ for i in {1..5}; do curl -Is my/$i.json | grep UPSTREAM ;done
X-CACHE-UPSTREAM: 192.168.1.100:80
X-CACHE-UPSTREAM: 192.168.1.101:80
X-CACHE-UPSTREAM: 192.168.1.102:80
X-CACHE-UPSTREAM: 192.168.1.100:80
X-CACHE-UPSTREAM: 192.168.1.100:80
하지만 다시 요청하면 Round-Robin 이라 기존과 동일한 순서로 요청되지 않는다.
$ for i in {1..5}; do curl -Is my/$i.json | grep UPSTREAM ;done
X-CACHE-UPSTREAM: 192.168.1.100:80
X-CACHE-UPSTREAM: 192.168.1.100:80
X-CACHE-UPSTREAM: 192.168.1.102:80
X-CACHE-UPSTREAM: 192.168.1.100:80
X-CACHE-UPSTREAM: 192.168.1.100:80
weighted round-robin balancing 이므로 weight값을 높이면 가중치만큼 적용된다 (weight 기본값 1)
upstream org {
server 192.168.1.100 weight=3;
server 192.168.1.101;
server 192.168.1.102;
}
$ for i in {1..10}; do curl -Is my/$1.json | grep UPSTREAM ;done
X-CACHE-UPSTREAM: 192.168.1.102:80
X-CACHE-UPSTREAM: 192.168.1.100:80
X-CACHE-UPSTREAM: 192.168.1.100:80
X-CACHE-UPSTREAM: 192.168.1.101:80
X-CACHE-UPSTREAM: 192.168.1.100:80
X-CACHE-UPSTREAM: 192.168.1.102:80
X-CACHE-UPSTREAM: 192.168.1.100:80
X-CACHE-UPSTREAM: 192.168.1.100:80
X-CACHE-UPSTREAM: 192.168.1.101:80
X-CACHE-UPSTREAM: 192.168.1.100:80
upstream 서버와 통신하는 동안 오류가 발생하면 요청은 다음 서버로 전달되며 작동하는 모든 서버가 시도될 때까지 계속됩니다. 어떤 서버에서도 성공적인 응답을 얻지 못한 경우 클라이언트는 마지막 서버와의 통신 결과를 받게 됩니다.
(아래는 192.168.1.100/24 ip를 삭제해 통신이 안되게 하면 next upstream인 192.168.1.101,102로 통신함을 알 수있다)
$ ip addr del 192.168.1.100/24 dev eno1
$ for i in {1..5}; do curl -Is my/1.json | grep UPSTREAM ;done
X-CACHE-UPSTREAM: 192.168.1.100:80, 192.168.1.101:80
X-CACHE-UPSTREAM: 192.168.1.100:80, 192.168.1.102:80
X-CACHE-UPSTREAM: 192.168.1.102:80
X-CACHE-UPSTREAM: 192.168.1.102:80
X-CACHE-UPSTREAM: 192.168.1.101:80
load balancing 알고리즘
nginx는 아래와 같이 여러가지 Load balancing 알고리즘을 지원한다.
- hash <key>: 클라이언트-서버 매핑이 해시된 key값을 기반으로 하는 서버 그룹에 대한 부하 분산 방법을 지정
- <key> 에는 텍스트, 변수 및 이들의 조합이 포함될 수 있음
- 서버 그룹에서 서버를 추가하거나 제거하면 대부분의 키가 다른 서버에 다시 매핑됨
- consistent 매개변수가 지정 되면 ketama consistent hashing method이 대신 사용되어 서버그룹에 서버가 추가되거나 제거될 때 몇 개의 키만 다른 서버에 다시 매핑되므로 캐쉬 적중률을 높이는데 도움이 됨 - ip_hash: Client IP 주소를 기반으로 서버 간에 요청이 분산되는 부하 분산 방법
- 클라이언트 IPv4 주소의 처음 세 octet (/24기반 netaddress가 같을 때 동일서버로 요청)
또는 전체 IPv6 주소가 해싱 키로 사용됨
- 동일한 클라이언트의 요청이 항상 동일한 서버로 전달이 필요할 때 사용 가능
- server 지시자의 down , weight 매개변수가 적용됨 - random : 랜덤으로 분배한다. 하지만 weight(가중치)가 있을경우 weight를 고려한다.
- least_conn: 가장 활성 연결 수가 적은 곳을 선택한다. weight를 고려한다.
- least_time: 평균 연결시간이 가장 짧으며 활성 연결 수가 적은 곳을 선택한다. weight 고려한다.
upstream org {
hash $host$uri;
server 192.168.1.100;
server 192.168.1.101;
server 192.168.1.102;
}
hash key를 $host$uri로 설정하고 URI에 따라 upstream처리가 분산처리된다.
$ for i in {1..5}; do curl -Is my/$i.json | grep UPSTREAM ;done
X-CACHE-UPSTREAM: 192.168.1.102:80
X-CACHE-UPSTREAM: 192.168.1.100:80
X-CACHE-UPSTREAM: 192.168.1.102:80
X-CACHE-UPSTREAM: 192.168.1.101:80
X-CACHE-UPSTREAM: 192.168.1.101:80
재 요청해도 기존 URL은 동일한 서버로 요청됨을 확인할 수 있다.
$ for i in {1..5}; do curl -Is my/$i.json | grep UPSTREAM ;done
X-CACHE-UPSTREAM: 192.168.1.102:80
X-CACHE-UPSTREAM: 192.168.1.100:80
X-CACHE-UPSTREAM: 192.168.1.102:80
X-CACHE-UPSTREAM: 192.168.1.101:80
X-CACHE-UPSTREAM: 192.168.1.101:80
서버 장애시에도 장애난 서버로의 요청은 타 서버로 재 분배되고 기존 요청은 유지된다.
$ ip addr del 192.168.1.100/24 dev eno1
$ for i in {1..5}; do curl -Is my/$i.json | grep UPSTREAM ;done
X-CACHE-UPSTREAM: 192.168.1.102:80
X-CACHE-UPSTREAM: 192.168.1.100:80, 192.168.1.101:80
X-CACHE-UPSTREAM: 192.168.1.102:80
X-CACHE-UPSTREAM: 192.168.1.101:80
X-CACHE-UPSTREAM: 192.168.1.101:80
하지만 서버목록에 대한 순서가 아래와 같이 변경되면 hash목록이 갱신되어 기존과 전혀다른 서버로 요청된다.
upstream org {
hash $host$uri;
server 192.168.1.102;
server 192.168.1.100;
server 192.168.1.101;
}
$ for i in {1..5}; do curl -Is my/$i.json | grep UPSTREAM ;done
X-CACHE-UPSTREAM: 192.168.1.101:80
X-CACHE-UPSTREAM: 192.168.1.102:80
X-CACHE-UPSTREAM: 192.168.1.101:80
X-CACHE-UPSTREAM: 192.168.1.100:80, 192.168.1.102:80
X-CACHE-UPSTREAM: 192.168.1.102:80
서버목록이 추가되거나 삭제되어도 기존 URL이 기존 서버로 요청되지 않는다.
upstream org {
hash $host$uri;
server 192.168.1.100;
server 192.168.1.101;
server 192.168.1.102;
server 192.168.1.103;
}
$ for i in {1..5}; do curl -Is my/$i.json | grep UPSTREAM ;done
X-CACHE-UPSTREAM: 192.168.1.103:80
X-CACHE-UPSTREAM: 192.168.1.103:80
X-CACHE-UPSTREAM: 192.168.1.103:80
X-CACHE-UPSTREAM: 192.168.1.102:80
X-CACHE-UPSTREAM: 192.168.1.102:80
이렇게 서버 추가, 삭제, 변경 시에도 요청이 최대한 기존 서버로 유지되게 할려면 consistent 매개변수를 추가해 주면 된다.
upstream org {
hash $host$uri consistent;
server 192.168.1.100;
server 192.168.1.101;
server 192.168.1.102;
}
$ for i in {1..5}; do curl -Is my/$i.json | grep UPSTREAM ;done
X-CACHE-UPSTREAM: 192.168.1.102:80
X-CACHE-UPSTREAM: 192.168.1.100:80
X-CACHE-UPSTREAM: 192.168.1.100:80
X-CACHE-UPSTREAM: 192.168.1.101:80
X-CACHE-UPSTREAM: 192.168.1.100:80
서버 순서 변경 시에도 위와 같은 순서로 요청됨
upstream org {
hash $host$uri consistent;
server 192.168.1.102;
server 192.168.1.100;
server 192.168.1.101;
}
$ for i in {1..5}; do curl -Is my/$i.json | grep UPSTREAM ;done
X-CACHE-UPSTREAM: 192.168.1.102:80
X-CACHE-UPSTREAM: 192.168.1.100:80
X-CACHE-UPSTREAM: 192.168.1.100:80
X-CACHE-UPSTREAM: 192.168.1.101:80
X-CACHE-UPSTREAM: 192.168.1.100:80
서버 추가 시에도 변경내역을 최소화 할 수 있다. 위의 경우 1/4만큼만 변경되어야 한다.
upstream org {
hash $host$uri consistent;
server 192.168.1.100;
server 192.168.1.101;
server 192.168.1.102;
server 192.168.1.103;
}
$ for i in {1..5}; do curl -Is my/$i.json | grep UPSTREAM ;done
X-CACHE-UPSTREAM: 192.168.1.103:80 > 변경됨
X-CACHE-UPSTREAM: 192.168.1.100:80
X-CACHE-UPSTREAM: 192.168.1.100:80
X-CACHE-UPSTREAM: 192.168.1.101:80
X-CACHE-UPSTREAM: 192.168.1.103:80 > 변경됨
만약 upstream서버쪽에 작업이 있을 경우에도 down으로 잠시 off해 놓으면 해당 요청은 타 서버로 이전된다.
upstream org {
hash $host$uri consistent;
server 192.168.1.100;
server 192.168.1.101 down;
server 192.168.1.102;
}
$ for i in {1..5}; do curl -Is my/$i.json | grep UPSTREAM ;done
X-CACHE-UPSTREAM: 192.168.1.102:80
X-CACHE-UPSTREAM: 192.168.1.100:80
X-CACHE-UPSTREAM: 192.168.1.100:80
X-CACHE-UPSTREAM: 192.168.1.100:80 >> 변경됨
X-CACHE-UPSTREAM: 192.168.1.100:80
http://nginx.org/en/docs/http/ngx_http_upstream_module.html
Module ngx_http_upstream_module
Module ngx_http_upstream_module The ngx_http_upstream_module module is used to define groups of servers that can be referenced by the proxy_pass, fastcgi_pass, uwsgi_pass, scgi_pass, memcached_pass, and grpc_pass directives. Example Configuration upstream
nginx.org
'nginx' 카테고리의 다른 글
nginx upstream module - part2 (0) | 2023.06.19 |
---|---|
Consistant hash (0) | 2023.06.16 |
nginx cache status 확인 (0) | 2023.06.15 |
nginx cache-control 및 priority (0) | 2023.06.14 |
cli로 nginx cache file 찾기, 삭제 및 TTL변경 (0) | 2023.06.14 |