Spring Boot + Nginx

Spring Boot & Nginx 연동

JIM KIM
jimmyberg.kim
13 min readOct 7, 2020

--

참고. swchoi.log 블로그 [Nginx 무중단 배포]

Nginx

Nginx

Nginx(엔진엑스)동시 접속 처리에 특화된 웹 서버 프로그램

  • Apache 보다는 단순하면서, 전달자 역할만 수행하기 때문에 동시 접속 처리에 용이
  • 비동기 Event-Driven 기반 구조
  • Nginx 의 역할로서,
  • 정적 파일 처리 HTTP 서버 역할
  • Reverse Proxy 서버 역할

Revser Proxy 서버?

Client 가 Server 로 요청한 경우, Proxy 서버 (NGINX) 가 Reverse 서버 (Application) 로부터 데이터를 가져오는 역할을 한다.

Event-Driven 구조?

여러 Connection 을 Event Handler 를 통해 비동기 방식으로 처리

Event-Driven

Nginx 무중단 배포

구성

  • Nginx 1대
  • HTTP : 80 port
  • HTTPS : 443 port
  • Spring Boot Application 2대
  • Spring Boot 1 : 8081 port
  • Spring Boot 2 : 8082 port

1. Nginx 설치 및 Spring Boot 연동

EC2 에 Nginx 설치

보안 그룹 추가

  • 인바운드 규칙 > HTTP port 80 지정 안되어 있는 경우 지정
  • EC2 domain 주소 (port 제외) 접속 확인

Spring Boot + Nginx

/etc/nginx/nginx.conf 수정

Nginx 재시작

Spring Boot 연동 확인

  • 프로젝트 Domain (port 제외) 접속 확인

2. Profile API 추가

  • 실행중인 profile 조회하기 위한 API

SecurityConfiguration.java 수정

  • /profile API security permission 승인 처리

3. Profile 파일 추가 생성

  • real1, real2 각각 profile yml 생성

4. Nginx 설정 수정

/etc/nginx/conf.d/service-url.inc 생성

/etc/nginx/nginx.conf 수정

Nginx 재시작

5. Deploy Script 추가

추가할 Scrpit 목록

| script | 설정 | | — — | : — -: | | profile.sh | 현재 profile 버전, port 확인 | | stop.sh | 실행 중이던 Spring Boot 종료 | | start.sh | 배포할 Spring Boot 종료 후 profile.sh 실행 | | health.sh | start.sh 로 실행된 프로젝트 정상 동작 확인 | | switch.sh | Nginx 의 Spring Boot 를 최신 profile 버전으로 변경 |

appspec.yml 수정

profile.sh

stop.sh

start.sh

health.sh

switch.sh

무중단 배포 확인

build.gradle 수정

Log 확인

--

--