在 Nginx 和 Golang web 上抢先体验 QUIC

QUIC(Quick UDP Internet Connection)是谷歌推出的一套基于 UDP 的传输协议,它实现了 TCP + HTTPS + HTTP/2 的功能,目的是保证可靠性的同时降低网络延迟。QUIC 是使用 UDP 协议,可以与原来的 TCP 服务不冲突。

QUIC

Nginx + QUIC

Nginx 官方还没支持 QUIC ,Cloudflare 给 nginx 做了一个补丁,但说仅支持 1.16.x 。我用nginx 目前最新稳定版 1.17.9 也没问题。

下面是安装的过程,系统 ubuntu/debian

安装依赖

1
apt-get install build-essential automake autoconf make git

安装 pcre,支持重写rewrite功能

源码下载地址: https://ftp.pcre.org/pub/pcre/

1
2
3
4
5
wget https://ftp.pcre.org/pub/pcre/pcre-8.44.tar.gz 
tar -zxvf pcre-8.44.tar.gz
cd pcre-8.44
./configure
make && make install

安装 zlib, 支持 gzip 压缩

源码下载地址: http://zlib.net

1
2
3
4
5
wget http://zlib.net/zlib-1.2.11.tar.gz
tar -zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure
make && make install

安装 nginx

源码地址: http://nginx.org/en/download.html

1
2
wget http://nginx.org/download/nginx-1.17.9.tar.gz
tar -zxvf nginx-1.17.9.tar.gz

下载 nginx quic 补丁

1
git clone --recursive https://github.com/cloudflare/quiche

还安装 golang 、rust 环境

1
2
wget https://dl.google.com/go/go1.14.linux-amd64.tar.gz
tar -C /usr/local -xzf go1.14.linux-amd64.tar.gz

添加环境变量 /etc/profile

1
export PATH=$PATH:/usr/local/go/bin

安装 cargo

1
curl https://sh.rustup.rs -sSf | sh

开始编译

1
2
3
4
5
6
7
8
9
10
11
12
13
cd nginx-1.17.9
patch -p01 < ../quiche/extras/nginx/nginx-1.16.patch

./configure --prefix=/usr/local/nginx \
   	--with-http_ssl_module              	\
   	--with-http_v2_module               	\
   	--with-http_v3_module               	\
   	--with-openssl=../quiche/deps/boringssl \
   	--with-quiche=../quiche \
    --with-pcre=/root/downloads/pcre-8.44 \
    --with-zlib=/root/downloads/zlib-1.2.11 

make && make install

配置,在 nginx.confserver 里添加下面内容,还要先申请证书,改为自己的证书路径。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
server {
    # Enable QUIC and HTTP/3.
    listen 443 quic reuseport;

    # Enable HTTP/2 (optional).
    listen 443 ssl http2;

    listen       80;
    server_name  localhost;

    ssl_certificate  /root/example.com.cer;
    ssl_certificate_key  /root/example.com.key;

    # Enable all TLS versions (TLSv1.3 is required for QUIC).
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
    
    # Add Alt-Svc header to negotiate HTTP/3.
    add_header alt-svc 'h3-23=":443"; ma=86400';

    ...
}

启动 nginx

1
/usr/local/nginx/sbin/nginx

查看nginx 监听端口

1
2
3
4
# netstat -peanut | grep  nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      0          499991     27365/nginx: master 
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      0          499990     27365/nginx: master 
udp        0      0 0.0.0.0:443             0.0.0.0:*                           0          499989     27365/nginx: master 

安装过程挺久,还需要有好的网络环境,还要安装 go 和 rust 相关库。还不如直接用 go

go + QUIC

caddy 很方便,但caddy 1 已经放弃参数 -quic ,将在 caddy 2 里支持,caddy 2 使用库 lucas-clemente/quic-go ,因此就直接使用 lucas-clemente/quic-go,先体验。

写一个go脚本

Go:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package main

import (
	"github.com/lucas-clemente/quic-go/http3"
	"net/http"
)

func main() {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		_, _ = w.Write([]byte("hello quic"))
	})

	_ = http3.ListenAndServe("0.0.0.0:443",
		"/root/example.com.cer",
		"/root/example.com.key", nil)
}

编译后运行,查看端口:

1
2
3
# netstat -peanut | grep  http3server
tcp6       0      0 :::443                  :::*                    LISTEN      0          501623     27472/./http3server        
udp6       0      0 :::443                  :::*                                0          501622     27472/./http3server 

协议是 tcp6udp6 ,但不影响 v4 的访问,nginx 是 v4。

总结

如果嵌入到单个应用,还是 go 使用比较方便。现在相关库还在完善,不急于部署到生产环境,虽然google 自家已经在很多产品使用 quic

google quic

参考

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

Suggested Topics

Golang 服务之坑:too many open files

出现这个问题是因为服务的文件句柄超出系统限制。当Go服务程序出现这个问题,首先应该看系统设置,然后再看程序本身。...

在128M的VPS上配置mysql+Tornado+Nginx笔记

最近 123systems http://goo.gl/2Q0X2 又推出一年$10的便宜 VPS,128M内存,可以用来学习。在这样的vps 上放一个博客或做反向代理绰绰有余,买下后尝试配一个mysql+Tornado+Nginx 环境。...

Golang 版 supervisord 使用记录

python 版经常出现一些错误,比如 supervisor.sock 文件找不到的错误。懒得去整,试试二进制的 supervisord ,用 Go 语言编写。...

Golang 实现新闻网页提取正文内容

前段时间接触到新闻页面的提取问题,发现了python 实现的 gne ,测试一段时间,效果很好,但还不适合个人的需求,于是就用 go 来实现类似的功能。...

Leave a Comment

2 thoughts on "在 Nginx 和 Golang web 上抢先体验 QUIC"

#1 李哈哈 says:

您好,在nginx.conf中加入以上内容,在启动nginx时提示配置文件中的quic为无效参数,想请教您这种情况怎么解决?谢谢

#2 pylist says:

@李哈哈 前提是从源码编译nginx,./configure 要包含相关内容。输出你的 configure 看看。