宝塔面板启用 QUIC 与 Brotli 的完整教程
环境
系统:Ubuntu 22.04.4 LTS x86_64
宝塔版本:7.7.0 (可使用本博客提供的一键安装优化脚本)
nginx版本:1.26.1
开放UDP端口
注意:在你的服务器商家那里也要开放443 udp
端口
sudo ufw allow 443/udp
然后重新加载 UFW 以使新规则生效:
sudo ufw reload
安装 ngx_brotli模块
cd /www/server
git clone --recurse-submodules -j8 https://github.com/google/ngx_brotli
添加 ngx_brotli 编译参数
echo "--add-module=/www/server/ngx_brotli" > /www/server/panel/install/nginx_configure.pl
到这一步其实可以编译nginx了,但是为了更好的性能释放,先升级OpenSSL后再编译
升级OpenSSL 版本
宝塔中编译安装的 Nginx 使用的是 OpenSSL 1.1.1(版本老旧),可以使用命令查看
nginx -V
开始升级
cd /www/server
git clone https://github.com/openssl/openssl.git
修改宝塔面板的 Nginx 安装脚本
cd /www/server/panel/install
wget -O zzbnginx.sh https://raw.githubusercontent.com/woniu336/open_shell/main/zzbnginx.sh
编译安装 Nginx,如果以前已安装过 Nginx 则先卸载
sh zzbnginx.sh install 1.26
等待编译完成
注意后面的小版本号不要输入,当前输入 1.26 即等于 1.26.1 版。
安装完成后,使用 nginx -V
查看编译详情:
如服务器的性能较强,CPU 与内存足够,还可以在 http 块(即宝塔面板>Nginx>设置>配置修改)中添加以下指令:
http3_max_concurrent_streams 256;
http3_stream_buffer_size 128k;
http3_max_concurrent_streams
这个指令是用来限制在单个 HTTP/3 连接上可以同时处理的最大流数量。默认值 128
意味着一个单一的 HTTP/3 连接可以同时承载最多 128 个双向数据流。
http3_stream_buffer_size
指令用于设置 HTTP/3 流的缓冲区大小。这个缓冲区用于存储待发送或已接收但尚未处理的数据。其默认值为 64k,增加缓冲区大小可以减少 I/O 操作的频率,可能对性能有所提升,特别是在处理大块数据传输时。
不过需要注意的是:这两个参数的值越大,占用的内存较多。
启用 Brotli压缩
nginx编译完成后,打开nginx配置,在gzip_disable的下方添加以下代码
brotli on;brotli_comp_level 6; brotli_buffers 16 8k; brotli_min_length 20; brotli_types *;brotli_static always; brotli_window 512k;
保存并重启 Nginx 。
当你重启nginx可能会报错的原因:
从 Nginx 1.25.1 开始,不再支持在 listen
中使用 http2
参数,需要单独一行 http2 on
;
所以,在站点配置中或其他站点,把http2去掉,单独一行,文末有说明
启用 QUIC/HTTP3
前提,你已经配置了证书
打开你的站点,完整的配置在文末,把域名改成你的即可:
注意事项:
- listen 443 quic reuseport; ## reuseport 只能在一个站点使用,其他站点请去掉reuseport
- listen 443 quic; ## 例如,其他站点没有reuseport参数
- include enable-php-81.conf; ## 这里的php是8.1版本,请修改成你使用的版本
- access_log和error_log 后面也要改成你的域名
- root /www/wwwroot/baidu.org; 这里改成你的站点目录
- server_name baidu.org www.baidu.org; 改成你的域名
当你重启nginx会报错的原因:
从 Nginx 1.25.1 开始,不再支持在 listen
中使用 http2
参数,需要单独一行 http2 on
;
所以,在站点配置中或其他站点,把http2去掉,单独一行,如图
完整站点配置
server
{listen 80;listen 443 ssl;listen 443 quic reuseport;http2 on;http3 on;http3_hq on;quic_retry on;server_name baidu.org www.baidu.org;index index.php index.html index.htm default.php default.htm default.html;root /www/wwwroot/baidu.org;# SSL 配置ssl_certificate /www/server/panel/vhost/cert/baidu.org/fullchain.pem;ssl_certificate_key /www/server/panel/vhost/cert/baidu.org/privkey.pem;ssl_protocols TLSv1.2 TLSv1.3;ssl_ciphers 'ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384';ssl_prefer_server_ciphers on;ssl_session_cache shared:SSL:10m;ssl_session_timeout 10m;ssl_early_data on;ssl_stapling on;ssl_stapling_verify on;# HTTP重定向至HTTPSif ($server_port !~ 443){rewrite ^(/.*)$ https://$host$1 permanent;}# Proxy headersproxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection "Upgrade";proxy_buffering off;proxy_http_version 1.1;proxy_read_timeout 600s;# HTTP/3 headersadd_header Alt-Svc 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"';add_header quic-status $http3 always;add_header x-quic 'h3' always;add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload";# Error pageserror_page 497 https://$host$request_uri;# PHP 配置include enable-php-81.conf;# Rewrite rulesinclude /www/server/panel/vhost/rewrite/baidu.org.conf;# 禁止访问的文件或目录location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md){return 404;}# 一键申请SSL证书验证目录相关设置location ~ \.well-known {allow all;}# Static file handlinglocation ~ .*\.(gif|jpg|jpeg|png|bmp|swf)${expires 30d;error_log /dev/null;access_log /dev/null;}location ~ .*\.(js|css)?${expires 12h;error_log /dev/null;access_log /dev/null; }# Loggingaccess_log /www/wwwlogs/baidu.org.log;error_log /www/wwwlogs/baidu.org.error.log;
}
保存并重启 Nginx 。
http3/quic检测
https://http3.wcode.net/
https://http3check.net/
重启nginx报错http2
- 从 Nginx 1.25.0 开始,不再支持在
listen
中使用http3
参数,需要修改为quic
; - 从 Nginx 1.25.1 开始,不再支持在
listen
中使用http2
参数,需要单独一行http2
所以,查找其他站点配置文件,把 http2 去掉,单独一行,比如:
server {listen 443 ssl;listen 443 quic reuseport;http2 on;
浏览器配置
谷歌Chrome浏览器 访问:chrome://flags/
启用:Experimental QUIC protocol (enable-quic)微软新版Edge浏览器 访问:edge://flags
启用:Experimental QUIC protocol 设置为“Enabled”火狐Firefox浏览器 访问:about:config
启用:network.http.http3.enabled