Configure HTTPS / HTTP2 on Nginx

NGINX 에서 HTTPS / HTTP2 설정 가이드

HTTP (HTTP/1.1) 은 기본적으로 1번의 연결에 1개의 리소스를 요청하기 때문에 동시 요청이 어렵고 느립니다. 이런 단점을 줄이기 위해 구글에서 SPDY 프로토콜을 개발하게 되었고 이 후에 HTTP2 표준에 SPDY 모델을 적용하게 되었습니다.

HTTP 에 대한 이야기는 방대하며 저도 다 알지는 못하므로 이 가이드에서는 다루지 못합니다. 광고는 아니지만 깊이 알고 싶으신 분은 HTTP 완벽 가이드 라는 책을 권장 드립니다.

Nginx 설치 가이드는 Nginx 설치 포스팅을 참고 해주기 바랍니다. HTTPS / HTTP2 를 적용하기 위해서는 SSL 인증서가 필요합니다. 개인들의 경우 무료로 사용할 수 있는 Let’s Encrypt 인증서를 많이 사용합니다. 발급 방법은 Let’s Encrypt 인증서 발급 포스팅을 참고 부탁 드립니다.

Nginx 설정

간단하게 사이트 설정 파일을 수정하는 것만으로 끝입니다. 강조 표시된 라인들이 설정의 핵심입니다.

site config 설정 편집
: vi /data/apps/ln/nginx/conf/sites-enabled/umount.net.conf

// 예제 샘플이므로 자신의 설정에 맞게 수정

# http로 접속시 https로 리디렉션
server {
    listen      80;
    server_name your_site_name;
    return 301 https://$server_name$request_uri;
}

server {
    listen      443 ssl http2; # https 및 http2 설정
    server_name your_site_name;

    add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";

    ssl_certificate           /path/to/your_certificate;
    ssl_certificate_key       /path/to/oyur_certificate_key;
    ssl_session_timeout       1d;
    ssl_session_cache         shared:SSL:10m;
    ssl_protocols             TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
    #ssl_ciphers               TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-256-GCM-SHA384:TLS13-AES-128-GCM-SHA256:EECDH+AESGCM:EDH+AESGCM; # only tls13
    ssl_ciphers               ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS;
    ssl_ecdh_curve            X25519:sect571r1:secp521r1:secp384r1;
    ssl_prefer_server_ciphers on;
    ssl_stapling              on;
    ssl_stapling_verify       on;
    ssl_trusted_certificate   /path/to/trusted_certificate_key;
    resolver                  8.8.8.8 8.8.4.4;

    charset utf-8;

    access_log logs/your_site_name_access.log main;
    error_log  logs/your_site_name_error.log  crit;

    root   /data/www/your_site_name;
    index  index.php;

    client_max_body_size 1024M;

    location = /favicon.ico {
        log_not_found off;
        access_log    off;
    }

    location = /robots.txt {
        allow         all;
        log_not_found off;
        access_log    off;
    }

    location / {
        try_files $uri $uri/ /index.php?$args;
        index index.php;
    }

    location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        if (!-f $document_root$fastcgi_script_name) {
            return 404;
        }

        fastcgi_param HTTP_PROXY "";

        fastcgi_pass                 unix:/path/to/php-fpm.sock;
        fastcgi_index                index.php;
        fastcgi_buffers              256 16k;
        fastcgi_buffer_size          128k;
        fastcgi_connect_timeout      180s;
        fastcgi_send_timeout         180s;
        fastcgi_read_timeout         180s;
        fastcgi_busy_buffers_size    256k;
        fastcgi_temp_file_write_size 256k;
        fastcgi_max_temp_file_size   0;
        fastcgi_intercept_errors     on;

        include       fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location ~* ^.+\.(css|js)$ {
        rewrite ^(.+)\.(\d+)\.(css|js)$ $1.$3 last;
        expires max;
        access_log off;
        log_not_found off;
        add_header Pragma public;
        add_header Cache-Control "max-age=31536000, public";
    }

    location ~* \.(asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|eot|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|odb|odc|odf|odg|odp|ods|odt|ogg|ogv|otf|pdf|png|pot|pps|ppt|pptx|ra|ram|svg|svgz|swf|tar|t?gz|tif|tiff|ttf|wav|webm|wma|woff|wri|xla|xls|xlsx|xlt|xlw|zip)$ {
        expires max;
        access_log off;
        log_not_found off;
        add_header Pragma public;
        add_header Cache-Control "max-age=31536000, public";
    }
}

nginx 재시작

[root@172-16-11-5 /]# systemctl restart nginx

브라우저에서 확인

HTTPS / HTTP2 확인
ssl2

You may also like...

Subscribe
Notify of
guest

이 사이트는 스팸을 줄이는 아키스밋을 사용합니다. 댓글이 어떻게 처리되는지 알아보십시오.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x