16장 Nginx에서 인증서를 교체한다
Tomcat 앞에 Nginx가 TLS를 종료하는 구조가 흔하다. Nginx 공식 문서는 ssl_certificate에 certificate chain 파일, ssl_certificate_key에 제한된 개인키 파일을 지정한다.
server {
listen 443 ssl;
server_name portal.example.com;
ssl_certificate /etc/letsencrypt/live/portal.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/portal.example.com/privkey.pem;
location /releaseportal/ {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
교체 순서는 다음과 같다.
- 현재 인증서 serial, 만료일, 파일 경로와 Nginx 설정 백업을 기록한다.
- 새 인증서의 SAN·기간·키·체인을 staging에서 검증한다.
- 설정이 새 파일 또는 안정적인
livesymlink를 가리키게 한다. nginx -t로 전체 설정을 검사한다.systemctl reload nginx로 새 worker가 설정을 읽게 한다.- 외부 hostname에서 serial, 체인, health, 주요 페이지를 확인한다.
- 실패하면 이전 파일 참조로 되돌리고 다시
nginx -t와 reload를 한다.
reload는 기존 연결을 즉시 모두 끊는 restart와 목적이 다르지만, 장시간 연결과 플랫폼 구현을 모니터링한다. 설정 테스트가 실패했는데 reload를 강행하지 않는다. Nginx master가 개인키를 읽지 못하면 새 worker가 뜨지 않을 수 있다.