nginx 를 통하여 웹서버 로드 밸런싱을 구축하여보았다. 아래와 같이 서버가 구성되어있는 경우이다.
서버#1 |
서버#2 |
서버#3 |
IIS 포트 80 |
IIS 포트 80 |
nginx 포트 80 |
nginx 를 사용하여 collectd 연동외에는 오로지 로드 밸런서의 역할만 수행하도록 설정을 해보았다.
#vi /etc/nginx/nginx.conf
user nginx;
worker_processes 4; -> CPU 코어 개수만큼 설정
worker_cpu_affinity 0001 0010 0100 1000; -> 상기 설정에 맞춰...
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
log_format upstreamlog '[$time_local] $remote_addr - $remote_user - $server_name '
'to: $upstream_addr: $request upstream_response_time $upstream_response_time msec '
'$msec request_time $request_time';
access_log /var/log/nginx/access.log upstreamlog;
upstream 그룹명#1 {
ip_hash;
server 서버#1의IP:80 weight=1 max_fails=5 fail_timeout=15s;
server 서버#2의IP:80 weight=5 max_fails=5 fail_timeout=15s;
}
server {
listen 80;
server_name www.example.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Queue-Start "t=${msec}000";
location / {
proxy_pass http://그룹명#1;
}
location /status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
# location / {
# if ($request_method = POST)
# {
# proxy_pass http://그룹명#2;
# break;
# }
# proxy_pass http://그룹명#1;
# }
}
}
log_format 의 경우, 디버그가 용이하도록 일부 내용을 보강하였으며, proxy_set_header 를 통하여 헤더 정보를 일부 추가하였다. 주석 처리한 아래부분의 내용은 POST 요청에 한하여 특정 서버군에게 전달할 경우의 설정이다.
upstream 블록의 옵션들은 다음과 같다.(옵션에 대하여 이상하게 설명한 글들이 종종 보인다.;;)
ip_hash |
동일 사용자(요청자)에 대하여 같은 업스트림 서버로 연결되도록 한다. |
weight=n |
서버 가중치. 만약 5라면 1로 설정한 서버에 비하여 5배 더 자주 선택된다. |
max_fails=n |
설정한 값 n만큼 실패가 발생하면 서버가 죽은 것으로 간주한다. |
fail_timeout=n |
max_fails 가 설정된 상태에서 n 시간만큼 응답하지 않으면 죽은 것으로 간주한다. |
down |
해당 서버를 사용하지 않는다. ip_hash 옵션이 설정된 경우에만 유효. |
backup |
대기하고 있다가 설정된 모든 서버가 동작하지 않을 때 사용되는 서버. |
5