# Nginx 启用 TLS 1.3
为 Nginx 启用 TLS 1.3 并配置 Brotli 压缩
使用包管理工具安装 Nginx
然后获取编译参数再编译安装
提示
随着新版本的发布,文章内所用的软件版本仅作为参考;请以最新版为准
Last updated on 2020-04-14
# 版本限制
本文使用 Ubuntu 18.04 和 CentOS7 测试,其他发行版可能会存在不同
推荐使用最新主线或稳定版本
注意
源码版本和已安装的版本尽量保持版本号一致,否则替换二进制文件后可能出现兼容性问题
# 获取 TLS 证书
- 启用
TLS
或Brotli
必须配置证书 - 已有证书可继续下一步
没有证书可以通过 Let's Encrypt 获取免费证书
# 解决依赖
[Ubuntu]
apt -y install libpcre3 libpcre3-dev zlib1g-dev
[CentOS]
yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
# 安装 Nginx
wget http://nginx.org/download/nginx-1.17.9.tar.gz
tar -xzvf nginx-1.17.9.tar.gz
rm nginx-1.17.9.tar.gz
# 获取 OpenSSL
提示
推荐同时更新系统 OpenSSL 版本
或者访问腾讯云软件源站 (opens new window)下载
wget https://www.openssl.org/source/openssl-1.1.1f.tar.gz
tar -xzvf openssl-1.1.1f.tar.gz
rm openssl-1.1.1f.tar.gz
# 获取 Brotli
国内主机可能需要使用代理服务器才能正常进行
git clone https://github.com/google/ngx_brotli.git
cd ngx_brotli
git submodule update --init
cd ..
# 检测
进入到 Nginx
目录
cd nginx-1.17.9
查看编译参数,复制 configure arguments:
后面的所有参数
nginx -V
追加以下参数并执行 ./configure
--add-module=../ngx_brotli --with-openssl=../openssl-1.1.1f --with-openssl-opt=enable-tls1_3
如果执行 configure
时出现以下错误可执行安装 build-essential
即可
checking for C compiler ... not found
./configure: error: C compiler cc is not found
[Ubuntu]
apt -y install build-essential
[CentOS]
yum -y install gcc-c++
# 编译
make && make install
替换二进制文件
cp objs/nginx /usr/sbin/
查看版本
nginx -V
显示如下
nginx version: nginx/1.17.9
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.1.1f 31 Mar 2020
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules
--conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log
--pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp
--http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp
--user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module
--with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module
--with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module
--with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module
--with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module
--with-stream_ssl_module --with-stream_ssl_preread_module
--with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong
--param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC'
--with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
--add-module=../ngx_brotli --with-openssl=../openssl-1.1.1f --with-openssl-opt=enable-tls1_3
# 配置 Nginx
- 使用
Brotli
并关闭Gzip
vi /etc/nginx/nginx.conf
找到 Gzip
并注释然后添加以下内容
#gzip off;
brotli on; # off 表示关闭
brotli_comp_level 1; # 压缩等级 0-11
brotli_buffers 16 8k;
brotli_min_length 1k;
brotli_types *; # 压缩类型,* 表示全部
- 启用 TLS 1.3
编辑你的虚拟主机配置并添加以下内容,除 ssl_protocols
外其他可选
# 不显示 Nginx 版本
server_tokens off;
# iframe 仅允许在同一域名下使用
add_header X-Frame-Options sameorigin;
# 详情见:https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/X-Content-Type-Options
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
# HSTS
add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
# openssl dhparam -dsaparam -out dhparam.pem 4096
#ssl_dhparam dhparam.pem;
ssl_ecdh_curve secp384r1;
ssl_session_timeout 10m;
ssl_session_tickets off;
ssl_session_cache shared:SSL:10m;
ssl_stapling on;
ssl_stapling_verify on;
# 0-RTT
#ssl_early_data on;
# 证书配置
ssl_certificate /root/.ssl/ilouis.crt;
ssl_certificate_key /root/.ssl/ilouis.key;
charset utf-8;
对于 ssl_early_data
也叫 0-RTT
可以查看官网介绍进行配置
# 验证
- 检查配置文件是否正确
sudo nginx -t
返回如下表示正确
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
- 重启
Nginx
sudo systemctl restart nginx
# 其他
# 验证
使用 SSLlabs (opens new window) 验证是否启用 TLS1.3
这是本站的检测结果
通过浏览器验证是否已启用 Brotli
# 错误
使用 systemctl status nginx
查看运行状态
对于上述命令中出现的 PID
的错误可以查看这篇文章解决
文章部分内容转载链接: