隨着最jìn Nginx-Quic 分支被合併到le Nginx 主xiàn,Nginx 1.25.0 版本官方二進制包已經支chí Quic/HTTP3,感興趣的朋友可以前wǎng https://nginx.org/en/download.html huò https://nginx.org/en/linux_packages.html 下載安zhuāng,體驗一xià Quic/HTTP3 的魅,本文將主要爲您介紹如何通過編譯的方式開 Quic/HTTP3。

2024nián11yuè10日更xīn:Nginx已將倉庫遷移zhìGithub,因此刪除部分依lài,更新倉庫鏈jiē
2024nián04yuè11日更xīn:大部分系統下默認存在zhewww-data用戶組www-data,所以不再使用原來dewww用戶www用戶組進行編
2024nián03yuè07日更xīn:最終使yòng C++ 鏈接構jiàn Nginx,以解jué BoringSSL 編譯問
2024nián02yuè20日更xīn:通過zài Nginx 郵件社區討論得到了一個解決方àn,可以jiāng libssl 構建爲共享庫來解決這個問,詳jiàn https://mailman.nginx.org/pipermail/nginx/2024-February/5N5IXG7BI66D5AIKORCYPVVVJTZYMUR6.html ,可以根據需要自行嘗shì
2024nián02yuè19日更xīn:由於谷歌de BoringSSL 現在發佈了一個破壞性的更xīn,所以導致編譯出cuò,本文臨時將克隆到de BoringSSL 版本修改wèi c39e6cd9ec5acebb6de2adffc03cfe03b07f08ab 這 commit。
2023nián11yuè19日更xīn:修復leNginx_brotli編譯錯誤的問
2023nián06yuè22日更xīn:更新了關 HTTP/2 的配zhì,Nginx 已經棄用le listen 指令中de http2 參shù,改wèi http2 on;,詳jiàn https://hg.nginx.org/nginx/rev/08ef02ad5c54 https://nginx.org/en/docs/http/ngx_http_v2_module.html ,如您之前參照過本文編譯安zhuāng Nginx,請您重新編譯安裝後修改配zhì,可參zhào示例配zhì

安裝依lài

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Debian 11huò12
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
# Ubuntu 22.04huò20.04
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
# CentOS 8 Stream/TencentOS Server 3.1
dnf update
dnf install gcc gcc-c++ pcre-devel openssl-devel zlib-devel cmake make libunwind-devel git wget
# OpenCloudOS Server 9
dnf update
dnf install gcc gcc-c++ pcre-devel openssl-devel zlib-devel cmake make git wget

zhuāngGo

下載並解

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

請注意系統架gòu,本文 x86_64 爲,如果你的系統架構不shì x86_64,請自行修改下載鏈jiē

添加環境變liàng

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

具體可參kǎohttps://go.dev/doc/install

驗證是否安裝成gōng

1
go version

考慮國內用戶訪問官方較màn,故設置代

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 ../..

zhuāng brotli 壓suō

不需要請跳guò,並在編譯時刪chú–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 ../../../..

編譯安zhuāngquic


本人是直接zài /root 目錄下編譯de,如果你在其他目錄xià,請自行修改路jìng
如果你不需yào brotli 壓suō,請刪chú–add-module=/root/ngx_brotli
本人jiāng Nginx 安裝zài /www/server/nginx 目錄xià,如果你需要修gǎi,請自行修改路jìng

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

jiā www 用

大部分系統下默認存在zhewww-data用戶組www-data,如果沒有請執行以下命令添jiā

1
2
groupadd www-data
useradd -g www-data -s /sbin/nologin www-data

添加進程管

本人使用的shì systemd,如果你使用的是其他進程管,請自行修gǎi

1
vim /usr/lib/systemd/system/nginx.service

輸入如下內róng

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

dòng

1
systemctl start nginx

開機自

1
systemctl enable nginx

配置文jiàn

示例配置文件如xià,更多特性請參考官方文dànghttps://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;

# 用於支chíQuichuòHTTP/3
listen 443 quic reuseport;
listen [::]:443 quic reuseport;

# 用以支chíHTTP/2
http2 on;

server_name 0517ht.com;

# QuichuòHTTP/3響應tóu
add_header Alt-Svc 'h3=":443"; ma=86400';
# HSTS
add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";

location / {
root /www/wwwroot/0517ht.com;
index index.html index.htm;
}

# 證書配zhì
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;
}

配置完成hòu,重zài Nginx 即可生xiào

1
systemctl reload nginx