隨着最近 Nginx-Quic 分支被合併到了 Nginx 主線,Nginx 1.25.0 版本官方二進制包已經支持 Quic/HTTP3,感興趣的朋友可以前往 https://nginx.org/en/download.html 或 https://nginx.org/en/linux_packages.html 下載安裝,體驗一下 Quic/HTTP3 的魅力,本文將主要爲您介紹如何通過編譯的方式開啓 Quic/HTTP3。
安裝依賴
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| apt update apt install build-essential ca-certificates zlib1g-dev libpcre3 libpcre3-dev tar unzip libssl-dev wget curl git cmake ninja-build libunwind-dev pkg-config
sudo su cd /root apt update apt install build-essential ca-certificates zlib1g-dev libpcre3 libpcre3-dev tar unzip libssl-dev wget curl git cmake ninja-build libunwind-dev pkg-config
dnf update dnf install gcc gcc-c++ pcre-devel openssl-devel zlib-devel cmake make libunwind-devel git wget
dnf update dnf install gcc gcc-c++ pcre-devel openssl-devel zlib-devel cmake make git wget
|
安裝Go
下載並解壓
1 2
| wget https://dl.google.com/go/go1.23.3.linux-amd64.tar.gz rm -rf /usr/local/go && tar -C /usr/local -xzf go1.23.3.linux-amd64.tar.gz
|
請注意系統架構,本文以 x86_64 爲例,如果你的系統架構不是 x86_64,請自行修改下載鏈接。
添加環境變量
1
| export PATH=$PATH:/usr/local/go/bin
|
具體可參考https://go.dev/doc/install
驗證是否安裝成功
考慮國內用戶訪問官方較慢,故設置代理
1
| export GOPROXY=https://mirrors.cloud.tencent.com/go/
|
編譯 boringssl
Debian/Ubuntu
1 2 3 4 5 6 7
| git clone --depth=1 https://github.com/google/boringssl.git cd boringssl mkdir build cd build cmake -GNinja .. ninja cd ../..
|
CentOS 8 Stream/TencentOS Server 3.1/OpenCloudOS Server 8
1 2 3 4 5 6 7
| git clone --depth=1 https://github.com/google/boringssl.git cd boringssl mkdir build cd build cmake -DCMAKE_BUILD_TYPE=Release .. make cd ../..
|
安裝 brotli 壓縮
不需要請跳過,並在編譯時刪除–add-module=../ngx_brotli
1 2 3 4 5 6
| git clone --recurse-submodules -j8 https://github.com/google/ngx_brotli cd ngx_brotli/deps/brotli mkdir out && cd out cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF -DCMAKE_C_FLAGS="-Ofast -march=native -mtune=native -flto -funroll-loops -ffunction-sections -fdata-sections -Wl,--gc-sections" -DCMAKE_CXX_FLAGS="-Ofast -march=native -mtune=native -flto -funroll-loops -ffunction-sections -fdata-sections -Wl,--gc-sections" -DCMAKE_INSTALL_PREFIX=./installed .. cmake --build . --config Release --target brotlienc cd ../../../..
|
編譯安裝quic
注意:
本人是直接在 /root 目錄下編譯的,如果你在其他目錄下,請自行修改路徑;
如果你不需要 brotli 壓縮,請刪除–add-module=/root/ngx_brotli
本人將 Nginx 安裝在 /www/server/nginx 目錄下,如果你需要修改,請自行修改路徑;
1 2 3 4 5
| git clone https://github.com/nginx/nginx.git cd nginx ./auto/configure --user=www-data --group=www-data --prefix=/www/server/nginx --with-pcre --add-module=../ngx_brotli --with-http_v2_module --with-stream --with-stream_ssl_module --with-http_ssl_module --with-http_gzip_static_module --with-http_gunzip_module --with-http_sub_module --with-http_flv_module --with-http_addition_module --with-http_realip_module --with-http_mp4_module --with-ld-opt=-Wl,-E --with-cc-opt=-Wno-error --with-ld-opt=-ljemalloc --with-http_dav_module --with-http_v3_module --with-cc=c++ --with-cc-opt="-I../boringssl/include -x c" --with-ld-opt="-L../boringssl/build/ssl -L../boringssl/build/crypto" make make install
|
添加 www 用戶
大部分系統下默認存在着www-data
用戶組和www-data
用戶,如果沒有請執行以下命令添加。
1 2
| groupadd www-data useradd -g www-data -s /sbin/nologin www-data
|
添加進程管理
本人使用的是 systemd,如果你使用的是其他進程管理,請自行修改
1
| vim /usr/lib/systemd/system/nginx.service
|
輸入如下內容:
1 2 3 4 5 6 7 8 9 10 11 12 13
| [Unit] Description=nginx After=network.target
[Service] Type=forking ExecStart=/www/server/nginx/sbin/nginx ExecReload=/www/server/nginx/sbin/nginx -s reload ExecStop=/www/server/nginx/sbin/nginx -s quit PrivateTmp=true
[Install] WantedBy=multi-user.target
|
啓動
開機自啓
配置文件
示例配置文件如下,更多特性請參考官方文檔:https://nginx.org/en/docs/http/ngx_http_v3_module.html
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
| server { listen 443 ssl; listen [::]:443 ssl;
listen 443 quic reuseport; listen [::]:443 quic reuseport;
http2 on;
server_name 0517ht.com;
add_header Alt-Svc 'h3=":443"; ma=86400'; add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
location / { root /www/wwwroot/0517ht.com; index index.html index.htm; }
ssl_certificate /root/.acme.sh/smb.wiki/fullchain.cer; ssl_certificate_key /root/.acme.sh/smb.wiki/smb.wiki.key; ssl_session_timeout 5m; ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; ssl_prefer_server_ciphers on; }
|
配置完成後,重載 Nginx 即可生效