1. 개요
무중단 배포를 Blue/Green 방식으로 하려면 nginx가 80번 포트로 트래픽을 받아 blue, green 중 정상 동작하는 서버로 로드 밸런싱을 해주어야 한다. 이번에는 이를 위한 설정 파일을 작성해보도록 하자.
2. nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
우선 /etc/nginx/nginx.conf 파일이다. 해당 파일에서 nginx의 기본적인 설정을 할 수 있다. 간단히만 살펴보자.
events {
worker_connections 1024;
}
worker process가 처리할 수 있는 동시 접속자 수를 나타낸다.
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
로그 형식 및 위치를 나타낸다.
http {
include /etc/nginx/conf.d/*.conf;
}
해당 코드로 /etc/nginx/conf.d/ 경로에 있는 모든 .conf 형식의 파일을 읽어들인다.
3. default.conf
upstream web-prod-blue{
server ${private_ip}:8081;
}
upstream web-prod-green{
server ${private_ip}:8082;
}
server {
listen 80;
listen [::]:80;
server_name localhost;
include /etc/nginx/conf.d/service-env.inc;
location / {
proxy_pass http://$service_url;
proxy_set_header X-real-IP $remote_addr;
proxy_set_header X-Forwarded_For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
위 파일은 /etc/nginx/conf.d/ 에 있는 default.conf이다. 무중단 배포를 구축하기 위해 추가 설정을 하는 느낌이라 default.conf에 관련 설정을 작성했다. 관련 코드를 하나씩 살펴보자.
upstream web-prod-blue{
server ${private_ip}:8081;
}
upstream web-prod-green{
server ${private_ip}:8082;
}
upstream 변수이다. 이 변수들은 server 설정에서 nginx가 받은 요청을 어디로 보낼지 결정할 때 사용하는 변수이다. Blue-Green 배포는 두 포트에 서버를 번갈아가며 할당하면서 어떤 포트로 트래픽을 보낼 지 결정해야 하므로 8081, 8082 포트 주소를 선언해주었다.
server {
listen 80;
listen [::]:80;
server_name localhost;
include /etc/nginx/conf.d/service-env.inc;
location / {
proxy_pass http://$service_url;
80 포트로 들어오는 요청을 받고, 도메인 주소를 localhost로 받는다. service-env.inc 파일을 import하고, '/' 경로로 들어오는 요청에 대해 http://$service_url;로 보내주는 역할을 한다.
proxy_set_header X-real-IP $remote_addr;
proxy_set_header X-Forwarded_For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
해당 코드로 nginx로 들어온 헤더 정보를 그대로 넘겨준다.
set $service_url web-prod-blue;
service-env.inc이다. 해당 파일로 proxy_pass에 들어갈 주소를 결정할 수 있다. 추후 스크립트를 통해 해당 파일을 blue/green 번갈아가면서 수정하게 된다.
4. 마무리
다음에는 해당 설정을 바탕으로 CI/CD 스크립트를 작성하고 무중단 배포를 실행해볼 것이다.
'Infra' 카테고리의 다른 글
무중단 배포 - 스크립트 작성하기 (0) | 2025.02.03 |
---|