Product/Nginx

[Nginx] SSL ์ ์šฉ (With Chain ์ธ์ฆ์„œ)

Codit Develop 2023. 2. 9. 14:12
๋ฐ˜์‘ํ˜•

 

๐Ÿ”‹ ๋„๋ฉ”์ธ SSL ์ธ์ฆ์„œ ์ ์šฉ

์ด๋ฒˆ์— ํšŒ์‚ฌ ๋„๋ฉ”์ธ์˜ SSL ์ธ์ฆ์„œ๊ฐ€ ์ƒˆ๋กœ ๋ฐœ๊ธ‰๋˜์–ด ์ ์šฉํ•˜๋ ค๋Š”๋ฐ
๋‹ค๋ฅธ ๋ฐฉ์‹์˜ SSL์„ ๊ตฌ์ž…ํ•˜์˜€๋Š”์ง€ ์ถ”๊ฐ€๋กœ Chain ์ธ์ฆ์„œ๊ฐ€ ํ•จ๊ป˜ ๋ฐœ๊ธ‰๋˜์—ˆ๋‹ค.

Nginx์— ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ฐพ์œผ๋ฉฐ ์ˆ˜์ง‘ํ•œ ์ •๋ณด๋ฅผ ๊ธฐ๋กํ•˜๋Š” ๊ฒธ ๊ธฐ์กด์— SSL ์ ์šฉ๋ฒ•๊นŒ์ง€ ์ •๋ฆฌํ•ด๋ณด๋ ค ํ•œ๋‹ค.

๐Ÿ“œ Nginx SSL ์„ค์ •

๊ธฐ์กด์— ์ด์šฉํ•˜๋˜ ์—…์ฒด์—์„œ ์ธ์ฆ์„œ๋ฅผ ๋ฐœ๊ธ‰๋ฐ›์„ ๊ฒฝ์šฐ, ๋ณดํ†ต crtํŒŒ์ผ์„ ์ „๋‹ฌ๋ฐ›๊ฒŒ ๋œ๋‹ค.
์šฐ๋ฆฌ๋Š” ์ด ํŒŒ์ผ์„ pem ํŒŒ์ผ๋กœ ๋ณ€ํ™˜ํ•ด์•ผ ํ•œ๋‹ค.

(๊ฐ€๋น„์•„๋Š” nginx ์ „์šฉ pem ํŒŒ์ผ์„ ์ „๋‹ฌํ•ด์ค€๊ฑฐ๊ฐ™๊ธฐ๋„ ํ•˜๊ณ ..)

๐Ÿšฅ Chain ์ธ์ฆ์„œ๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ

Chain ์ธ์ฆ์„œ?

Chain ์ธ์ฆ์„œ๋Š” Root ์ธ์ฆ์„œ์™€ ๋„๋ฉ”์ธ ์ธ์ฆ์„œ ๊ฐ„ ์—ฐ๊ฒฐ์„ ์ธ์ฆํ•ด์ฃผ๋Š” ์ธ์ฆ์„œ๋ผ๊ณ  ํ•œ๋‹ค.

์ธ์ฆ์„œ๋ฅผ ์ „๋‹ฌ๋ฐ›์œผ๋ฉด ์•„๋งˆ Root ์ธ์ฆ์„œ, Chain ์ธ์ฆ์„œ, Domain ์ธ์ฆ์„œ, Key ํŒŒ์ผ ์ด 4๊ฐœ๋ฅผ ์ „๋‹ฌ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

์ „๋‹ฌ๋ฐ›์€ ํŒŒ์ผ๋“ค์„ ํ•˜๋‚˜์˜ crt ์ธ์ฆ์„œ ํŒŒ์ผ๋กœ ํ•ฉ์ณ์•ผ ํ•œ๋‹ค.

 # Linux
 cat Domain์ธ์ฆ์„œ.crt Chain์ธ์ฆ์„œ.crt Root์ธ์ฆ์„œ.crt > bundle.crt

 # Windows๋Š” ๋ฉ”๋ชจ์žฅ์œผ๋กœ ๊ฐ ํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ํ•˜๋‚˜๋กœ ํ•ฉ์น˜๋ฉด ๋œ๋‹ค.

ํ•ฉ์น˜๊ณ  ๋‚œ ์ดํ›„, ์•„๋ž˜์™€ ๊ฐ™์ด ์ธ์ฆ์„œ ๊ตฌ๋ถ„(๊ฐœํ–‰)์ด ์ œ๋Œ€๋กœ ์ ์šฉ๋˜์–ด ์žˆ๋Š”์ง€ ํ™•์ธํ•ด์•ผ ํ•œ๋‹ค.

 # ์ž˜๋ชป๋œ ๊ฒฝ์šฐ
 -----END CERTIFICATE----------BEGIN CERTIFICATE-----

 # ์˜ฌ๋ฐ”๋ฅธ ๊ฒฝ์šฐ 
 -----END CERTIFICATE-----
 -----BEGIN CERTIFICATE-----

๐Ÿšฅ CRT ํŒŒ์ผ์„ PEM์œผ๋กœ ๋ณ€ํ™˜

Root , Chain, Domain ์ธ์ฆ์„œ ๋ณ‘ํ•ฉ๋ณธ์„ nginx์— ์ ์šฉํ•  ๊ฒฝ์šฐ ์ œ๋Œ€๋กœ ์ ์šฉ๋˜์ง€ ์•Š๋Š” ํ˜„์ƒ์ด ์žˆ๋‹ค.

์•„๋ž˜์—์„œ๋Š” ๋ฐฉ๋ฒ•๋งŒ ์„ค๋ช…ํ•˜๊ณ , ์‹ค์ œ conf์—๋Š” crt๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ ์šฉํ•œ๋‹ค.

์šฐ์„  ํŒŒ์ผ์„ ๋ณ€ํ™˜ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” openssl์„ ์ด์šฉํ•œ๋‹ค.
(์„ค์น˜ ๋ฐฉ๋ฒ•์€ ๋”ฐ๋กœ ์ •๋ฆฌํ•˜๊ฒ ๋‹ค.)

์ „๋‹ฌ๋ฐ›์€ crtํŒŒ์ผ (๋˜๋Š” ๋ณ€ํ™˜ํ•œ crtํŒŒ์ผ)์„ ์•„๋ž˜ ๋ช…๋ น์–ด๋กœ pem์œผ๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ๋‹ค.

# ์ „๋‹ฌ๋ฐ›์€ crt (ํ˜น์€ ๋ณ€ํ™˜ํ•œ crt) : bundle.crt
openssl x509 -inform PEM -in bundle.crt > bundle.pem

key ํŒŒ์ผ์€ ์ ์šฉํ•ด๋ณธ ๊ฒฐ๊ณผ pem์œผ๋กœ ๋ณ€ํ™˜ํ•˜์ง€ ์•Š์•„๋„ ํฐ ๋ฌธ์ œ๋Š” ์—†์—ˆ์ง€๋งŒ,
ํ˜น์‹œ ๋ชจ๋ฅด๋‹ˆ ์ผ๋‹จ ๊ธฐ๋กํ•ด๋ณธ๋‹ค.

openssl rsa -in bundle.key -text > bundle.key.pem

๐Ÿšฅ Nginx SSL ์„ค์ •

์ด์ œ nginx์„ค์ •๋งŒ ๋ฐ”๊พธ๋ฉด ๋๋‚œ๋‹ค.

SSL ์„ค์ •์€ ๊ฐ ๋„๋ฉ”์ธ์˜ server ์„นํ„ฐ์—์„œ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

conf.d ๋ถ„๋ฆฌ๋ฅผ ํ•ด๋†“์ง€ ์•Š์•˜๋‹ค๋ฉด nginx.conf์—์„œ, ๋ถ„๋ฆฌ๋ฅผ ํ•ด๋†“์•˜๋‹ค๋ฉด ํ•ด๋‹น ํŒŒ์ผ์—์„œ ์ˆ˜์ •ํ•˜๋ฉด ๋œ๋‹ค.

๊ธฐ์กด ์„ค์ •์€ ์•„๋ž˜์™€ ๊ฐ™์ด server_name์— ํ•ด๋‹น ๋„๋ฉ”์ธ์ด ๋“ค์–ด๊ฐ„ ์ƒํƒœ๋กœ ์ ์šฉ๋˜์–ด ์žˆ์„ ๊ฒƒ์ด๋‹ค.

server {
    listen 80;
    listen [::]:80;
    server_name sample.co.kr;

    location / {

    }
}

ํ•ด๋‹น ์„ค์ • ํŒŒ์ผ์— SSL์„ ์ ์šฉ์‹œ์ผœ๋ณด์ž.

server {
    listen 443;
    listen [::]:443;
    server_name sample.co.kr;

    location / {

    }

    ssl on;
    # ssl_certificate [์ธ์ฆ์„œ ์ „์ฒด ๊ฒฝ๋กœ]
    # ssl_certificate /var/cert/bundle.pem;
    ssl_certificate /var/cert/bundle.crt;

    # ssl_certificate_key [Key ํŒŒ์ผ ์œ„์น˜]
    ssl_certificate_key /var/cert/bundle.key.pem;


    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';

}

# ์ผ๋ฐ˜ HTTP๋กœ ์ ‘์† ์‹œ HTTPS๋กœ Redirect ์„ค์ •
server {
    if ($host = sample.co.kr) {
        return 301 https://$host$request_uri;
    }

    listen 80;
    listen [::]:80;
    server_name sample.co.kr;

    return 404;
}

ssl_certificate๋กœ ์ธ์ฆ์„œ ํŒŒ์ผ์„ ์„ค์ •ํ•˜๊ณ , ssl_certificate_key๋กœ ์ธ์ฆ์„œ ๋น„๋ฐ€ํ‚ค๋ฅผ ์„ค์ •, ๊ธฐํƒ€ ์„ค์ •์„ ๋งˆ๋ฌด๋ฆฌํ•˜๋ฉด SSL ์ ์šฉ์ด ์™„๋ฃŒ๋œ๋‹ค.

๐Ÿ“ ์ ์šฉ ํ…Œ์ŠคํŠธ

ํ…Œ์ŠคํŠธ๋Š” SSL ํ…Œ์ŠคํŠธ ์‚ฌ์ดํŠธ ์—์„œ ๋ณธ์ธ์˜ ๋„๋ฉ”์ธ ์ฃผ์†Œ๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ํ…Œ์ŠคํŠธํ•  ์ˆ˜ ์žˆ๋‹ค.

ํ…Œ์ŠคํŠธ ์‹œ, ์•„๋ž˜์™€ ๊ฐ™์ด Chain์ด ๋ถˆ์•ˆ์ •ํ•˜๋‹ค๋Š” ๋ฉ”์„ธ์ง€๊ฐ€ ํ‘œ์‹œ๋  ์ˆ˜ ์žˆ๋‹ค.

)

ํ•ด๋‹น ๋ฉ”์„ธ์ง€๊ฐ€ ํ‘œ์‹œ๋  ๊ฒฝ์šฐ, ์•ž์„œ ๋ฉ”๋ชจํ•œ ๋Œ€๋กœ pem ๋Œ€์‹  ๋ณ€ํ™˜์„ ๊ฑฐ์น˜์ง€ ์•Š์€ crt ํŒŒ์ผ (Root + Chain + Domain crt)์„ ์ ์šฉ์‹œํ‚ฌ ๊ฒฝ์šฐ ํ•ด๊ฒฐ๋ฌ๋‹ค.

๐Ÿ”Ž ๋งˆ๋ฌด๋ฆฌ

์œ„ ์ฝ”๋“œ๋Š” ์ด์ „์— ์ž‘์—…ํ•œ ์ฝ”๋“œ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์ž‘์„ฑ๋œ ๊ฒƒ์ด๋‹ค.
๋ถ„๋ช… ๊ทธ๋•Œ ssl_ciphers๋Š” ๋‹ค๋ฅธ ์˜ค๋ฅ˜๊ฐ€ ์žˆ์–ด ์ ์šฉํ•œ๊ฒƒ์ธ๋ฐ..

์™œ ์ ์šฉํ–ˆ๋Š”์ง€ ๊ธฐ์–ต์ด ์•ˆ๋‚œ๋‹ค ใ…Ž


ssl ์ƒ์„ธ ์„ค์ •์— ๋Œ€ํ•œ ์„ค๋ช…์€ ๋‚˜์ค‘์—~

๋ฐ˜์‘ํ˜•