HTTP/2
HTTP/2
Supports multiplexing, header compression, and server push. An improvement on HTTP/1.1.
HTTP/2
Supports multiplexing, header compression, and server push. An improvement on HTTP/1.1.
HTTP/2 is a next-generation web protocol standardized by the IETF in 2015, designed to overcome the performance limitations of HTTP/1.1. It was developed based on Google's SPDY protocol, and by introducing Binary Framing, it enables much more efficient data transmission than the text-based HTTP/1.1. Currently, more than 50% of websites worldwide use HTTP/2.
The core feature of HTTP/2 is Multiplexing. In HTTP/1.1, only one request could be processed per TCP connection, so browsers had to open 6 parallel connections. In contrast, HTTP/2 can process hundreds of requests simultaneously over a single connection, which solves the Head-of-Line Blocking problem and improves page loading speed by 30-50%.
HPACK header compression is another important feature of HTTP/2. In HTTP/1.1, identical headers (Cookie, User-Agent, etc.) were sent repeatedly with every request, but HTTP/2 maintains a header table and sends only the changed parts. This can reduce header size by up to 85-90%, and it shows a significant performance improvement, especially in mobile environments.
Server Push is a feature in which the server sends needed resources in advance, before the client requests them. When an HTML file is requested, the server can push the associated CSS and JavaScript files along with it. However, in actual use, due to issues such as cache invalidation, Server Push support was removed starting with Chrome 106, and 103 Early Hints or the preload tag is now recommended.
# Nginx HTTP/2 설정
# /etc/nginx/nginx.conf 또는 /etc/nginx/sites-available/default
server {
listen 443 ssl http2; # HTTP/2 활성화 (TLS 필수)
listen [::]:443 ssl http2;
server_name example.com;
# SSL 인증서 설정
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
# HTTP/2 최적화 설정
http2_max_concurrent_streams 128; # 동시 스트림 수
http2_push_preload on; # preload 링크 자동 푸시
# HTTP/2 서버 푸시 예시 (Chrome 106 이후 미지원)
location / {
root /var/www/html;
http2_push /css/style.css;
http2_push /js/app.js;
}
# 103 Early Hints 설정 (서버 푸시 대안)
location / {
add_header Link "; rel=preload; as=style";
add_header Link "; rel=preload; as=script";
}
}
# HTTP → HTTPS 리다이렉트
server {
listen 80;
server_name example.com;
return 301 https://$server_name$request_uri;
}
# HTTP/2 지원 확인: curl -I --http2 https://example.com
# Apache HTTP/2 설정
# /etc/apache2/apache2.conf 또는 httpd.conf
# HTTP/2 모듈 활성화
# sudo a2enmod http2
LoadModule http2_module modules/mod_http2.so
# 전역 HTTP/2 설정
Protocols h2 h2c http/1.1
H2Direct on
H2MaxSessionStreams 100
H2WindowSize 65535
# 가상 호스트 설정
<VirtualHost *:443>
ServerName example.com
DocumentRoot /var/www/html
# SSL 설정
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
# HTTP/2 활성화
Protocols h2 http/1.1
# HTTP/2 서버 푸시
<Location />
Header add Link "</css/style.css>; rel=preload; as=style"
Header add Link "</js/app.js>; rel=preload; as=script"
</Location>
# HTTP/2 우선순위 설정
H2Push on
H2PushPriority * after
H2PushPriority text/css before
H2PushPriority application/javascript interleaved
# 로그 설정
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %{SSL_PROTOCOL}x %{HTTP2}e" http2_log
</VirtualHost>
# 확인: apachectl -M | grep http2
const http2 = require('http2');
const fs = require('fs');
const path = require('path');
// HTTP/2 서버 생성 (TLS 필수)
const server = http2.createSecureServer({
key: fs.readFileSync('server.key'),
cert: fs.readFileSync('server.crt'),
allowHTTP1: true // HTTP/1.1 폴백 허용
});
// 스트림 핸들러
server.on('stream', (stream, headers) => {
const reqPath = headers[':path'];
const method = headers[':method'];
console.log(`${method} ${reqPath}`);
if (reqPath === '/') {
// 메인 HTML 응답
stream.respond({
'content-type': 'text/html; charset=utf-8',
':status': 200
});
// 서버 푸시 (권장하지 않음 - 대안 사용)
// 103 Early Hints 방식으로 대체 권장
stream.pushStream({ ':path': '/style.css' }, (err, pushStream) => {
if (!err) {
pushStream.respond({ 'content-type': 'text/css', ':status': 200 });
pushStream.end('body { font-family: sans-serif; }');
}
});
stream.end(`
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="/style.css">
</head>
<body>
<h1>HTTP/2 Server with Node.js</h1>
<p>멀티플렉싱, 헤더 압축, 서버 푸시 지원</p>
</body>
</html>
`);
}
});
// HTTP/2 성능 비교 테스트
const http2Client = http2.connect('https://example.com');
// 멀티플렉싱: 여러 요청 동시 전송
const requests = ['/api/users', '/api/posts', '/api/comments'];
requests.forEach(path => {
const req = http2Client.request({ ':path': path });
req.on('response', headers => console.log(headers[':status']));
req.end();
});
server.listen(443, () => console.log('HTTP/2 server running on port 443'));
"If we switch to HTTP/2, thanks to multiplexing we don't need to do as much image spriting or CSS/JS bundling. In fact, splitting files up is better for caching. However, Server Push has been removed from Chrome, so use 103 Early Hints or the preload tag."
"The core of HTTP/2 is binary framing and multiplexing. HTTP/1.1 was text-based and processed only one request per connection, so it had the HOL Blocking problem, but HTTP/2 processes multiple streams simultaneously over a single connection. Headers are also compressed by more than 85% with HPACK. In practice, when configuring Nginx, we ran it with http2_max_concurrent_streams set to 128."
"TLS is not required for HTTP/2, but browsers don't support h2c (cleartext), so HTTPS is effectively required. You just get a certificate from Let's Encrypt and add 'listen 443 ssl http2' in Nginx, and it's also compatible with existing HTTP/1.1 clients. The protocol is negotiated via ALPN."
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free
With HTTP/2, domain sharding, image sprites, and CSS/JS bundling can actually degrade performance. Because multiplexing is available, split files apart so that each can be cached individually.
Server Push support was removed starting with Chrome 106 (2022). Instead, use a 103 Early Hints response or the <link rel="preload"> tag in HTML. Server Push had the problem of resending resources that were already cached.
HTTP/2 solved HTTP-level HOL Blocking, but it still occurs at the TCP level. When packets are lost, all streams are affected. This problem is solved in HTTP/3, which is based on QUIC.
Enable TLS 1.2 or higher + HTTP/2, use individual caching by splitting files, prioritize loading of critical resources with preload hints, and confirm HTTP/2 support when using a CDN. Use the curl -I --http2 command to verify whether it has been applied.