nginx ssl 적용하기

nginx ssl

이전 포스트 에서 nginx 를 사용하여 back-end server를 load balancing 하는 법을 알아보았다.
이번 포스트에서는 nginx 에 ssl을 적용하여 https를 사용해보자.


ssl 생성

필자는 발급기관에서 발급한 인증서를 사용하지 않고 openssl로 개인이 생성한 인증서를 기반으로 nginx에 ssl을 적용할것이므로 인증서를 생성하자.

1
$ openssl req -new -newkey rsa:2048 -nodes -keyout <개인키이름>.key -out <인증요청서이름>.csr

필자는 다음과같이 진행하였다.

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
root@16626249b1d1:/etc/nginx/ssl# openssl req -new -newkey rsa:2048 -nodes -keyout jaehunpark-ssl.key -out jaehunpark-ssl.csr
Generating a 2048 bit RSA private key
.....................+++
.....................................................................+++
writing new private key to 'jaehunpark-ssl.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:KR
State or Province Name (full name) [Some-State]:Seoul
Locality Name (eg, city) []:Seoul
Organization Name (eg, company) [Internet Widgits Pty Ltd]:jaehunpark
Organizational Unit Name (eg, section) []:jaehunpark
Common Name (e.g. server FQDN or YOUR name) []:jaehunpark
Email Address []:setyourmindpark@gmail.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:0000
An optional company name []:
root@16626249b1d1:/etc/nginx/ssl# ls
jaehunpark-ssl.csr jaehunpark-ssl.key

입력을 마치게되면 <개인키이름>.key 와 <인증요청서이름>.csr 파일이 생성된다( 개인키, 인증요청서 )

이제 생성된 개인키와 인증요청서로 인증서를 만들어보자.

1
$ openssl x509 -req -days 365 -in <인증요청서이름>.csr -signkey <개인키이름>.key -out <생성할인증서이름>.crt

1
2
3
4
5
6
root@16626249b1d1:/etc/nginx/ssl# openssl x509 -req -days 365 -in jaehunpark-ssl.csr -signkey jaehunpark-ssl.key -out jaehunpark-ssl.crt
Signature ok
subject=/C=KR/ST=Seoul/L=Seoul/O=jaehunpark/OU=jaehunpark/CN=jaehunpark/emailAddress=setyourmindpark@gmail.com
Getting Private key
root@16626249b1d1:/etc/nginx/ssl# ls
jaehunpark-ssl.crt jaehunpark-ssl.csr jaehunpark-ssl.key

해당 명령어를 수행하게되면 인증서( 인증서이름.crt )가 생성된다.


개인키에 걸린 비밀번호제거

nginx에서 ssl 적용시 <개인키이름>.key와 <인증서이름>.crt 파일로 nginx가 ssl을 적용한다.
하지만 개인키에 비밀번호가 걸려있을경우 nginx 재기동시 비밀번호를 요구하므로 비밀번호를 요구하지않도록 사전에 작업을 진행할수있다.

1
2
$ cp <생성된개인키이름>.key <생성할개인키복사본이름>.key.secure
$ openssl rsa -in <생성된개인키복사본이름>.key.secure -out <재생성할개인키이름>.key

1
2
3
4
5
root@16626249b1d1:/etc/nginx/ssl# cp jaehunpark-ssl.key jaehunpark-ssl.key.secure
root@16626249b1d1:/etc/nginx/ssl# openssl rsa -in jaehunpark-ssl.key.secure -out jaehunpark-ssl.key
writing RSA key
root@16626249b1d1:/etc/nginx/ssl# ls
jaehunpark-ssl.crt jaehunpark-ssl.csr jaehunpark-ssl.key jaehunpark-ssl.key.secure

필자는 재생성할개인키이름을 기존의 생성된 개인키명과 동일하게주어 기존의 개인키이름을 덮어씌우게 하였다. (새로운 개인키이름으로만들면 파일이 하나더 늘어나기에..)
필자같은경우는 최초에 생성한 개인키에 비밀번호가 걸려있지않으므로 간단하게 비밀번호를 없앤 개인키를 다시만들었지만 ( 사실 위의 명령어를 수행할필요가없었다 원래 비밀번호가 걸려있지않았으므로 )
실제 비밀번호가 걸려있는 개인키로 작업시 [ writing RSA key ] 문구가 뜨기전에 비밀번호를 요구할것이다.
이제 필요한 준비는 모두끝이났다.


ssl 적용

1
$ vi /etc/nginx/conf.d/<서비스명>.conf

필자는 생성된 인증서를 기반으로 다음과같이 설정하였다.

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
# Load Balancing
upstream target-server {
least_conn;
server 10.10.200.3:4000 max_fails=3 fail_timeout=10s;
server 10.10.200.4:4000 max_fails=3 fail_timeout=10s;
}
server {
listen 443;
server_name 10.10.200.2;
charset utf-8;
access_log /etc/nginx/log/access.log;
error_log /etc/nginx/log/error.log;
ssl on; #ssl사용
ssl_certificate /etc/nginx/ssl/jaehunpark-ssl.crt; #생성된 인증서경로
ssl_certificate_key /etc/nginx/ssl/jaehunpark-ssl.key; #생성된 개인키
location / {
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_pass http://target-server;
}
}

1
$ service nginx restart


참고

필자는 nginx 최신버전 1.12.0 를 사용하고있다.
nginx 에서 ssl 을 정상적으로 사용하기 위해서는 http_ssl_module 모듈이 설치되어 있어야한다.
포함된 모듈 확인은

1
$ nginx -V

명령어로 설치된 모듈들을 확인할수있다.


ssl이 잘 적용되었는지 확인하기위해 https://<주소>:<포트> 로 접속해보자.
nginx-1_2
정상적으로 잘 동작하는것을 확인할수있다.


참고사이트

인증서 개념
nginx ssl 적용
ssl 생성
nginx 추가모듈 설치