编译安装 Nginx 以支持 QUIC

简介

由于 OpenSSL 暂时不支持 QUIC 协议,我们需要使用 Google/BoringSSL 支持 QUIC 协议。

准备

本文使用的 Linux 发行版为 CentOS Stream 8。

安装 Golang 及其他编译依赖

sudo dnf install -y golang zlib-devel pcre-devel libunwind-devel gcc gcc-c++ cmake mercurial psmisc
sudo dnf group install -y "Development Tools"

拉取 nginx-quicBoringSSL 的源码

cd ~
sudo dnf install -y git hg
hg clone -b quic https://hg.nginx.org/nginx-quic
git clone https://boringssl.googlesource.com/boringssl

编译

国内无法访问 Go ,需要使用 GOPROXY.IO 代理进行编译。

export GOPROXY=https://goproxy.io,direct
export GOPRIVATE=git.mycompany.com,github.com/my/private

BoringSSL

⚠ 注意:nginx 的 OCSP Stapling 是由 OpenSSL 实现,使用 BoringSSL 会导致该功能无法使用。实际使用体验上来说感觉无伤大雅。

cd ~/boringssl
mkdir build
cd build
cmake ..
make

nginx-quic

cd ~/nginx-quic
./auto/configure --prefix=/etc/nginx \
            --sbin-path=/usr/sbin/nginx \
            --modules-path=/usr/lib64/nginx/modules \
            --conf-path=/etc/nginx/nginx.conf \
            --error-log-path=/var/log/nginx/error.log \
            --pid-path=/var/run/nginx.pid \
            --lock-path=/var/run/nginx.lock \
            --user=nginx \
            --group=nginx \
            --with-select_module \
            --with-poll_module \
            --with-threads \
            --with-file-aio \
            --with-http_ssl_module \
            --with-http_v2_module \
            --with-http_v3_module \
            --with-http_realip_module \
            --with-http_addition_module \
            --with-http_xslt_module=dynamic \
            --with-http_image_filter_module=dynamic \
            --with-http_geoip_module=dynamic \
            --with-http_sub_module \
            --with-http_dav_module \
            --with-http_flv_module \
            --with-http_mp4_module \
            --with-http_gunzip_module \
            --with-http_gzip_static_module \
            --with-http_auth_request_module \
            --with-http_random_index_module \
            --with-http_secure_link_module \
            --with-http_degradation_module \
            --with-http_slice_module \
            --with-http_stub_status_module \
            --http-log-path=/var/log/nginx/access.log \
            --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 \
            --with-mail=dynamic \
            --with-mail_ssl_module \
            --with-stream=dynamic \
            --with-stream_ssl_module \
            --with-stream_realip_module \
            --with-stream_geoip_module=dynamic \
            --with-stream_ssl_preread_module \
            --with-compat \
            --with-cc-opt="-I../boringssl/include" \
            --with-ld-opt="-L../boringssl/build/ssl -L../boringssl/build/crypto" \
            --with-debug
make
make install

设置

nginx 配置文件的目录位于 /etc/nginx/

发现了一个非常好用的 nginx 配置生成器 NGINXConfig | DigitalOcean,可以使用它生成配置,按照网站上的指引进行操作,本文不再赘述。

需要注意的是,生成的配置并不可以直接使用。需要在网站的 server{} 块中启用 0-RTT 并加入响应标头

ssl_early_data on;
add_header Alt-Svc 'h3=":443"; ma=86400';

为便于管理 nginx , 我们可以将 nginx 加入 systemd 。

将以下内容保存到 /lib/systemd/system/nginx.service

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

重新加载 systemd

systemctl daemon-reload

配置完成后即可使用 systemd 来管理 nginx。

参考

  1. Building BoringSSL
  2. nginx-quic README
  3. NGINX systemd service file | NGINX
  4. 在Nginx中支持HTTP3.0/QUIC


已发布

分类

作者:

评论

《“编译安装 Nginx 以支持 QUIC”》 有 8 条评论

  1. Recomandare Binance 的头像

    Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me?

  2. binance US-registrera 的头像

    Your article helped me a lot, is there any more related content? Thanks!

  3. Cel mai bun cod de recomandare Binance 的头像

    Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me?

  4. Λογαριασμ Binance 的头像

    Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.

  5. temp .mail 的头像

    Temp mail This is my first time pay a quick visit at here and i am really happy to read everthing at one place

  6. ^Inscrieti-va pentru a obtine 100 USDT 的头像

    Your article helped me a lot, is there any more related content? Thanks!

  7. Ymrjch 的头像

    cost lasuna – purchase himcolin generic himcolin

  8. b^onus de indicac~ao binance 的头像

    I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article.

发表回复