Nginx 源码编译并启用 HTTP/3

Nginx 1.25.0 实验性地将 HTTP/3 的支持合并到 mainline 中,现在可以通过 --with-http_v3_module 参数启用该功能。本文将带你从源码开始编译一个可用于学习环境的 Nginx,并启用 HTTP/3。


系统准备

在阅读本文之前,你至少应具备基础的 Linux 指令知识,了解 Linux 环境下的文件系统、文件权限等,并熟悉 GNU 编译器套件。如果你有使用过面板的经验,现在想从零开始手动搭建环境,那么本文非常适合你。

本系列的工作基于 Ubuntu 22.04.3 LTS版本。你可以选择在物理机上安装,使用虚拟机,或者直接使用云服务器。

Nginx 编译步骤

  1. 建立新用户
    首先,创建一个专门用于 Nginx 的用户:

    1
    sudo useradd -M -r -s /bin/false -c 'Web server' www-date
  2. 创建目录并赋予权限
    建立日志、缓存、配置文件等目录:

    1
    mkdir /var/log/nginx /var/cache/nginx
  3. 更新系统并安装编译器
    更新系统并安装必要的编译工具:

    1
    2
    sudo apt update
    sudo apt install build-essential
  4. 安装开发库
    安装必要的开发库,包括 pcre2zlibopenssl。其中,pcre2zlib 可通过包管理器安装,openssl 使用最新版源码:

    1
    sudo apt install libpcre2-dev zlib1g-dev
  5. 设置工作环境
    /usr/local 目录下创建工作目录:

    1
    mkdir /usr/local/src && cd /usr/local/src
  6. 下载 OpenSSL 源码
    下载 OpenSSL 并解压上传服务器对应目录解压:

    1
    tar zxf openssl-3.5.0.tar.gz
  7. 下载 Nginx 源码
    前往nginx下载并对应合适版本的Nginx 源码(下面以1.28.0为例子)并解压:

    1
    2
    wget https://nginx.org/download/nginx-1.28.0.tar.gz
    tar zxf nginx-1.28.0.tar.gz
  8. 配置 Nginx 编译选项
    进入 Nginx 源码目录,配置编译选项并启用 HTTP/3 模块:

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
cd nginx-1.28.0
./configure --with-openssl=/usr/local/src/openssl-3.5.0 \
--prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib/nginx/modules \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--user=www-date --group=www-date \
--with-compat --with-file-aio --with-threads \
--with-http_addition_module --with-http_auth_request_module \
--with-http_flv_module \
--with-http_gunzip_module --with-http_gzip_static_module \
--with-http_mp4_module --with-http_random_index_module \
--with-http_realip_module --with-http_secure_link_module \
--with-http_slice_module --with-http_ssl_module \
--with-http_stub_status_module --with-http_sub_module \
--with-http_v2_module --with-http_v3_module \
--with-mail --with-mail_ssl_module --with-stream \
--with-stream_realip_module --with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-http_dav_module
  1. 编译并安装
    根据 CPU 核心数进行多线程编译:

    1
    2
    make -j2
    sudo make install
  2. 创建 systemd 服务文件
    为 Nginx 添加服务配置,便于系统管理:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
cat > /etc/systemd/system/nginx.service << EOF
[Unit]
Description=nginx - high performance web server
Documentation=https://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
TimeoutStopSec=5
KillMode=mixed
PrivateTmp=true
LimitNOFILE=65535

[Install]
WantedBy=multi-user.target
EOF
  1. 检查版本
    安装完成后,检查 Nginx 版本:
1
nginx -V

如果输出版本信息(如 nginx/1.28.0)即表示安装成功。

  1. 启动 Nginx 服务
    启动并设置开机启动:
1
2
sudo systemctl daemon-reload
sudo systemctl enable nginx.service --now

其他管理命令包括:

1
2
3
sudo systemctl disable nginx.service   # 关闭开机启动
sudo systemctl stop nginx.service # 停止服务
sudo systemctl status nginx.service # 查看服务状态

虚拟主机配置

  1. 创建虚拟主机配置目录
    创建配置文件存放目录:

    1
    mkdir /etc/nginx/conf.d
  2. 修改主配置文件
    /etc/nginx/nginx.conf 中的 http 模块部分添加:

    1
    2
    3
    4
    5
    6
    7
    http {
    server {
    # 默认虚拟主机配置
    }

    include /etc/nginx/conf.d/*.conf; # 包含所有虚拟主机配置
    }

通过这种方式,你可以为每个网站创建独立的虚拟主机配置文件,详细配置会在后续文章中讲解。


通过以上步骤,你就可以成功在 Ubuntu 系统上编译并安装 Nginx,启用 HTTP/3,并配置 Nginx 服务。