如何使用Nginx服务开启HTTP2
- 首先在Nginx配置文件中配置
proxy_cache_path cache levels=1:2 keys_zone=my_cache:10m;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name test.com;
return 302 https://$server_name$request_uri;
}
server {
listen 443 http2; # 配置HTTP2
server_name test.com;
http2_push_preload on; # 配置HTTP2
ssl on;
ssl_certificate_key ../certs/localhost-privkey.pem;
ssl_certificate ../certs/localhost-cert.pem;
location / {
proxy_cache my_cache;
proxy_pass http://127.0.0.1:8888;
proxy_set_header Host $host;
}
}
在server.js中要加上Link,如下
response.writeHead(200, {
"Content-Type": "text/html",
Connection: "close",
Link: "</test.jpg>; as=image; rel=preload",
});