• HTTP/2部署使用


    本文来自网易云社区

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

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

    HTTP2的支持了解

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

    HTTP2 Server

    nameLanguageNegotiationsprotocol
    NginxCALPN, NPN, directh2, h2c
    Apache Tomcat 8.5+JavaALPN, Upgrade, directh2, h2c
    H2OCALPN, NPN, direct, Upgradeh2
    ngHttp2CALPN, NPN, direct, Upgradeh2, h2c
    NettyJavaALPN, NPN, direct, Upgradeh2, h2c

    HTTP2 Client

    nameLanguageNegotiationsprotocol
    OKHttpAndroid, JavaALPN, NPNh2
    NSURLSession(>iOS9)OCALPNh2
    NettyJavaALPN, NPN, direct, Upgradeh2, h2c
    Chromium NetC++ALPNh2
    Curl and libCurlCALPN, NPN, Upgradeh2, h2c

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

    使用Nginx搭建HTTP2服务器


    Nginx是在2015年底开始支持HTTP2,Announcing an Early Alpha Patch for HTTP/2,官网上这篇文章也详细介绍了如何让我们的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.gz
    mv 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.gz
    cd 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_module
    
    make
    sudo 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/bin
    DAEMON=/usr/local/nginx/sbin/nginx
    NAME=nginx
    DESC=nginx
    
    test -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,所以必须提供域名的合法证书。使用Let's Encrypt免费证书一文介绍了如何申请Let's Encrypt的免费证书。

    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系列之HTTP/2流量调试一文,了解如何使用WireShark调试HTTP/2流量。

    参考

    Nginx 配置之完整篇



    网易云新用户大礼包:https://www.163yun.com/gift

    本文来自网易实践者社区,经作者何慧授权发布。


  • 相关阅读:
    蚂蚁变大象:浅谈常规网站是如何从小变大的(八)(转)
    蚂蚁变大象:浅谈常规网站是如何从小变大的(七)(转)
    蚂蚁变大象:浅谈常规网站是如何从小变大的(六)(转)
    蚂蚁变大象:浅谈常规网站是如何从小变大的(五)(转)
    蚂蚁变大象:浅谈常规网站是如何从小变大的(四)(转)
    蚂蚁变大象:浅谈常规网站是如何从小变大的(三)(转)
    简单的Poc Exp编写(上)
    kali渗透windowsXP过程
    [W3bSafe]Metasploit溢出渗透内网主机辅助脚本
    WannaCry勒索病毒全解读,权威修复指南大集合
  • 原文地址:https://www.cnblogs.com/zyfd/p/9511034.html
Copyright © 2020-2023  润新知