리눅스

[draft] HAProxy에서 Basic Authentication(접속 암호) 설정하기

SangChul Dot Kr Blog 2025. 10. 22. 14:34

HAProxy에서 Basic Authentication(접속 암호) 설정하기

HTTP Basic Authentication(아이디/비밀번호 팝업 로그인)을 구현할 수 있습니다.

1. HAProxy 설정 파일 열기

sudo vim /etc/haproxy/haproxy.cfg

2. 사용자 계정(userlist) 정의

userlist 블록을 통해 인증 계정을 정의합니다.

#---------------------------------------------------------------------
# 사용자 계정 설정 (Basic Authentication)
#---------------------------------------------------------------------
userlist basic-auth
    # user <아이디> password <암호의 bcrypt 해시값>
    # user elastic password ZWxhc3RpYzplbGFzdGlj  # (예시: 인코딩된 값)
    user elastic insecure-password elastic
    user guest   insecure-password guest
Tip :
실제 운영 환경에서는 insecure-password 대신 password (bcrypt 해시 기반)을 사용하는 것이 안전합니다.
예를 들어 haproxy -B 명령으로 해시를 생성할 수 있습니다
echo "mysecret" | mkpasswd --method=bcrypt

 

3. 프론트엔드(frontend) 정의

HTTP 요청을 받아서 어떤 백엔드로 보낼지 결정합니다.

#---------------------------------------------------------------------
frontend www-lb
    bind *:80
    mode http
    log global
    option httplog

    # X-Forwarded-Proto 헤더 설정
    http-request set-header X-Forwarded-Proto http

    # Elasticsearch 요청 라우팅
    acl acl_elasticsearch path_beg -i /es
    use_backend elasticsearch9200 if acl_elasticsearch

    # 그 외 요청은 Kibana로 전달
    default_backend kibana5601

4. 백엔드(backend)에 인증 적용

Kibana 백엔드에 접근 시 정의된 사용자만 접근할 수 있도록 Basic Auth 적용합니다.

#---------------------------------------------------------------------
backend kibana5601
    # Basic Auth ACL 적용
    acl acl_kibana5601 http_auth(basic-auth)
    http-request auth if !acl_kibana5601

    mode http
    balance roundrobin
    cookie SERVERID insert indirect nocache

    # 상태 체크 (Kibana 기본 리디렉션 코드)
    option httpchk GET /
    http-check expect status 302

    # 서버 풀
    server datanode01 192.168.0.101:5601 check cookie s1
    server datanode03 192.168.0.102:5601 check cookie s2

5. 설정 적용 및 확인

브라우저에서 HAProxy의 프록시 주소로 접근하면 아래처럼 ID/PASSWORD 입력 팝업이 나타납니다.

http://<haproxy_ip/

HAProxy

  • 사용자명 : elastic
  • 비밀번호 : elastic

haproxy.cfg 설정

더보기

---

vim /etc/haproxy/haproxy.cfg
...
#---------------------------------------------------------------------
userlist basic-auth
  #user elastic password ZWxhc3RpYzplbGFzdGlj
  user elastic insecure-password elastic
  user guest insecure-password guest


#---------------------------------------------------------------------
frontend www-lb
    bind *:80
    mode http
    http-request set-header X-Forwarded-Proto http
    log global
    option httplog
    #option dontlog-normal
    #option dontlognull
    acl acl_elasticsearc path_beg -i /es
    use_backend elasticsearch9200 if acl_elasticsearc
    default_backend kibana5601


#---------------------------------------------------------------------
backend kibana5601
    acl acl_kibana5601 http_auth(basic-auth)
    http-request auth if !acl_kibana5601
    mode http
    balance roundrobin
    cookie SERVERID insert indirect nocache
    option httpchk GET /
    http-check expect status 302
    #option httpchk GET "/app/kibana#?_g=()"
    #http-check expect string "Kibana"
    #default-server inter 10s downinter 5s rise 2 fall 2 slowstart 60s maxconn 250 maxqueue 256 weight 100
    #server datanode01 192.168.0.101:5601 check inter 3000 rise 2 fall 5
    server datanode01 192.168.0.101:5601 check cookie s1
    server datanode03 192.168.0.102:5601 check cookie s2
...

---

HAProxy에서도 별도 인증 서버 없이 단일 설정만으로 Basic Auth를 구현할 수 있습니다.