2015년 1월 6일 화요일

Nginx + SSL 서버 구축하기

ssl 인증서버를 nginx와 연동시키는 방법입니다.
기본적으로 nginx와 ssl module이 설치되어 있어야 합니다.

구축 순서는 SSL 인증키 만들기 -> nginx에 적용시키기 순으로 이어집니다.


SSL 인증서 만들기


1. 개인키(.key)와 인증서 서명 요청 파일을 생성합니다.

# openssl req -new -newkey rsa:2048 -nodes -keyout <파일명>.key -out <파일명>.csr

Generating a 2048 bit RSA private key
...+++
.............................................+++
writing new private key to '<파일명>.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) [XX]:kr
State or Province Name (full name) []:korea
Locality Name (eg, city) [Default City]:seoul
Organization Name (eg, company) [Default Company Ltd]:jun's company
Organizational Unit Name (eg, section) []:infra team
Common Name (eg, your name or your server's hostname) []:jun
Email Address []:<이메일>

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:비밀번호
An optional company name []:비밀번호

주황색으로 된 부분만 알맞게 입력하시면 됩니다.

2. 파일 확인하기

# ls -al

-rw-r--r--  1 root root  1098 Jan  6 20:46 <파일명>.csr
-rw-r--r--  1 root root  1704 Jan  6 20:46 <파일명>.key

3. 자체 서명된 SSL 인증서 생성하기

# openssl x509 -req -days 365 -in <파일명>.csr -signkey <파일명>.key -out <파일명>.crt

4. password phase 삭제하기

# cp <파일명>.key <파일명>.key.secure
# openssl rsa -in <파일명>.key.secure -out <파일명>.key


nginx에 등록하기


1. nginx.conf 편집하기

# vi /usr/local/nginx/conf/nginx.conf 

HTTPS 섹션을 찾아서 변경해주세요.

 # HTTPS server
    #
    server {
        listen       443;
        server_name  localhost;
        
        ssl     on;
        ssl_certificate /root/192.168.137.61.crt;
        ssl_certificate_key     /root/192.168.137.61.key;
        ssl_session_timeout  5m;
        ssl_protocols SSLv2 SSLv3 TLSv1;
        ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
        ssl_prefer_server_ciphers   on;
    
        location / {
            root   <홈페이지 경로 예) /home/jun>;
            index  index.php index.html;  // html을 이용한다면 제일 첫 부분에 html로 변경
        }
    }

2. nginx에 ssl 모듈 있는지 확인하기

# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.7.7
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC)
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module  // ssl_module 유무 확인

3. ssl 모듈 없으면 nginx 컴파일링하기

# ./configure --prefix=/usr/local/nginx --with-http_ssl_module

# make && make install

4. 설정 오류 검사하기

# /etc/init.d/nginx configtest
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

4-1. http로 접속 했을 경우에 https로 리다이렉션하기

# vi /etc/init.d/nginx

server {
        listen       80;
        server_name  192.168.137.61;
        rewrite ^(.*) https://192.168.137.61$1 permanent; // 이 줄을 추가하면 http://192.168.137.61로 접속하면 https로 바뀌어서 접속됩니다.

5. 재시작하고 확인

# /etc/init.d/nginx restart

웹 브라우저에서 http://홈페이지 주소


댓글 없음:

댓글 쓰기