博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HTTP/2部署使用
阅读量:4672 次
发布时间:2019-06-09

本文共 6743 字,大约阅读时间需要 22 分钟。

为了更好地研究HTTP2的一些新特性,或者有小伙伴想让自己的站点支持HTTP2的请求,以提升访问性能……无论出于什么目的,我们都有必要尝试将HTTP2部署使用。

 

而刚好,我们前一段时间在做HTTP2的性能测试,使用Nginx搭建了Web服务器,所以这里再次做下总结。

 

HTTP2的支持了解

 

在部署之前,我们先来了解一下,这里也简单地列举一些比较知名和常用的实现:

 

HTTP2 Server

 

name Language Negotiations protocol
Nginx C ALPN, NPN, direct h2, h2c
Apache Tomcat 8.5+ Java ALPN, Upgrade, direct h2, h2c
H2O C ALPN, NPN, direct, Upgrade h2
ngHttp2 C ALPN, NPN, direct, Upgrade h2, h2c
Netty Java ALPN, NPN, direct, Upgrade h2, h2c

 

HTTP2 Client

 

name Language Negotiations protocol
OKHttp Android, Java ALPN, NPN h2
NSURLSession(>iOS9) OC ALPN h2
Netty Java ALPN, NPN, direct, Upgrade h2, h2c
Chromium Net C++ ALPN h2
Curl and libCurl C ALPN, NPN, Upgrade h2, h2c

 

我们选择了Nginx来搭建HTTP2的服务器,可以用Chrome浏览器来作为HTTP2的访问入口,或者是使用如Chromium net、Curl\libCurl等这些支持HTTP2的网络库来实现一个简易的客户端,作为iOS开发,也不得不提下NSURLSession自iOS9.0之后也开始持HTTP2。接下来,我们重点介绍如何用Nginx来搭建HTTP2的服务器。

使用Nginx搭建HTTP2服务器

 

Nginx是在2015年底开始支持HTTP2,,官网上这篇文章也详细介绍了如何让我们的nginx服务器支持HTTP2。这里结合最新的版本,对如何搭建HTTP2服务器进行详细的解析。

 

安装必要组件

 

nginx-ct

 

nginx-ct用于启用Certificate Transparency功能。

 

wget -O nginx-ct.zip -c https://github.com/grahamedgecombe/nginx-ct/archive/v1.3.1.zipunzip nginx-ct.zip

OpenSSL

 

一般的系统都会自带OpenSSL,但是由于不够新,所以并不推荐使用。所以推荐下载最新的OpenSSl库,并在编译Nginx时指定新下载的OpenSSL源码的目录,而不是系统自带版本。

 

wget -O openssl.tar.gz -c https://www.openssl.org/source/openssl-1.1.0c.tar.gztar zxf openssl.tar.gzmv openssl-1.1.0c/ /root/hehui_workbench/openssl

Nginx

 

下载最新Nginx源码并编译安装。

 

wget -c https://nginx.org/download/nginx-1.11.6.tar.gztar zxf nginx-1.11.6.tar.gzcd nginx-1.11.6/./configure --add-module=../nginx-ct-1.3.1 --with-openssl=/root/hehui_workbench/openssl --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_modulemakesudo make install

至此,Nginx会默认安装到/usr/local/nginx/目录下,如需要更改路径,可以在configure是指定。

管理脚本和自启动nginx

 

我们可以通过脚本来管理nginx服务,首先创建一个管理脚本:

 

vim /etc/init.d/nginx

脚本内容:

 

#! /bin/sh### BEGIN INIT INFO# Provides:          nginx# Required-Start:    $all# Required-Stop:     $all# Default-Start:     2 3 4 5# Default-Stop:      0 1 6# Short-Description: starts the nginx web server# Description:       starts nginx using start-stop-daemon### END INIT INFOPATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/binDAEMON=/usr/local/nginx/sbin/nginxNAME=nginxDESC=nginxtest -x $DAEMON || exit 0# Include nginx defaults if availableif [ -f /etc/default/nginx ] ; then  . /etc/default/nginxfiset -e. /lib/lsb/init-functionscase "$1" in  start)    echo -n "Starting $DESC: "    start-stop-daemon --start --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \        --exec $DAEMON -- $DAEMON_OPTS || true    echo "$NAME."    ;;  stop)    echo -n "Stopping $DESC: "    start-stop-daemon --stop --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \        --exec $DAEMON || true    echo "$NAME."    ;;  restart|force-reload)    echo -n "Restarting $DESC: "    start-stop-daemon --stop --quiet --pidfile \        /usr/local/nginx/logs/$NAME.pid --exec $DAEMON || true    sleep 1    start-stop-daemon --start --quiet --pidfile \        /usr/local/nginx/logs/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true    echo "$NAME."    ;;  reload)    echo -n "Reloading $DESC configuration: "    start-stop-daemon --stop --signal HUP --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \        --exec $DAEMON || true    echo "$NAME."    ;;  status)    status_of_proc -p /usr/local/nginx/logs/$NAME.pid "$DAEMON" nginx && exit 0 || exit $?    ;;  *)    N=/etc/init.d/$NAME    echo "Usage: $N {start|stop|restart|reload|force-reload|status}" >&2    exit 1    ;;esacexit 0

修改执行权限:

 

chmod a+x /etc/init.d/nginx

管理Nginx服务(start、stop、restart、reload conf):

 

service nginx start|stop|restart|reload

 

配置Nginx服务器

 

全局配置

 

打开/usr/local/nginx/conf/nginx.conf,修改全局配置文件。通常会将自己的站点配置写在单独的配置文件中,以include的方式引入,这里对于站点的配置文件我会单独放在对应的站点目录里面:/root/hehui_workbench/www/xxxx/website.conf

 

http {    include       mime.types;    default_type  application/octet-stream;    charset       UTF-8;    sendfile        on;    #tcp_nopush     on;    keepalive_timeout  65;    gzip               on;    gzip_vary          on;    gzip_comp_level    6;    gzip_buffers       16 8k;    gzip_min_length    1000;    gzip_proxied       any;    gzip_disable       "msie6";    gzip_http_version  1.0;    gzip_types         text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript image/svg+xml;   # 加载其他配置文件   include                /root/hehui_workbench/www/xxxx/website.conf}

至此,我们已经可以部署支持HTTP1.x的请求服务了。但是,要想对应域名(站点)的请求支持HTTP2(这里指HTTP2 over TLS),我们需要先部署HTTPS,所以必须提供域名的合法证书。一文介绍了如何申请的免费证书。

 

WEB站点配置

 

在获取服务器证书后,对站点进行如下配置:

 

server {    listen 443 ssl http2 fastopen=3 reuseport;    server_name www.lovelyhui.cc    server_tokens off;    include /root/hehui_workbench/www/nginx_conf/ip.blacklist;    #ssl_ct             on;    #ssl_ct_static_scts     /root/hehui_workbench/www/scts;    # 中间证书 + 站点证书    ssl_certificate     /root/hehui_workbench/ssl/chained.pem;    # 创建 CSR 文件时用的密钥    ssl_certificate_key  /root/hehui_workbench/ssl/domain.key;    # openssl dhparam -out dhparams.pem 2048    # https://weakdh.org/sysadmin.html    # ssl_dhparam          /root/hehui_workbench/www/ssl/dhparams.pem;     # https://github.com/cloudflare/sslconfig/blob/master/conf    ssl_ciphers                EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;ssl_prefer_server_ciphers  on;    ssl_protocols              TLSv1 TLSv1.1 TLSv1.2;    ssl_session_cache          shared:SSL:50m;    ssl_session_timeout        1d;    ssl_session_tickets        on;    ssl_stapling               on;    ssl_stapling_verify        on;    # 根证书 + 中间证书    # https://imququ.com/post/why-can-not-turn-on-ocsp-stapling.html    # ssl_trusted_certificate    /root/hehui_workbench/ssl/full_chained.pem;    resolver                   114.114.114.114 valid=300s;    resolver_timeout           10s;    access_log                 /root/hehui_workbench/www/nginx_log/lovelyhui_cc.log;    if ($request_method !~ ^(GET|HEAD|POST|OPTIONS)$ ) {        return           444;     }    if ($host != 'www.lovelyhui.cc'){    rewrite ^/(.*)$  https://www.lovelyhui.cc/$1 permanent;    }    location ^~/api/testApi1 {    default_type application/json;    return 200 '{"code":"50","msg":"This is test for Api1, This is test for Api1"}';  }

此时,我们已经配置好我们的HTTP2.0服务器,我们可以直接浏览器访问https://www.lovelyhui.cc访问此站点了。

简单测试

 

在 Chrome 地址栏输入 chrome://net-internals/#http2 即可进入及具体的请求通信过程:

 

 

 

我们可以继续选择其中的一个HTTP2 Session,点击具体的Session ID,便可以看到全部帧信息:

 

 

 

但是,Chrome的工具仅局限于Chrome浏览器,对于FireFox或者自实现App完成的H2请求,就无法派上用场了。所以,对于HTTP2流量的测试以及性能分析,我们更推荐使用WireShark去完成。可以移步我们HTTP2系列之一文,了解如何使用WireShark调试HTTP/2流量。

 

参考

 

 

 

转至 https://www.cnblogs.com/163yun/p/9511032.html

转载于:https://www.cnblogs.com/wintersoft/p/10767780.html

你可能感兴趣的文章
VTK图形模型主要对象
查看>>
c# Linq实现 获得某一个路径下所有文件的名(不含扩展名)
查看>>
动静态广播的区别
查看>>
前缀式计算(前缀表达式)
查看>>
Linux常用命令大全
查看>>
添加删除tag
查看>>
ARM学习篇 中断定时理解
查看>>
卷积神经网络在tenserflow的实现
查看>>
[STL]用法
查看>>
PostgresException: 42883: function ifnull(integer, integer) does not exist
查看>>
python3 表情符号编码
查看>>
桥接模式
查看>>
跨server传输数据注意事项
查看>>
使用蒙版--渐变--制作瓶子倒影
查看>>
后代元素 span:first-child{...}
查看>>
LeetCode 540. 有序数组中的单一元素(Single Element in a Sorted Array) 42
查看>>
codevs 5958 无
查看>>
htaccess 实现网址缩短
查看>>
第四周作业&&结对编程
查看>>
12. 构造代码块
查看>>