GolangNote

Golang笔记

在自己的网站部署TLS 1.3

Permalink

前不久Go 1.12 发布,对TLS 1.3 作初步支持,对使用Go 开发的后台来说,这是一个很好的消息。但要启用TLS 1.3 必须添加一个编译参数 GODEBUG=tls13=1,等Go 1.13 就默认支持。

为什么要升级到TLS 1.3?从第三方比较数据看,TLS 1.3 握手时间会减半,即比TLS 1.2 少用100ms。

下面对常见几个后端部署环境启用TLS 1.3 做一下记录

Nginx + OpenSSL

版本要求:TLS 1.3 要求 Nginx 1.13+,OpenSSL 1.1.1+

这里 Nginx,我个人推荐 1.15+,因为 1.14 虽然已经能支持TLS1.3了,但是一些 TLS1.3 的进阶特性还只在 1.15+ 中提供。

目前Nginx 最新版本是 1.15.9 ,OpenSSL 最新版本是 1.1.1b

参见

用最新版本都很方便编译

OpenSSL

首先查看当前的openssl 版本

Bash: openssl
1
2
$ openssl version
OpenSSL 1.0.2g  1 Mar 2016

肯定是很老的版本,下载最新稳定版+打补丁

Bash: openssl 最新稳定版+打补丁
1
2
3
4
5
6
7
8
wget https://github.com/openssl/openssl/archive/OpenSSL_1_1_1b.zip
unzip OpenSSL_1_1_1b.zip
cd openssl-OpenSSL_1_1_1b

curl https://raw.githubusercontent.com/hakasenyang/openssl-patch/master/openssl-1.1.1b-chacha_draft.patch | patch -p1

./config
make install

再看看新版本:

Bash: openssl version
1
openssl version

如果出现下面错误:

plaintext: openssl error
1
openssl: error while loading shared libraries: libssl.so.1.1: cannot open shared object file: No such file or directory

就拷贝两个文件:

Bash: copy *.so
1
2
cp /usr/local/lib/libssl.so.1.1 /lib/x86_64-linux-gnu
cp /usr/local/lib/libcrypto.so.1.1 /lib/x86_64-linux-gnu

删除旧的,建立软链:

Bash: 建立软链
1
2
mv /usr/bin/openssl /tmp/
ln -s /usr/local/bin/openssl /usr/bin/openssl

Nginx

Bash: Nginx 安装
1
2
3
4
5
6
7
8
wget http://nginx.org/download/nginx-1.15.9.tar.gz
tar -zxvf nginx-1.15.9.tar.gz
cd nginx-1.15.9

./configure --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module

make
make install

nginx http 配置

Nginx configuration file: http 配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
events {
    worker_connections  1024;
    use epoll;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    #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;
    tcp_nodelay     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    client_max_body_size 100M;
    client_body_buffer_size 100M;

    gzip  on;
    gzip_min_length 1k;
    gzip_comp_level 2;
    gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
    gzip_vary on;
    gzip_disable "MSIE [1-6]\."; 
}

server 配置,关键部分:

Nginx configuration file: server 配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
listen                       443 http2;

# TLS 1.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;
ssl_prefer_server_ciphers    on;
ssl_protocols                TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
ssl_session_cache            shared:SSL:50m;
ssl_session_timeout          1d;
ssl_session_tickets          on;
ssl_early_data               on;

location / {
    proxy_set_header Early-Data $ssl_early_data;
}

Caddy

Caddy 确实是简单易用的服务,可以直接从官网下载最新版

也可以本地自己编译:

Bash: 本地编译 Caddy
1
2
3
go get -u github.com/mholt/caddy
cd $GOPATH/src/github.com/mholt/caddy/caddy
GOOS=linux go build .

Go Web Server

对于使用Go 来作web 服务来说,只需添加一个环境变量 GODEBUG=tls13=1,或者在程序代码里添加:

Go: tls13
1
2
3
4
5
6
7
goDebug := os.Getenv("GODEBUG")
if goDebug == "" {
    goDebug = "tls13=1"
} else {
    goDebug += ",tls13=1"
}
os.Setenv("GODEBUG", goDebug)

编译即可。

验证TLS 1.3

看看本站已启用

启用TLS1.3

本文网址: https://golangnote.com/topic/254.html 转摘请注明来源

Related articles

Golang Web 程序生产环境独立部署示例

一个 web 应用通常是跑在一个前端代理,如 Nginx 后,这样可以方便的在同一个服务器部署多个应用。这里说的独立部署是指让 go web 程序直接暴露在外面,独占 443、80 端口(俗称裸跑)。这样做除了性能有些提高外,更重要的是部署方便。...

Golang 单实例实现网站多域名请求

有时候写网站,为了统一的后端,把不同业务都集中到一个后端,这时就需要处理多域名的请求,在 Go http server 里实现很简单,只需把不同域名映射到不同的 `http.Handler`。...

Write a Comment to "在自己的网站部署TLS 1.3"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.20 Processed in 1ms