Nginx에 SSL 인증서 설정하기


테스트 환경 구성부터 Nginx에 SSL 인증서 적용까지


공인 인증기관 CA가 아닌 테스트용으로 사용할 수 있는 SSL 인증서 생성과 적용을 살펴보겠습니다.
openssl을 통해 SSL 인증서를 직접 생성하는 것이기 때문에 브라우저에서의 통신은 불가능합니다.
대신에 curl 명령어로 검증할 인증서를 전달하여 테스트하도록 하겠습니다.

인증서 적용하기

1. 테스트 환경 구성


도커의 centos7 이미지로 테스트 환경을 구성하겠습니다.
centos7 버전 이미지를 다운 받은 후, 포트를 연결하여 컨테이너를 띄웁니다.

1
2
$ docker pull centos:7
$ docker run -it --name ssl-test -p 8443:443 -p 8080:80 centos:7 /bin/bash

컨테이너에 접근하여 nginx와 openssl을 설치합니다.

openssl 설치

1
$ yum install -y openssl.aarch64

nginx 설치

nginx 레포지토리를 추가해야 합니다.

1
2
3
4
5
6
7
8
$ vi /etc/yum.repos.d/nginx.repo

# 아래 내용 추가 후 저장
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

nginx를 설치 후 실행시킵니다.

1
2
$ yum install -y nginx
$ nginx

127.0.0.1:8080으로 접근 시, nginx 화면이 나타나는 것을 확인할 수 있습니다.

2. 인증서 생성


인증서로는 개인키, 인증요청서, 인증서 순으로 생성한다.

개인키 생성

아래와 같이 RSA 2048 비트의 비밀키를 생성합니다.

1
$ openssl genrsa -des3 -out server.key 2048

RSA 이외에도 ECDSA 등이 있지만 보통 RSA를 사용합니다.

인증 요청서 생성

인증서 요청 파일(CSR)을 아래와 같이 작성합니다.

1
$ openssl req -new -key server.key -out server.csr

인증서 생성

인증서를 자신의 비밀 키로 서명해서 생성합니다.

1
$ openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

인증서 생성까지 마친 후 아래 3개의 파일을 확인 할 수 있습니다.

1
2
3
-rw-r--r--   1 root root  1184 Jul  9 05:41 server.crt
-rw-r--r-- 1 root root 997 Jul 9 05:40 server.csr
-rw-r--r-- 1 root root 1675 Jul 9 05:41 server.key

3. nginx 설정 파일 수정


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#/etc/nginx/conf.d/default.conf

server {
listen 80;
server_name localhost;

rewrite ^(.*) https://localhost$1 permanent;

location / {
root html;
index index.html;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}

server {
listen 443 ssl;
server_name localhost;

#access_log /var/log/nginx/host.access.log main;


ssl_certificate /tmp/server.crt;
ssl_certificate_key /tmp/server.key;
ssl_protocols TLSv1.2 TLSv1.3;

location / {
root /usr/share/nginx/html;
index index.html index.htm;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}

4. 테스트


우선 80 포트로 접근 시, 301로 리다이렉트되는 것을 확인할 수 있습니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ curl -i localhost

HTTP/1.1 301 Moved Permanently
Server: nginx/1.20.1
Date: Fri, 09 Jul 2021 06:44:40 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive
Location: https://localhost:8443/

<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>

그리고 443 포트로 접근 시, nginx html을 확인할 수 있습니다.
cacert 옵션으로 직접 인증서를 건내줍니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
$ curl --cacert server.crt https://localhost:443

HTTP/1.1 200 OK
Server: nginx/1.20.1
Date: Fri, 09 Jul 2021 06:46:23 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 25 May 2021 13:39:39 GMT
Connection: keep-alive
ETag: "60acfe1b-264"
Accept-Ranges: bytes

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

Comments