리눅스에서 SSL 자가 서명 인증서 생성 및 Nginx HTTPS 설정

1. 기존 HTTP 주소: http://172.28.5.4

클라이언트 요청으로 HTTPS로 업그레이드 필요. IP 주소로 접근하므로 자체 인증서 생성 및 Nginx 설정 필요.

2. home 디렉터리에 새로운 cert 폴더 생성

[root@localhost home]# mkdir new_cert

3. OpenSSL을 사용하여 서버와 클라이언트용 키 쌍 생성

1. 서버용 개인키 생성

[root@localhost home]# cd new_cert/
[root@localhost new_cert]# openssl genrsa -out server.key 1024
Generating RSA private key, 1024 bit long modulus
..............................++++++
...........................++++++
e is 65537 (0x10001)
[root@localhost new_cert]# 

2. 서버용 공개키 생성

[root@localhost new_cert]# openssl rsa -in server.key -pubout -out server.pem
writing RSA key
[root@localhost new_cert]# 

3. 클라이언트용 개인키 생성

[root@localhost new_cert]# openssl genrsa -out client.key 1024
Generating RSA private key, 1024 bit long modulus
...............................................++++++
...................++++++
e is 65537 (0x10001)
[root@localhost new_cert]# 

4. 클라이언트용 공개키 생성

[root@localhost new_cert]# openssl rsa  -in client.key -pubout -out client.pem
writing RSA key
[root@localhost new_cert]# 
[root@localhost new_cert]# ll
total 16
-rw-r--r-- 1 root root 887 Jan 11 16:06 client.key
-rw-r--r-- 1 root root 272 Jan 11 16:07 client.pem
-rw-r--r-- 1 root root 887 Jan 11 15:55 server.key
-rw-r--r-- 1 root root 272 Jan 11 16:05 server.pem
[root@localhost new_cert]# 

4. CA 인증서 생성

1. CA 개인키 생성

[root@localhost new_cert]# openssl genrsa -out ca.key 1024
Generating RSA private key, 1024 bit long modulus
.....++++++
.........++++++
e is 65537 (0x10001)
[root@localhost new_cert]# 

2. CA 인증서 요청(CSR) 파일 생성

[root@localhost new_cert]# openssl req -new -key ca.key -out ca.csr
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]:cn
State or Province Name (full name) []:beijing
Locality Name (eg, city) [Default City]:chaoyang
Organization Name (eg, company) [Default Company Ltd]:hl95_ca
Organizational Unit Name (eg, section) []:hl95_sms_ca
Common Name (eg, your name or your server's hostname) []:172.28.5.4
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:172.28.5.4
[root@localhost new_cert]# 

3. CA 키와 CSR을 이용하여 CRT 인증서 서명

[root@localhost new_cert]# openssl x509 -req -in ca.csr -signkey ca.key -out ca.crt
Signature ok
subject=/C=cn/ST=beijing/L=chaoyang/O=honglian95/OU=honglian95_hlsms/CN=test.hl95.com
Getting Private key
[root@localhost new_cert]# 

5. 서버 및 클라이언트 CRT 인증서 생성

1. 서버용 CSR 생성

[root@localhost new_cert]# openssl req -new -key server.key -out server.csr
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]:cn
State or Province Name (full name) []:beijing
Locality Name (eg, city) [Default City]:chaoyang
Organization Name (eg, company) [Default Company Ltd]:hl95_server
Organizational Unit Name (eg, section) []:hl95_sms_server
Common Name (eg, your name or your server's hostname) []:172.28.5.4
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:172.28.5.4
[root@localhost new_cert]# 

2. 클라이언트용 CSR 생성

[root@localhost new_cert]# openssl req -new -key client.key -out client.csr       
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]:cn
State or Province Name (full name) []:beijing
Locality Name (eg, city) [Default City]:chaoyang
Organization Name (eg, company) [Default Company Ltd]:hl95_client
Organizational Unit Name (eg, section) []:hl95_sms_client
Common Name (eg, your name or your server's hostname) []:172.28.5.4
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:172.28.5.4
[root@localhost new_cert]# 

서버와 클라이언트의 Organization Name 및 Organizational Unit Name은 CA와 반드시 다르게 설정해야 합니다

3. 생성한 CA를 사용하여 인증서 발급 (서버 및 클라이언트)

[root@localhost new_cert]# openssl x509 -req -CA ca.crt -CAkey ca.key -CAcreateserial -in server.csr -out server.crt
Signature ok
subject=/C=cn/ST=beijing/L=chaoyang/O=hl95_server/OU=hl95_sms_server/CN=172.28.5.4
Getting CA Private Key
[root@localhost new_cert]# openssl x509 -req -CA ca.crt -CAkey ca.key -CAcreateserial -in client.csr -out client.crt
Signature ok
subject=/C=cn/ST=beijing/L=chaoyang/O=hl95_client/OU=hl95_sms_client/CN=172.28.5.4
 Getting CA Private Key [root@localhost new_cert]# 
[root@localhost new_cert]# ll
total 48
-rw-r--r-- 1 root root 899 Jan 11 17:35 ca.crt
-rw-r--r-- 1 root root 708 Jan 11 17:30 ca.csr
-rw-r--r-- 1 root root 887 Jan 11 17:12 ca.key
-rw-r--r-- 1 root root  17 Jan 11 17:54 ca.srl
-rw-r--r-- 1 root root 895 Jan 11 17:54 client.crt
-rw-r--r-- 1 root root 704 Jan 11 17:50 client.csr
-rw-r--r-- 1 root root 887 Jan 11 16:06 client.key
-rw-r--r-- 1 root root 272 Jan 11 16:07 client.pem
-rw-r--r-- 1 root root 895 Jan 11 17:53 server.crt
-rw-r--r-- 1 root root 704 Jan 11 17:48 server.csr
-rw-r--r-- 1 root root 887 Jan 11 15:55 server.key
-rw-r--r-- 1 root root 272 Jan 11 16:05 server.pem
[root@localhost new_cert]# 

6. 최종적으로 필요한 key와 crt 파일 생성

[root@localhost new_cert]# openssl rsa -in server.key -out server_nginx.key
writing RSA key
[root@localhost new_cert]# openssl x509 -req -days 3650 -in server.csr -signkey server_nginx.key -out server_nginx.crt
Signature ok
subject=/C=cn/ST=beijing/L=chaoyang/O=hl95_server/OU=hl95_sms_server/CN=172.28.5.4
Getting Private key
[root@localhost new_cert]# 

7. Nginx에 key와 crt 파일 업로드하고 설정 파일 수정 (https://xxx.xxx.xxx.xxx:8061)

user  nginx;
worker_processes  8;

error_log  /var/log/nginx/info.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections 1024;
    accept_mutex on;
    multi_accept on;
    use epoll;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    upstream sms-web-wx {
        server 172.28.5.6:8061;
        server 172.28.5.8:8061;
    }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

       
        location / {

            proxy_pass  http://sms-web-wx;
            proxy_set_header host $host;
            proxy_set_header X-real-ip $remote_addr;
            proxy_set_header X-forwarded-for $proxy_add_x_forwarded_for;

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


    }

    server {
        listen       8061 ssl;
        server_name  zx.sms.web;

        ssl_certificate      /home/cert/server_nginx.crt;
        ssl_certificate_key  /home/cert/server_nginx.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers  on;

        location / {

            proxy_pass  http://sms-web-wx;
            proxy_set_header host $host;
            proxy_set_header X-real-ip $remote_addr;
            proxy_set_header X-forwarded-for $proxy_add_x_forwarded_for;

         }
   }
}

8. 브라우저에서 접속

계속 진행 버튼 클릭 후, 인증서 오류 메시지에서 인증서 확인

신뢰할 수 없는 인증서 경고 발생, "인증서 설치" 선택

설치 완료 후 브라우저 재시작

더 이상 인증서 오류 발생하지 않음

인터넷 옵션 → 내용 → 인증서

9. CRT 형식 인증서를 PFX 형식으로 변환 (Tomcat 용)

[root@localhost new_cert]# openssl pkcs12 -export -in server_nginx.crt -inkey server_nginx.key -out client.pfx
Enter Export Password:
Verifying - Enter Export Password:

태그: linux SSL Certificate nginx OpenSSL

10월 26일 21:34에 게시됨