설치
Bash
# Ubuntu / Debian
sudo apt update && sudo apt install -y haproxy
# CentOS / RHEL
sudo yum install -y haproxy
# 버전 확인
haproxy -v
# 서비스 시작
sudo systemctl enable --now haproxy기본 설정 구조
CODE
/etc/haproxy/haproxy.cfgHAPROXY
global
log /dev/log local0
maxconn 50000
user haproxy
group haproxy
daemon
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5s
timeout client 30s
timeout server 30s
option forwardfor # X-Forwarded-For 헤더 추가
option http-server-close
# 프론트엔드 (클라이언트 연결 수신)
frontend web_front
bind *:80
bind *:443 ssl crt /etc/ssl/mysite.pem
redirect scheme https if !{ ssl_fc } # HTTP → HTTPS 리다이렉트
default_backend web_back
# 백엔드 (서버 풀)
backend web_back
balance roundrobin # 알고리즘: roundrobin, leastconn, source
option httpchk GET /health # 헬스체크 경로
server web1 192.168.1.10:3000 check weight 1
server web2 192.168.1.11:3000 check weight 1
server web3 192.168.1.12:3000 check weight 2 # 가중치 2배헬스체크 설정
HAPROXY
backend api_back
option httpchk GET /api/health HTTP/1.1\r\nHost:\ api.example.com
http-check expect status 200
server api1 10.0.0.1:8080 check inter 2s rise 2 fall 3
server api2 10.0.0.2:8080 check inter 2s rise 2 fall 3
# inter: 체크 간격, rise: 복구 판정 횟수, fall: 장애 판정 횟수
# 백업 서버 (모든 서버 장애 시 사용)
server backup 10.0.0.99:8080 check backup스티키세션 (세션 유지)
HAPROXY
backend app_back
balance leastconn
cookie SERVERID insert indirect nocache # 쿠키 기반 스티키세션
server app1 10.0.0.1:3000 check cookie app1
server app2 10.0.0.2:3000 check cookie app2Stats 대시보드
HAPROXY
frontend stats
bind *:8404
stats enable
stats uri /stats
stats refresh 10s
stats auth admin:StrongPass123 # 기본 인증
stats show-legends브라우저에서 http://서버IP:8404/stats 접속
설정 검증 및 재로드
Bash
# 설정 문법 검사
sudo haproxy -c -f /etc/haproxy/haproxy.cfg
# 무중단 재로드 (기존 연결 유지)
sudo systemctl reload haproxy
# 상태 확인
sudo systemctl status haproxy편집 안내 · Editorial Note
이 가이드는 AI 도구를 활용해 초안을 구성하고 사람이 명령어·문맥을 검토해 발행했습니다. 운영체제와 도구 버전에 따라 결과가 달라질 수 있으므로 적용 전 공식 문서를 함께 확인하세요. 오류를 발견하시면 이메일로 제보해 주세요.
질문 & 답변 (Q&A)
이 가이드에 대해 궁금한 점을 질문해보세요. 확인 후 답변드립니다.