nginx 已经很早就支持http2,今天证书过期,重新申请了一个,同时测试下http2 的push 功能
环境准备
- 证书
这个结合自己的实际去申请,我使用免费的letsencrypt,支持泛域名证书,参考https://www.cnblogs.com/rongfengliang/p/8579181.html - 配置
nginx.conf
worker_processes auto;
events {
worker_connections 65535;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
real_ip_header X-Forwarded-For;
server {
listen 443 ssl http2;
server_name dalong.apicaddy.com;
ssl_certificate /etc/nginx/apicaddy-cert.pem;
ssl_certificate_key /etc/nginx/apicaddy-key.pem;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:AES256+EECDH:AES256+EDH';
ssl_prefer_server_ciphers on;
root /usr/share/nginx/html;
location =/index.html {
http2_push /index.css;
http2_push /app.js;
http2_push /app.css;
}
}
}
- docker-compose 文件
version: "3"
services:
nginx:
build: ./
volumes:
- "./apicaddy-cert.pem:/etc/nginx/apicaddy-cert.pem"
- "./apicaddy-key.pem:/etc/nginx/apicaddy-key.pem"
- "./nginx.conf:/etc/nginx/nginx.conf"
ports:
- "443:443"
- dockerfile
FROM nginx
COPY app.js /usr/share/nginx/html/
COPY app.css /usr/share/nginx/html/
COPY index.css /usr/share/nginx/html/
COPY index.html /usr/share/nginx/html/
测试
- 安装nghttp2
yum insatll nghttp2
- 测试
nghttp -ans https://dalong.apicaddy.com
- 效果
nghttp -ans https://dalong.apicaddy.com
[WARNING]: -a, --get-assets option is ignored because
the binary was not compiled with libxml2.
[WARNING] Certificate verification failed: unable to verify the first certificate
***** Statistics *****
Request timing:
responseEnd: the time when last byte of response was received
relative to connectEnd
requestStart: the time just before first byte of request was sent
relative to connectEnd. If '*' is shown, this was
pushed by server.
process: responseEnd - requestStart
code: HTTP status code
size: number of bytes received as response body without
inflation.
URI: request URI
see http://www.w3.org/TR/resource-timing/#processing-model
sorted by 'complete'
id responseEnd requestStart process code size request path
13 +1.83ms +156us 1.67ms 200 209 /
2 +1.85ms * +1.70ms 150us 200 26 /index.css
4 +1.86ms * +1.73ms 127us 200 134 /app.js
6 +1.88ms * +1.74ms 135us 200 37 /app.css
参考资料
https://www.cnblogs.com/rongfengliang/p/8579181.html
https://github.com/rongfengliang/nginx-http2-push-demo
https://www.nginx.com/blog/nginx-1-13-9-http2-server-push/