본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

리버스 프록시 - HAProxy

원문 보기 위키 갱신

Jenkins를 사용자 친화적인 URL, 다른 공용 포트, 또는 SSL 종료와 함께 제공하고 싶다면 Jenkins(또는 Jenkins가 실행되는 서블릿 컨테이너)를 HAProxy 뒤에 두는 게 유용해요. HAProxy는 일반 HTTP부터 SSL, Unix 도메인 소켓까지 다양한 방식으로 Jenkins 앞에서 프록시 역할을 할 수 있습니다. 이 문서에서 대표적인 구성을 하나씩 살펴볼게요.

출처: Jenkins 문서

본문

사용자 친화적인 URL, 다른 공용 포트를 원하거나, SSL 연결을 Jenkins에 도달하기 전에 종료하고 싶은 상황에서는 Jenkins(또는 Jenkins가 실행되는 서블릿 컨테이너)를 HAProxy 뒤에서 실행하는 것이 유용할 수 있습니다. 이 섹션에서는 이를 위한 몇 가지 방법을 설명합니다.

이 6분 분량의 비디오 튜토리얼(Darin Pope 제작)은 HAProxy 리버스 프록시를 구성합니다.

HAProxy 리버스 프록시 구성

일반 HTTP (Plain HTTP)

HAProxy 2.6.7을 사용해 일반 HTTP로 프록시하는 HAProxy.cfg 예시입니다.

# If you already have an haproxy.cfg file, you can probably leave the
# global and defaults section as-is, but you might need to increase the
# timeouts so that long-running CLI commands will work.
global
    maxconn 4096
    log stdout local0 debug

defaults
   log global
   option httplog
   option dontlognull
   option forwardfor
   maxconn 20
   timeout connect 5s
   timeout client 60s
   timeout server 60s

frontend http-in
  log stdout format raw local0 debug #for additional logging
  bind *:80
  mode http
  acl prefixed-with-jenkins  path_beg /jenkins
  # Use http-request redirect prefix to add /jenkins prefix to URL's location
  # to ensure jenkins base url (context path) is working properly.
  http-request redirect code 301 prefix /jenkins unless prefixed-with-jenkins
  use_backend jenkins if prefixed-with-jenkins

backend jenkins
  log stdout format raw local0 debug
  mode http
  server jenkins1 127.0.0.1:8080 check
  http-request replace-path /jenkins(/)?(.*) /\\2
  http-request set-header X-Forwarded-Port %[dst_port]
  http-request add-header X-Forwarded-Proto https if { ssl_fc }
  http-request set-header X-Forwarded-Host %[req.hdr(Host)]

이 구성은 Jenkins가 로컬의 8080 포트에서 실행 중이라고 가정합니다.

이 구성은 HAProxy가 노출하는 사이트와 Jenkins 자체 모두에 /jenkins 컨텍스트 경로를 사용한다고 가정합니다. 그렇지 않다면 구성을 조정해야 합니다. 자세한 내용은 트래픽 라우팅에 대한 HAProxy 문서를 참조하세요.

Jenkins가 HAProxy 뒤에서 실행되는데 Jenkins에서 긴 CLI 명령을 실행하려 할 때 다음 오류가 발생한다면, 아마도 HAProxy가 CLI 연결을 타임아웃시켰기 때문일 것입니다. 명령이 성공적으로 완료되도록 필요에 따라 timeout client 및 timeout server 설정을 늘릴 수 있습니다.

WARNING: null
hudson.cli.DiagnosedStreamCorruptionException
Read back: 0x00 0x00 0x00 0x1e 0x07
           'Started reverse-proxy-test #68'
           0x00 0x00 0x00 0x01 0x07 0x0a
Read ahead:
Diagnosis problem:
    java.io.IOException: Premature EOF
        at sun.net.www.http.ChunkedInputStream.readAheadBlocking(ChunkedInputStream.java:565)
        ...
    at hudson.cli.FlightRecorderInputStream.analyzeCrash(FlightRecorderInputStream.java:82)
    at hudson.cli.PlainCLIProtocol$EitherSide$Reader.run(PlainCLIProtocol.java:153)
Caused by: java.io.IOException: Premature EOF
    at sun.net.www.http.ChunkedInputStream.readAheadBlocking(ChunkedInputStream.java:565)
    ...
    at java.io.DataInputStream.readInt(DataInputStream.java:387)
    at hudson.cli.PlainCLIProtocol$EitherSide$Reader.run(PlainCLIProtocol.java:111)

SSL 사용 (With SSL)

HAProxy 2.6.7을 사용해 SSL로 프록시에 연결하고, SSL 연결을 종료한 뒤 일반 HTTP로 Jenkins와 통신하는 HAProxy.cfg 예시입니다.

# If you already have an haproxy.cfg file, you can probably leave the
# global and defaults section as-is, but you might need to increase the
# timeouts so that long-running CLI commands will work.

global
    maxconn 4096
    log stdout local0 debug

defaults
   log global
   option httplog
   option dontlognull
   option forwardfor
   maxconn 20
   timeout connect 5s
   timeout client 5m
   timeout server 5m

frontend http-in
  log stdout format raw local0 debug
  bind *:80
  bind *:443 ssl crt /usr/local/etc/haproxy/ssl/server.pem
  mode http
  acl prefixed-with-jenkins  path_beg /jenkins
  http-request redirect code 301 prefix /jenkins unless prefixed-with-jenkins
  redirect scheme https if !{ ssl_fc } # Redirect http requests to https
  use_backend jenkins if prefixed-with-jenkins

backend jenkins
  log stdout format raw  local0 debug
  mode http
  server jenkins1 127.0.0.1:8080 check
  http-request replace-path /jenkins(/)?(.*) /\\2
  http-request set-header X-Forwarded-Port %[dst_port]
  http-request add-header X-Forwarded-Proto https if { ssl_fc }
  http-request set-header X-Forwarded-Host %[req.hdr(Host)]

Unix 도메인 소켓 (Unix Domain Sockets)

HAProxy는 Jenkins에 대한 요청을 TCP 포트 대신 Unix 도메인 소켓으로 프록시하도록 구성할 수 있습니다. 이 접근 방식은 Jenkins의 네트워크 노출을 없애 보안을 강화하며, Jenkins와 HAProxy가 같은 호스트에서 실행될 때 특히 유용합니다.

참고: Jenkins의 Unix 도메인 소켓 지원은 systemd 기반 설치(예: Linux 패키지 설치)를 요구합니다. 이 구성은 Jenkins WAR이나 공식 Jenkins Docker 이미지에는 적용되지 않습니다. 그들은 systemd에 의존하지 않기 때문입니다.

Unix 도메인 소켓용 Jenkins 구성

먼저 Jenkins가 TCP 포트 대신 Unix 도메인 소켓에서 수신하도록 구성합니다.

이 섹션은 systemd로 관리되는 Jenkins 설치(예: Linux 패키지 설치)에만 적용됩니다. Jenkins WAR 또는 Docker 기반 설치의 경우 Jenkins 초기 구성 문서를 참조하세요.

Jenkins systemd 서비스를 편집해 TCP를 비활성화하고 Unix 소켓을 활성화합니다.

systemctl edit jenkins

다음 오버라이드를 추가합니다.

[Service]
Environment="JENKINS_PORT=-1"
Environment="JENKINS_UNIX_DOMAIN_PATH=/run/jenkins/jenkins.socket"

Jenkins를 리로드하고 다시 시작합니다.

systemctl daemon-reload
systemctl restart jenkins

소켓이 존재하는지 확인합니다.

ls -l /run/jenkins/jenkins.socket

Unix 도메인 소켓용 HAProxy 구성

HAProxy 2.6.7 이상을 사용해 Unix 도메인 소켓으로 Jenkins에 요청을 프록시하는 HAProxy.cfg 예시입니다.

# If you already have an haproxy.cfg file, you can probably leave the
# global and defaults section as-is, but you might need to increase the
# timeouts so that long-running CLI commands will work.

global
    maxconn 4096
    log stdout local0 debug

defaults
   log global
   option httplog
   option dontlognull
   option forwardfor
   maxconn 20
   timeout connect 5s
   timeout client 5m
   timeout server 5m

frontend http-in
  log stdout format raw local0 debug
  bind *:80
  bind *:443 ssl crt /usr/local/etc/haproxy/ssl/server.pem
  mode http
  acl prefixed-with-jenkins  path_beg /jenkins
  http-request redirect code 301 prefix /jenkins unless prefixed-with-jenkins
  redirect scheme https if !{ ssl_fc } # Redirect http requests to https
  use_backend jenkins if prefixed-with-jenkins

backend jenkins
  log stdout format raw  local0 debug
  mode http
  # Connect to Jenkins via Unix domain socket
  server jenkins1 unix@/run/jenkins/jenkins.socket check
  http-request replace-path /jenkins(/)?(.*) /\\2
  http-request set-header X-Forwarded-Port %[dst_port]
  http-request add-header X-Forwarded-Proto https if { ssl_fc }
  http-request set-header X-Forwarded-Host %[req.hdr(Host)]

TCP 구성과의 핵심 차이는 이제 127.0.0.1:8080 대신 Unix 소켓 경로를 가리키는 server 지시문입니다.

권한 (Permissions)

HAProxy가 Jenkins Unix 소켓에 접근할 수 있는지 확인하세요. 소켓은 jenkins 사용자와 그룹으로 생성됩니다. HAProxy 사용자를 jenkins 그룹에 추가해야 할 수 있습니다.

usermod -aG jenkins haproxy
systemctl restart haproxy

컨텍스트 경로 (Context path)

컨텍스트 경로는 URL 경로의 접두어입니다. Jenkins 컨트롤러와 리버스 프록시는 동일한 컨텍스트 경로를 사용해야 합니다. 예를 들어 Jenkins 컨트롤러 URL이 https://www.example.com/jenkins/ 이라면 --prefix=/jenkins 인자가 Jenkins 컨트롤러 명령줄 인자에 포함되어야 합니다.

Linux 패키지를 사용할 때는 systemctl edit jenkins를 실행하고 다음을 추가해 컨텍스트 경로를 설정합니다.

[Service]
Environment="JENKINS_PREFIX=/jenkins"

Windows 컨트롤러에서는 설치 디렉터리의 jenkins.xml 파일에 --prefix 명령줄 인자를 포함해 컨텍스트 경로를 설정합니다.

리버스 프록시가 Jenkins를 제공하는 컨텍스트 경로에서 Jenkins가 실행 중인지 확인하세요. 이 원칙을 지키면 가장 큰 고통을 피할 수 있습니다.

컨텍스트 경로가 비어 있으면 --prefix 명령줄 인자는 필요하지 않습니다. 예를 들어 https://jenkins.example.com/ URL은 빈 컨텍스트 경로를 가집니다.

더 알아보기 (Learn more)

  • 리버스 프록시 구성의 다른 예(nginx, Apache, IIS 등)는 Jenkins 사용자 핸드북의 Reverse proxy configuration 문서를 참고하세요.
  • 컨텍스트 경로와 Jenkins 설치 구성에 대한 자세한 내용은 Jenkins 사용자 핸드북의 Installing Jenkins 문서에서 확인할 수 있어요.