리버스 프록시 - Nginx
서버에 기존 웹사이트가 있는 경우, 더 큰 웹사이트의 일부에 Jenkins를 바인딩할 수 있도록 Jenkins(또는 Jenkins가 실행되는 서블릿 컨테이너)를 Nginx 뒤에서 실행하는 것이 유용할 수 있어요. 이 문서에서는 Nginx를 리버스 프록시로 구성하는 방법, 컨텍스트 경로 설정, 권한 설정, 장기 CLI 명령 타임아웃 문제 해결을 알려드릴게요.
출처: Jenkins 문서
본문
서버에 기존 웹사이트가 있는 상황에서 Jenkins(또는 Jenkins가 실행되는 서블릿 컨테이너)를 Nginx 뒤에서 실행하는 것이 유용할 수 있습니다. 그렇게 하면 가질 수 있는 더 큰 웹사이트의 일부에 Jenkins를 바인딩할 수 있기 때문이에요. 이 섹션에서는 이를 위한 몇 가지 접근 방식을 논의합니다.
특정 URL에 대한 요청이 도착하면 Nginx가 프록시가 되어 그 요청을 Jenkins로 전달하고, 응답을 다시 클라이언트로 전달합니다.
Darin Pope의 이 9분 동영상 튜토리얼은 Nginx를 리버스 프록시로 구성합니다.
Nginx를 리버스 프록시로 구성하기
아래 Nginx 구성 조각은 Nginx 리버스 프록시 구성의 예를 제공합니다. Jenkins 컨트롤러와 Nginx 리버스 프록시가 같은 컴퓨터에서 실행되고 있다고 가정합니다.
upstream jenkins {
keepalive 32; # keepalive connections
server 127.0.0.1:8080; # jenkins ip and port
}
# Required for Jenkins websocket agents and HTTP keepalive support.
# Setting '' to '' clears the Connection header for non-WebSocket traffic,
# allowing upstream keepalive connections (e.g., keepalive 32) to function.
# References:
# - https://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive
# - https://nginx.org/en/docs/http/websocket.html
map $http_upgrade $connection_upgrade {
default upgrade;
'' '';
}
server {
listen 80; # Listen on port 80 for IPv4 requests
server_name jenkins.example.com; # replace 'jenkins.example.com' with your server domain name
# this is the jenkins web root directory
# (mentioned in the output of "systemctl cat jenkins")
root /var/run/jenkins/war/;
access_log /var/log/nginx/jenkins.access.log;
error_log /var/log/nginx/jenkins.error.log;
# pass through headers from Jenkins that Nginx considers invalid
ignore_invalid_headers off;
location ~ "^\/static\/[0-9a-fA-F]{8}\/(.*)$" {
# rewrite all static files into requests to the root
# E.g /static/12345678/css/something.css will become /css/something.css
rewrite "^\/static\/[0-9a-fA-F]{8}\/(.*)" /$1 last;
}
location /userContent {
# have nginx handle all the static requests to userContent folder
# note : This is the $JENKINS_HOME dir
root /var/lib/jenkins/;
if (!-f $request_filename){
# this file does not exist, might be a directory or a /**view** url
rewrite (.*) /$1 last;
break;
}
sendfile on;
}
location / {
sendfile off;
proxy_pass http://jenkins;
proxy_redirect default;
proxy_http_version 1.1;
# Required for Jenkins websocket agents
proxy_set_header Connection $connection_upgrade;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_max_temp_file_size 0;
#this is the maximum upload size
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_request_buffering off; # Required for HTTP CLI commands
}
}
이것은 Jenkins를 포트 8080에서 실행한다고 가정합니다. /var/log/nginx/jenkins 폴더를 만드는 것을 잊지 마세요.
| | $connection_upgrade map은 WebSocket 요청에 대해 Connection 헤더를 upgrade로 설정하고, 비-WebSocket 트래픽에 대해서는 헤더를 지웁니다(""로 설정). Connection 헤더를 지우는 것은 NGINX upstream keepalive 문서와 NGINX WebSocket proxying 문서에 문서화된 대로 업스트림 HTTP keepalive 연결(예: keepalive 32;)을 보존하는 데 필요합니다. |
proxy_pass 값을 슬래시로 끝내지 않도록 주의하세요. 이 토론에 설명된 것과 같은 문제를 일으킬 수 있습니다.
컨텍스트 경로(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 커맨드 라인 인자는 필요하지 않습니다. 예를 들어 URL https://jenkins.example.com/ 은 빈 컨텍스트 경로를 가집니다.
Blue Ocean에서 일부 경로(예: 폴더)에 문제가 발생한다면 프록시 구성에 다음 스니펫을 추가해야 할 수 있습니다.
if ($request_uri ~* "/blue(/.*)") {
proxy_pass http://YOUR_SERVER_IP:YOUR_JENKINS_PORT/blue$1;
break;
}
권한(Permissions)
Nginx에 Jenkins 웹 루트 폴더를 읽을 권한을 주려면 nginx 사용자를 Jenkins 그룹에 추가하세요.
usermod -aG jenkins nginx
마지막 명령이 nginx 사용자가 시스템에 정의되지 않아 실패했다면 www-data 사용자를 Jenkins 그룹에 추가할 수 있습니다.
usermod -aG jenkins www-data
Jenkins에서 프록시를 통해 장기 CLI 명령을 실행할 때 타임아웃이 발생하면 필요에 따라 proxy_read_timeout 설정을 늘릴 수 있습니다. 오래된 Jenkins 버전은 proxy_read_timeout 설정을 존중하지 않을 수 있어요.
Jenkins가 Nginx 뒤에서 실행되는 동안 Jenkins에서 장기 CLI 명령 실행 시 다음 오류가 발생한다면, 이는 Nginx가 CLI 연결을 타임아웃시키기 때문일 가능성이 큽니다. 필요에 따라 proxy_read_timeout 설정을 늘려 명령이 성공적으로 완료되도록 할 수 있어요.
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)
Index
더 알아보기 (Learn more)
- Reverse proxy configuration
- Reverse proxy - Issues
- Managing systemd services