记录手动编译nginx以及HTTP/3配置过程
[TOC]
1. 准备编译环境依赖
apt update
apt install build-essential ca-certificates zlib1g-dev libpcre3 libpcre3-dev tar unzip libssl-dev wget curl git cmake ninja-build mercurial libunwind-dev pkg-config libjemalloc-dev
2. 编译QuicTLS或BoringSSL(可选)
想开启http/3 quic尝尝鲜,不需要此功能可以直接跳过
这里使用比较稳定的QuicTLS
2.1 下载源码包
wget https://github.com/quictls/openssl/archive/refs/tags/openssl-3.1.5-quic1.tar.gz && tar -xzf openssl-3.1.5-quic1.tar.gz && cd openssl-openssl-3.1.5-quic1
2.2 进行编译前配置
./config –prefix=$(pwd)/build no-shared
2.3 编译
make
2.4 安装
make install_sw
cd ..
!!如编译出错需要检查依赖版本是否正确,特别是通过apt或yum包管理器安装的依赖,apt仓库里的不一定是最新的,可能是很旧的版本!!
3. 编译brotli数据压缩模块(可选)
此模块不需要也可以直接跳过
3.1 下载源码包
git clone --recurse-submodules -j8 https://github.com/google/ngx_brotli && cd ngx_brotli/deps/brotli && mkdir out && cd out
3.2 编译前配置
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 ..
3.3 编译
cmake --build . --config Release --target brotlienc
cd ../../../../
!!如编译出错需要检查编译的依赖版本是否正确,特别是通过apt或yum包管理器安装的依赖,apt仓库里的不一定是最新的,可能是很旧的版本!!
如下图所示就是libunwind模块版本过低,该版本并不包含 UNW INIT SIGNAL FRAME 这个API,导致编译出错。
4. 安装Nginx
4.1 下载nginx源码包
使用最新的stable频道1.26.0稳定版
wget "http://nginx.org/download/nginx-1.26.0.tar.gz" && tar -xf nginx-1.26.0.tar.gz && cd nginx-1.26.0
4.2 编译前配置
模块请根据自己需求按需加载!
./configure
--user=www \
--group=www \
--prefix=/usr/local/nginx \
--add-module=../ngx_brotli \ #没有编译brotli模块请删除这行!!!
--with-pcre \
--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 \ #没有编译QuicTLS库请删除这行!!!
--with-cc-opt="-I../openssl-openssl-3.1.5-quic1/build/include" \ #没有编译QuicTLS库请删除这行!!!
--with-ld-opt="-L../openssl-openssl-3.1.5-quic1/build/lib64" #没有编译QuicTLS库请删除这行!!!
4.3 编译安装
make && make install
5. 添加 www 用户
groupadd www
useradd -g www -s /sbin/nologin www
6. 添加进程管理(Ubuntu)
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx #替换为实际nginx编译安装路径!编译前配置中的prefix参数定义了安装路径
ExecReload=/usr/local/nginx/sbin/nginx -s reload #替换为实际nginx编译安装路径!编译前配置中的prefix参数定义了安装路径
ExecStop=/usr/local/nginx/sbin/nginx -s quit #替换为实际nginx编译安装路径!编译前配置中的prefix参数定义了安装路径
PrivateTmp=true
[Install]
WantedBy=multi-user.target
6.1 原神 ~ 启动!
systemctl enable nginx && systemctl start nginx
7. Nginx配置
路径请替换为实际的路径
server {
listen 443 ssl;
listen [::]:443 ssl;
#开启Quic或HTTP/3支持,如果没有使用QuicTLS库要去掉quic、reuseport标识
listen 443 quic reuseport;
listen [::]:443 quic reuseport;
#开启HTTP/2支持
http2 on;
server_name www.innyang.cn;
root /xxx/public;
ssl_certificate /xxx/innyang.cn.pem; # 证书文件的路径
ssl_certificate_key /xxx/innyang.cn.key; # 私钥文件的路径
add_header Alt-Svc 'h3=":443";ma=86400,quic=":443"; ma=2592000; v="46,43"'; #如果没有使用QuicTLS库删掉此行
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 1d;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
ssl_protocols TLSv1.3 TLSv1.2 TLSv1.1;
ssl_stapling on;
ssl_stapling_verify on;
add_header Strict-Transport-Security "max-age=63072000" always;
client_max_body_size 75M;
#如果没有使用brotli模块删掉以下行 Start
brotli on; #启用
brotli_comp_level 6; #压缩等级,默认6,最高11,太高的压缩水平可能需要更多的CPU
brotli_min_length 1k; #指定压缩数据的最小长度,只有大于或等于最小长度才会对其压缩。这里指定1k
brotli_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/json image/svg application/font-woff application/vnd.ms-fontobject application/vnd.apple.mpegurl image/x-icon image/jpeg image/gif image/png image/bmp; #指定允许进行压缩类型
brotli_static always; #是否允许查找预处理好的、以.br结尾的压缩文件,可选值为on、off、always
brotli_window 512k; #窗口值,默认值为512
#没有编译brotli模块删掉以上行 End
location / {
root /xxx/public;
try_files $uri $uri/ /index.html;
index index.html;
}
error_log /xxx/error.log error;
}
8. Done !
此时站点可以正常访问了
但是QUIC还未生效,还需要两步小细节:
- QUIC协议是基于UDP通信的,服务器安全组需要开放443/UDP端口
- 浏览器需要开启QUIC支持,Chrome、Firefox、Edge浏览器需要手动开启,苹果的Safari浏览器默认开启无需手动启用。
以上完成后可以使用这两个网站检测HTTP/3启用状态,如下图显示表示成功启用。
HTTP/3 CHECK (国外)
HTTP/3 QUIC(国内)