본문 바로가기

리눅스

NGINX에서 가상 호스트에 HTTP2를 설정하는 방법

반응형

NGINX에서 가상 호스트(Virtual Host)에 HTTP/2를 설정하는 방법

NGINX 버전 확인

nginx -v

1. http2 on; 방식

이 지시문은 버전 1.25.1에 나타났습니다.

#default.conf configure
server {
    listen 80 default_server;
    server_name _;
    return 301 https://$host$request_uri;
}

#Settings for a TLS enabled server.
server {
    listen 443 ssl;
    http2 on;
    server_name _;
    root /usr/share/nginx/html;
    index index.html index.htm;
...
}

2. listen 443 ssl http2; 방식

#default.conf configure
server {
    listen 80 default_server;
    server_name _;
    return 301 https://$host$request_uri;
}

#Settings for a TLS enabled server.
server {
    listen 443 ssl http2;
    server_name _;
    root /usr/share/nginx/html;
    index index.html index.htm;
...
}

3. HTTP/2 관련 최적화

vim /etc/nginx/nginx.conf
user www-data www-data;
worker_processes auto;

error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    include /etc/nginx/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 /var/log/nginx/access.log main;

    server_tokens        off;
    sendfile              on;
    tcp_nopush            on;
    tcp_nodelay           on;
    keepalive_timeout     65;
    types_hash_max_size 4096;
    
    # SSL/TLS 최적화
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers on;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_session_cache shared:SSL:10m;

    # HTTP/2 성능 최적화
    http2_max_field_size 16k;
    http2_max_header_size 32k;
    http2_idle_timeout 5m;
    http2_max_requests 1000;
    http2_buffer_size 16k;
    
    # Gzip 압축 활성화
    gzip on;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
    gzip_min_length 256;
    
    include /etc/nginx/conf.d/*.conf;
}
vim /etc/nginx/conf.d/www.example.com
# default.conf configure
server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://$host$request_uri;
    charset utf-8;
}

# Settings for a TLS enabled server.
server {
    listen 443 ssl;
    http2 on; # HTTP/2 활성화
    server_name example.com www.example.com;

    # 웹 루트 디렉토리
    root /var/www/example.com/html;
    index index.html index.htm;

    # 로그 파일
    access_log /var/log/nginx/example.access.log;
    error_log /var/log/nginx/example.error.log;

    # 기본 위치 설정
    location / {
        try_files $uri $uri/ =404;
    }
    
    # SSL 인증서 설정
    ssl_certificate /etc/ssl/certs/example.com.crt;
    ssl_certificate_key /etc/ssl/private/example.com.key;
    
    error_page 404 /404.html;
        location = /40x.html {
    }

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

4. HTTP/2 테스트

curl 명령어

curl -I --http2 -k https://www.scbyun.com
HTTP/2 200 
date: Wed, 08 Jan 2025 07:32:53 GMT
content-type: text/html;charset=UTF-8
content-length: 36855
vary: Accept-Encoding
t_userid: 46ec2313bed99375f4e9566796fae12e62c3ca0a
set-cookie: REACTION_GUEST=5deb76fd231463add499a83242b9fe4229910112
x-content-type-options: nosniff
x-xss-protection: 0
cache-control: no-cache, no-store, max-age=0, must-revalidate
pragma: no-cache
expires: 0
strict-transport-security: max-age=31536000 ; includeSubDomains

온라인 HTTP/2 테스트 도구

tistos.com/ko/tools

 

참고URL

-Nginx Documentation : http2 directive

 

728x90
반응형