Spring Boot + Nginx와 SSL 인증서
포스트
취소

Spring Boot + Nginx와 SSL 인증서

이번에 우아한테크코스 프로젝트를 진행하면서 받은 요구 사항 중 하나는 “HTTPS 적용하기” 였다.

스프링 톰캣 서버에 HTTPS를 바로 적용할 수도 있지만, 여러 가지 이유로 nginx를 리버스 프록시로써 이용하게 되었는데

    * nginx + certbot을 이용하면 매우 간단하게 Let's Encrypt에서 인증서 발급, 자동갱신까지 된다. * 80 / 443번 포트는 root만이 열 수 있는데, Jenkins로 배포 자동화 시 일반 사용자 계정을 사용하기 때문에 이를 직접 사용할 수 없다. 사용자가 :8080 등의 포트 번호를 입력해서 접속해야 하는 문제점이 있다. 이를 nginx를 리버스 프록시로 사용하여 해결할 수 있다.

이번에 프로덕션 서버 외의 개발 서버를 하나 추가하면서 다시 설정하면서 기록해 본다.

`$ sudo apt install nginx
// nginx를 설치한다.

$ sudo apt-get install software-properties-common
$ sudo add-apt-repository universe
$ sudo apt-get update
$ sudo apt-get install certbot python3-certbot-nginx
// certbot을 설치한다.`
`$ sudo vi /etc/nginx/sites-available/default
// nginx의 기본 사이트 설정 기준. 사이트 여러 개를 사용하고 있다면 원하는 파일을 수정한다.
`

server_name 뒤에 자신의 서버 주소를 적어 넣는다.

location / 괄호 안의 내용을 지우고 다음 내용으로 채워 넣는다.

`                proxy_pass http://localhost:(Spring 포트 번호);
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;`
`$ sudo service nginx restart
// nginx를 재시작하고 80포트로 접속했을 때 스프링 애플리케이션이 잘 뜨는지 확인한다.`

그런 다음 인증서를 발급받는 명령어는 다음과 같다.

`$ sudo certbot --nginx -d (사이트 주소)`

착착 진행되다가 중간에 다음과 같은 질문이 뜨는데, http로 접속시 https로 자동 리다이렉트할 것인지 묻는 것이다. 취향에 따라 선택한다.

`Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
-------------------------------------------------------------------------------
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
-------------------------------------------------------------------------------
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): `

작업이 완료되면 웹 브라우저상에서 주소창 왼쪽에 자물쇠 아이콘이 떠 있는 것을 바로 확인할 수 있다.

This post is licensed under CC BY 4.0 by the author.

Jenkins로 Spring Boot 서버 자동 배포하기 – 3. 빌드한 서버 배포하기

우아한테크코스 - 끝나지 않은 모험

Comments powered by Disqus.