VPS/云主机Web域名/SSL建站运维

Oneinstack/LNMP/宝塔面板:开启TLSV1.3与Brotli压缩-网站优化加速图文教程

本教程分享一下网站优化加速方法:如何开启TLSV1.3和Brotli压缩进行网站加速。相对于TLSV1.2,TLSV1.3主要是减少握手延迟,提高跨协议攻击的难度,使互联网更快,更多安全。Brotli是由谷歌开发的压缩算法,与其他压缩算法相比,它有着更高的压缩效率。

一般来说我们的VPS/云主机已经默认开启了GZIP压缩了,而Brotli与GZIP可以同时共存,当同时开启两种压缩算法时,Brotli 压缩等级优先级高于 Gzip。实际上,最新版的宝塔BT面板、Oneinstack和LNMP都已经默认可以开启TLSV1.3。

Oneinstack/LNMP/宝塔面板:开启TLSV1.3与Brotli压缩-网站优化加速图文教程插图

更多的关于网站服务器优化与加速,这里有:

    1. 如何用Nginx实现 反向绑定/缓存加速/自动更新缓存/获取真实IP/CDN加速
    2. 一键快速安装TCP-BBR加速模块提升VPS/云主机速度

一、开启TLSV1.3

1.1  Oneinstack TLSV1.3

目前最新版的Oneinstack已经默认开启TLSV1.3了,所以如果你是全新安装的Oneinstack,安装完成后就启用了TLSV1.3了。

如果你是全新安装Oneinstack,则可以按照教程先将Oneinstack一键包下载到本地,不要执行安装。而是选择oneinstack目录,编辑version.txt,把openssl_version版本号改到1.1.1以上。

Oneinstack/LNMP/宝塔面板:开启TLSV1.3与Brotli压缩-网站优化加速图文教程插图1

然后开始安装即可。如果你已经是Oneinstack的老用户了,方法类似,先升级一下Oneinstack,修改version.txt的openssl_version版本号,执行Oneinstack自带的升级脚本,升级一下Nginx。然后就可以看到openssl已经升级了。

Oneinstack/LNMP/宝塔面板:开启TLSV1.3与Brotli压缩-网站优化加速图文教程

最后就是修改你的网站Nginx配置文件了,在ssl_protocols 后面添加TLSv1.3,在ssl_ciphers添加数个加密套件,重启Nginx完成。以下是我的Nginx的TLSv1.3配置,ssl_ciphers 的TLSv1.3部分是新增加的。

  1. ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
  2. ssl_ciphers TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256:EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+ECDSA+AES128:EECDH+aRSA+AES128:RSA+AES128:EECDH+ECDSA+AES256:EECDH+aRSA+AES256:RSA+AES256:EECDH+ECDSA+3DES:EECDH+aRSA+3DES:RSA+3DES:!MD5;

现在打开Chrome就可以看到已经成功使用了TLSv1.3。

现在浏览器更新的速度很快,除IE,Chrome和Firefox新版对于TLSv1.3都已经支持了,所以淘汰IE是必然趋势啊。

Oneinstack/LNMP/宝塔面板:开启TLSV1.3与Brotli压缩-网站优化加速图文教程

1.2  LNMP TLSV1.3

目前LNMP 1.6版本已经默认支持TLSv1.3,你只需要按照上面的操作调整一下你的网站Nginx配置就可以开启TLSv1.3。如果你用的是LNMP 1.5的话,可以使用升级命令:upgrade1.x-1.6.sh 升级一下管理脚本。

Oneinstack/LNMP/宝塔面板:开启TLSV1.3与Brotli压缩-网站优化加速图文教程插图4

然后使用1.6的升级脚本升级一下nginx就可以了。如果你不想让LNMP 1.5升级到LNMP 1.6,则打开 \lnmp1.5\include\version.sh 文件,将 Openssl_Ver=‘openssl-1.0.2o’ 修改为:Openssl_Ver=‘openssl-1.1.1a’

Oneinstack/LNMP/宝塔面板:开启TLSV1.3与Brotli压缩-网站优化加速图文教程插图5

然后修改 \lnmp1.5\lnmp.conf 文件,将 Nginx_Modules_Options= 改为:Nginx_Modules_Options=‘–with-openssl-opt=enable-weak-ssl-ciphers’(注:enable-weak-ssl-ciphers 作用是让 OpenSSL 继续支持 3DES 等不安全 Cipher Suite,如果你打算继续支持 IE8,才需要加上这个选项;若不需要支持 XP IE8 访问可忽略此处修改)

Oneinstack/LNMP/宝塔面板:开启TLSV1.3与Brotli压缩-网站优化加速图文教程

进入 lnmp1.5 目录,执行命令:

  1. ./upgrade.sh nginx

然后输入需要升级的 nginx 版本号,如目前最新的 1.16.x。nginx 最新版本号可从官网获取:http://nginx.org。静待编译完成。执行 nginx -V 可查询详细配置信息:

  1. Checking …
  2. Program will display Nginx Version……
  3. nginx version: nginx/1.15.7
  4. ======== upgrade nginx completed ======
  5. nginx version: nginx/1.15.7
  6. built by gcc 4.8.5 20150623 (Red Hat 4.8.528) (GCC)
  7. built with OpenSSL 1.1.1a 20 Nov 2018
  8. TLS SNI support enabled
  9. configure arguments: –user=www –group=www –prefix=/usr/local/nginx –with-http_stub_status_module –with-http_ssl_module –with-http_v2_module –with-http_gzip_static_module –with-http_sub_module –with-stream –with-stream_ssl_module –with-openssl=/root/lnmp1.5/src/openssl-1.1.1a –with-openssl-opt=enable-weak-ssl-ciphers

修改主机配置文件,加入TLSv1.3配置,如下(和上面的Oneinstack是一样的)。文件修改完,重启一下 nginx,然后就可以去浏览器访问验证一下。

  1. ssl_protocols 加入 TLSv1.3 支持,如:ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
  2. #ssl_ciphers 参考配置:
  3. ssl_ciphers TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256:EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+ECDSA+AES128:EECDH+aRSA+AES128:RSA+AES128:EECDH+ECDSA+AES256:EECDH+aRSA+AES256:RSA+AES256:EECDH+ECDSA+3DES:EECDH+aRSA+3DES:RSA+3DES:!MD;

1.3  宝塔BT面板 TLSV1.3

新版的宝塔BT面板,已经支持nginx1.16了,直接在“软件管理”页面切换nginx1.16即可。

Oneinstack/LNMP/宝塔面板:开启TLSV1.3与Brotli压缩-网站优化加速图文教程

然后在网站的配置文件加上TLSv1.3相关配置就可以了。

  1. ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
  2. #ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
  3. ssl_ciphers TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256:EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+ECDSA+AES128:EECDH+aRSA+AES128:RSA+AES128:EECDH+ECDSA+AES256:EECDH+aRSA+AES256:RSA+AES256:EECDH+ECDSA+3DES:EECDH+aRSA+3DES:RSA+3DES:!MD5;

Oneinstack/LNMP/宝塔面板:开启TLSV1.3与Brotli压缩-网站优化加速图文教程

二、服务器SSL优化

SSL优化:

  1. 效果演示:https://www.ssllabs.com/ssltest/analyze.html?d=你的网站域名

如下图:

Oneinstack/LNMP/宝塔面板:开启TLSV1.3与Brotli压缩-网站优化加速图文教程插图9

优化的方法除了本文讲到的开启TLSV1.3,优化的经验(SSL双证书、开启HSTS)也在以下两篇文章中有分享过:

1.开启HSTS功能并加入Preload List让网站Https访问更加安全(附代删除HSTS方法)

2.HTTPS和SSL优化使用心得之:减少等待时间与降低Https性能损耗(Apache/Nginx)

三、开启Brotli压缩

项目:

  1. https://github.com/google/ngx_brotli

3.1  Oneinstack Brotli

Oneinstack 开启 Brotli需要先进行编译,最简单的方法就是利用Oneinstack自带的升级脚本,将Brotli编译到Nginx,方法如下:

  1. cd oneinstack/src
  2. git clone https://github.com/google/ngx_brotli.git
  3. cd ngx_brotli
  4. git submodule update –init
  5. #修改options.conf
  6. nginx_modules_options中新增–add-module=../ngx_brotli
  7. ./upgrade.sh web nginx
  8. #20190118更新,新版Oneinstack需要到oneinstack\include目录,找到upgrade_web.sh,修改:
  9. ./configure ${nginx_configure_args}
  10. #新增:
  11. ./configure ${nginx_configure_args} –add-module=../ngx_brotli
  12. #最后(执行升级,选择Nginx):
  13. ~/oneinstack/upgrade.sh
  14. #修改/usr/local/nginx/conf/nginx.conf
  15. brotli on;
  16. brotli_comp_level 6;
  17. brotli_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript image/svg+xml;

或者直接手动编译Brotli到Nginx:

  1. cd oneinstack/src
  2. git clone https://github.com/google/ngx_brotli.git
  3. cd ngx_brotli
  4. git submodule update –init
  5. #以下几个安装包都是Oneinstack自带的,不同的版本可能会不同,请根据情况下载到/root/oneinstack/src/并解压
  6. tar xzf nginx-1.14.2.tar.gz
  7. tar xzf pcre-8.42.tar.gz
  8. tar xzf openssl-1.1.1a.tar.gz
  9. cd /root/oneinstack/src/nginx-1.14.2
  10. nginx -V #查看nginx编译参数,最后加上–add-module=../ngx_brotli
  11. ./configure –prefix=/usr/local/nginx –user=www –group=www –with-http_stub_status_module –with-http_v2_module –with-http_ssl_module –with-http_gzip_static_module –with-http_realip_module –with-http_flv_module –with-http_mp4_module –with-openssl=../openssl-1.1.1a –with-pcre=../pcre-8.42 –with-pcre-jit –with-ld-opt=-ljemalloc –add-module=../ngx_cache_purge-2.3 –add-module=../ngx_brotli
  12. make #编译
  13. mv /usr/local/nginx/sbin/nginx{,_`date +%F`} #备份nginx
  14. cp objs/nginx /usr/local/nginx/sbin
  15. nginx -V

Nginx Brotli编译成功。

Oneinstack/LNMP/宝塔面板:开启TLSV1.3与Brotli压缩-网站优化加速图文教程

打开你的网站的Nginx配置文件,添加以下代码:

  1. #修改/usr/local/nginx/conf/nginx.conf
  2. brotli on;
  3. brotli_comp_level 6;
  4. brotli_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript image/svg+xml;

如下图:

Oneinstack/LNMP/宝塔面板:开启TLSV1.3与Brotli压缩-网站优化加速图文教程

重启Nginx,刷新网页,利用Chrome审查元素,就可以看到网页显示“br”字样,表示使用了Brotli压缩。

Oneinstack/LNMP/宝塔面板:开启TLSV1.3与Brotli压缩-网站优化加速图文教程插图12

3.2  LNMP Brotli

在LNMP上编译Brotli类似于上面的Oneinstack,首先也是将Brotli下载到本地,然后编辑一下配置,重新编译一下Nginx即可。命令如下:

  1. #下载
  2. cd lnmp1.5/src
  3. git clone https://github.com/google/ngx_brotli.git
  4. cd ngx_brotli
  5. git submodule update –init
  6. #编辑配置,请根据你自己的路径调整
  7. vi /root/lnmp1.5/lnmp.conf
  8. #添加
  9. Nginx_Modules_Options=‘–add-module=/www/lnmp1.5/ngx_brotli’
  10. #最后,重新重新编译或者升级Nginx
  11. cd /root/lnmp1.5
  12. ./upgrade.sh nginx

编辑配置文件如下:

Oneinstack/LNMP/宝塔面板:开启TLSV1.3与Brotli压缩-网站优化加速图文教程

最后,还是修改你的网站配置加入Brotli代码。

  1. brotli on;
  2. brotli_comp_level 6;
  3. brotli_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript image/svg+xml;

3.3  宝塔面板 Brotli

宝塔面板编译 Brotli也一样,利用宝塔自带的脚本:www/server/panel/install/nginx.sh对Nginx重新编译和升级,代码如下:

  1. #安装libbrotli
  2. cd /www/server
  3. git clone https://github.com/bagder/libbrotli
  4. cd libbrotli
  5. ./autogen.sh
  6. ./configure
  7. make && make install
  8. #下载ngx_brotli模块及其依赖:
  9. cd /www/server
  10. git clone https://github.com/google/ngx_brotli
  11. cd ngx_brotli && git submodule update –init
  12. #获取Nginx Arguments
  13. nginx -V
  14. #编辑配置
  15. vi /www/server/panel/install/nginx.sh
  16. #在你需要安装的Nginx版本下增加:–add-module=/www/server/ngx_brotli
  17. if [ “${nginx_version}” == “1.12.2” ] || [ “${nginx_version}” == “1.14.2” ];then
  18. ./configure –user=www –group=www –prefix=${Setup_Path} –with-openssl=${Update_Path}/src/openssl –add-module=${Update_Path}/src/ngx_devel_kit –add-module=${Update_Path}/src/lua_nginx_module –add-module=${Update_Path}/src/ngx_cache_purge –add-module=${Update_Path}/src/nginx-sticky-module –with-http_stub_status_module –with-http_ssl_module –with-http_image_filter_module –with-http_v2_module –with-http_gzip_static_module –with-http_gunzip_module –with-stream –with-stream_ssl_module –with-ipv6 –with-http_sub_module –with-http_flv_module –with-http_addition_module –with-http_realip_module –with-http_mp4_module –with-ld-opt=“-Wl,-E” –with-pcre=pcre-${pcre_version} ${jemallocLD} –add-module=/www/server/ngx_brotli
  19. elif [ “${nginxVersion}” == “1.15.6” ]; then
  20. ./configure –user=www –group=www –prefix=${Setup_Path} –with-openssl=${Setup_Path}/src/openssl –add-module=${Setup_Path}/src/ngx_devel_kit –add-module=${Setup_Path}/src/lua_nginx_module –add-module=${Setup_Path}/src/ngx_cache_purge –add-module=${Setup_Path}/src/nginx-sticky-module –with-http_stub_status_module –with-http_ssl_module –with-http_v2_module –with-http_image_filter_module –with-http_gzip_static_module –with-http_gunzip_module –with-stream –with-stream_ssl_module –with-ipv6 –with-http_sub_module –with-http_flv_module –with-http_addition_module –with-http_realip_module –with-http_mp4_module –with-ld-opt=“-Wl,-E” –with-openssl-opt=“enable-tls1_3 enable-weak-ssl-ciphers” ${jemallocLD} –add-module=/www/server/ngx_brotli
  21. elif [ “$nginx_version” == “openresty” ]; then
  22. ./configure –user=www –group=www –prefix=${Setup_Path} –with-openssl=${Update_Path}/src/openssl –with-pcre=pcre-${pcre_version} –add-module=${Update_Path}/src/ngx_cache_purge –add-module=${Update_Path}/src/nginx-sticky-module –with-luajit –with-http_stub_status_module –with-http_ssl_module –with-http_image_filter_module –with-http_v2_module –with-http_gzip_static_module –with-http_gunzip_module –with-stream –with-stream_ssl_module –with-ipv6 –with-http_sub_module –with-http_flv_module –with-http_addition_module –with-http_realip_module –with-http_mp4_module –with-ld-opt=“-Wl,-E” ${jemallocLD}
  23. elif [ “${nginxVersion}” = “-Tengine2.2.3” ]; then
  24. ./configure –user=www –group=www –prefix=${Setup_Path} –with-openssl=${Update_Path}/src/openssl –add-module=${Update_Path}/src/ngx_devel_kit –add-module=${Update_Path}/src/lua_nginx_module –add-module=${Update_Path}/src/ngx_cache_purge –add-module=${Update_Path}/src/nginx-sticky-module –with-http_stub_status_module –with-http_ssl_module –with-http_image_filter_module –with-http_v2_module –with-http_gzip_static_module –with-http_gunzip_module –with-ipv6 –with-http_sub_module –with-http_flv_module –with-http_addition_module –with-http_realip_module –with-http_mp4_module –with-ld-opt=“-Wl,-E” –without-http_upstream_session_sticky_module –with-pcre=pcre-${pcre_version} –add-module=/www/server/ngx_brotli
  25. #最后,重新编译Nginx(请根据自己的需要来选择)
  26. sh /www/server/panel/install/nginx.sh install 1.14

最后,在你的网站的Nginx配置中加入Brotli代码即可。

  1. brotli on;
  2. brotli_comp_level 6;
  3. brotli_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript image/svg+xml;

关于Brotli一些参数的说明,你可以根据自己的需要来调整:

  1. brotli on; #启用
  2. brotli_comp_level 6; #压缩等级,默认 6,太高的压缩水平可能需要更多的 CPU
  3. brotli_buffers 16 8k; #请求缓冲区的数量和大小
  4. brotli_min_length 20; #指定压缩数据的最小长度,只有大于或等于最小长度才会对其压缩。这里指定 20 字节
  5. brotli_types *; #指定允许进行压缩类型
  6. # brotli_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml text/html application/json image/svg application/font-woff application/vnd.ms-fontobject application/vnd.apple.mpegurl image/x-icon image/jpeg image/gif image/png image/bmp;
  7. brotli_static always; #是否允许查找预处理好的、以 .br 结尾的压缩文件,可选值为 on、off、always
  8. brotli_window 512k; #窗口值,默认值为 512k

四、总结

要不要用TLSV1.3?总得来看,TLSV1.3是未来的趋势,有人担心浏览器不支持,其实除了IE,最新版的Chrome和Firefox都是支持TLSV1.3的,各大浏览器支持TLSV1.3情况见:https://caniuse.com/#feat=tls1-3

Oneinstack/LNMP/宝塔面板:开启TLSV1.3与Brotli压缩-网站优化加速图文教程插图14

要不要用Brotli?理论上讲Brotli压缩比比GZIP要高不少,所以对于网页打开速度也是有一定作用的,不过想要秒开的感觉,还是先从硬件上提升一下速度吧,这里是各大浏览器支持Brotli的情况:https://caniuse.com/#feat=brotli

Oneinstack/LNMP/宝塔面板:开启TLSV1.3与Brotli压缩-网站优化加速图文教程

有没有有一键自动开启TLSV1.3和Brotli?有,如果你用的是CloudFlare,CloudFlare已经为所有的网站自动开启TLSv1.3和Brotli。

(END)

文章出自:挖站否 https://wzfou.com/tlsv1-3-brotli/,部分内容参考自 imququ  常阳时光 版权所有。