在 Nginx 和 Golang web 上抢先体验 QUIC
QUIC(Quick UDP Internet Connection)是谷歌推出的一套基于 UDP 的传输协议,它实现了 TCP + HTTPS + HTTP/2 的功能,目的是保证可靠性的同时降低网络延迟。QUIC 是使用 UDP 协议,可以与原来的 TCP 服务不冲突。
Nginx + QUIC
Nginx 官方还没支持 QUIC ,Cloudflare 给 nginx 做了一个补丁,但说仅支持 1.16.x
。我用nginx 目前最新稳定版 1.17.9
也没问题。
下面是安装的过程,系统 ubuntu/debian
安装依赖
apt-get install build-essential automake autoconf make git
安装 pcre,支持重写rewrite功能
源码下载地址: https://ftp.pcre.org/pub/pcre/ 4
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 2
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 5
wget http://nginx.org/download/nginx-1.17.9.tar.gz
tar -zxvf nginx-1.17.9.tar.gz
下载 nginx quic 补丁
git clone --recursive https://github.com/cloudflare/quiche
还安装 golang 、rust 环境
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
export PATH=$PATH:/usr/local/go/bin
安装 cargo
curl https://sh.rustup.rs -sSf | sh
开始编译
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.conf
的 server
里添加下面内容,还要先申请证书,改为自己的证书路径。
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
/usr/local/nginx/sbin/nginx
查看nginx 监听端口
# 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脚本
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)
}
编译后运行,查看端口:
# netstat -peanut | grep http3server
tcp6 0 0 :::443 :::* LISTEN 0 501623 27472/./http3server
udp6 0 0 :::443 :::* 0 501622 27472/./http3server
协议是 tcp6
和 udp6
,但不影响 v4 的访问,nginx 是 v4。
总结
如果嵌入到单个应用,还是 go 使用比较方便。现在相关库还在完善,不急于部署到生产环境,虽然google 自家已经在很多产品使用 quic
参考
精彩评论
发表评论
相关推荐
小工具
标签
最近发表
- Chrome 控制台 DevTools failed to load SourceMap 警告的消除方法
- Mac 关闭 Microsoft 自动更新
- Mac 登录 App Store 出现“发生了未知错误”的解决方法
- 老笔记本改造为无风扇静音主机方案
- 自己组装21瓦低功耗家庭爬虫、文件、web服务器
- 微信视频号尝鲜体验
- Ubuntu/Debian 查看CPU温度的方法
- 在Ubuntu/debian Server 系统使用Chrome 无头浏览模式
- 换手机后 Google 身份验证器 Google Authenticator 数据迁移的简单方法
- 使用Golang selenium WebDriver 自动登录微博
- 在 Ubuntu 或其它 GNU/Linux 系统下安装 Debian
- Mac 下制作 USB ubuntu/debian 系统启动、安装盘的几种方法
- ubuntu/debian 下自行编译 OpenWRT 固件
- 宅家自己动手换手机屏幕掉坑记
- 路由 UBNT ER-X 官方固件升级及开启硬件加速的方法
- 在 Nginx 和 Golang web 上抢先体验 QUIC
最近浏览
- Ubuntu/Debian 查看CPU温度的方法
- 用cpulimit 动态限制进程cpu使用率
- Openwrt 路由上开启BBR
- python 解析电子书的信息
- VirtualBox 虚拟机里网络很慢的解决方法
- 换手机后 Google 身份验证器 Google Authenticator 数据迁移的简单方法
- Openwrt 使用USB 4G 上网卡共享网络
- 路由 UBNT ER-X 官方固件升级及开启硬件加速的方法
- Mac 登录 App Store 出现“发生了未知错误”的解决方法
- 使用caddy 快速部署多个https 网站
- python 终端 print 正确显示中文
- 在终端使用Mac diskutil 命令格式化顽固U盘
- go get 使用socks5 代理安装依赖库
- Golang 正则切割字符串
- OpenWrt 国内源
- 关于go json 的性能效率问题
您好,在nginx.conf中加入以上内容,在启动nginx时提示配置文件中的quic为无效参数,想请教您这种情况怎么解决?谢谢
@李哈哈 前提是从源码编译nginx,
./configure
要包含相关内容。输出你的configure
看看。