nginx启用HTTP2特性

本文于2017年2月底完成,发布在个人博客网站上。
考虑个人博客因某种原因无法修复,于是在博客园安家,之前发布的文章逐步搬迁过来。


查看当前nginx的编译选项

#./nginx -V
nginx version: nginx/1.9.15
built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4)
built with OpenSSL 1.0.2g 1 Mar 2016
TLS SNI support enabled
configure arguments: --prefix=/home/jackie/software/nginx --with-openssl=/home/jackie/Downloads/nginx/openssl-1.0.2g --with-pcre=/home/jackie/Downloads/nginx/pcre-8.38 --with-zlib=/home/jackie/Downloads/nginx/zlib-1.2.8 --with-http_ssl_module --with-threads --with-debug

启用http2支持

  1. 修改编译选项
    configure的选项中加入--with-http_v2_module,由于HTTP2需要SSL的支持,因此如缺少--with-http_ssl_module选项,还需要加入--with-http_ssl_module
    如下:

    ./configure --prefix=/home/jackie/software/nginx \
    --with-openssl=/home/jackie/Downloads/nginx/openssl-1.0.2g \
    --with-pcre=/home/jackie/Downloads/nginx/pcre-8.38 \
    --with-zlib=/home/jackie/Downloads/nginx/zlib-1.2.8 \
    --with-http_ssl_module \
    --with-threads \
    --with-debug \
    --with-http_v2_module
  2. 编译&升级

    make & make install
  3. 修改配置文件,启用HTTP2,如下:

    server {
    listen 8443 ssl http2 default_server; # 增加 http2 default_server
    server_name 192.168.0.107;
    ...
    }
  4. 验证配置文件

    #./nginx -t
    nginx: the configuration file /home/jackie/software/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /home/jackie/software/nginx/conf/nginx.conf test is successful
  5. 启动nginx

    #./nginx

验证HTTP2是否已启用

方法一

使用高版本如56.0.2924.87的Chrome,按照如下步骤操作

  1. 使用Chrome访问启用http2的站点,比如Jackie的环境为https://192.168.0.107:8443
  2. 新开TAB页,在地址栏中输入chrome://net-internals/#http2,检查HTTP/2 sessions下的表格。
  3. 确认表格里是否出现了上一步访问的主机地址,比如192.168.0.107:8443。

方法二

使用curl命令,参考HTTP/2 with curl,执行如下命令,确认站点返回的协议是否为HTTP

curl --http2 -I 192.168.0.107:8443

如执行上述命令时遇到如下错误,说明系统当前安装的curl还不支持HTTP2协议。

curl https://192.168.0.107:8443/ --http2
curl: (1) Unsupported protocol

可以执行如下命令,检查系统当前安装的curl支持的特性列表,确认是否包含HTTP2。

curl -V
curl 7.47.0 (i686-pc-linux-gnu) libcurl/7.47.0 GnuTLS/3.4.10 zlib/1.2.8 libidn/1.32 librtmp/2.3
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp smb smbs smtp smtps telnet tftp
Features: AsynchDNS IDN IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz TLS-SRP UnixSockets

从前述输出信息可以了解到,当前安装的curl还不支持HTTP2。
这时可参考如何启用curl命令HTTP2支持重新编译curl,加入HTTP2的支持。

方法三

安装Chrome插件HTTP/2 and SPDY indicator,安装完毕后访问启用HTTP2的站点,如果地址栏出现蓝色的闪电,说明站点已启用HTTP2。
不过Jackie身在墙内,安装总是失败,所以没有验证该方法的有效性。

完整的配置文件

如下是完整的配置文件,删除了一些与HTTP2特性不相关的内容。

worker_processes 1;
error_log logs/error.log debug;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server_tokens off;
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 logs/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
charset utf-8;
server {
listen 8080;
server_name 192.168.0.107;
if ($scheme ~ http) {
return https://$server_name:8443$request_uri;
}
}
server {
listen 8443 ssl http2 default_server;
server_name 192.168.0.107;
ssl_certificate /home/jackie/software/nginx_conf/server.crt;
ssl_certificate_key /home/jackie/software/nginx_conf/server.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers 'ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA';
ssl_prefer_server_ciphers on;
add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload" always;
add_header X-Frame-Options SAMEORIGIN always;
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Content-Type-Options nosniff;
location ~ / {
index index.jsp;
proxy_pass http://127.0.0.1:18080;
proxy_set_header referer '';
include proxy.conf;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
}
}

参考资料

nginx启用http2的资料

HTTP2的相关资料

其它

posted @   jackieathome  阅读(226)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示