Quantcast
Channel: 小蓝博客
Viewing all articles
Browse latest Browse all 3155

CentOS 8 构建 Nginx1.27.1+BoringSSL+HTTP3+OpenResty

$
0
0

CentOS 8 上构建 Nginx 1.27.1 版本,并结合 BoringSSLHTTP/3 以及 OpenResty,可以增强服务器的安全性、性能和扩展性。本文将详细介绍该构建过程,包括依赖项的安装、源码编译以及配置。

1. 准备工作

1.1 更新系统并安装必要依赖

首先,确保系统是最新的,并安装编译 Nginx 和 OpenResty 所需的依赖项。

sudo dnf update -y
sudo dnf groupinstall "Development Tools" -y
sudo dnf install pcre pcre-devel zlib zlib-devel gcc gcc-c++ make git wget curl unzip libxslt-devel gd-devel perl perl-devel perl-ExtUtils-Embed -y

1.2 获取 Nginx 1.27.1 源码

下载 Nginx 1.27.1 版本源码:

cd /usr/local/src
wget https://nginx.org/download/nginx-1.27.1.tar.gz
tar -xzvf nginx-1.27.1.tar.gz
cd nginx-1.27.1

1.3 获取 BoringSSL 源码

BoringSSL 是 Google 维护的 SSL/TLS 库,它被设计为比 OpenSSL 更轻量和安全。下载并构建 BoringSSL:

cd /usr/local/src
git clone https://boringssl.googlesource.com/boringssl
cd boringssl
mkdir build
cd build
cmake ..
make
cd ..

1.4 获取 HTTP/3 和 QUIC 补丁

HTTP/3 和 QUIC 是下一代互联网协议,提供更快和更安全的连接。Nginx 支持 HTTP/3 需要一些额外的模块和补丁。我们将使用 cloudflare/quiche 库。

cd /usr/local/src
git clone --recursive https://github.com/cloudflare/quiche

1.5 获取 OpenResty 模块

OpenResty 是基于 Nginx 的高性能 Web 平台,它通过引入 Lua 扩展支持复杂的业务逻辑和 API。下载 OpenResty 模块:

cd /usr/local/src
wget https://openresty.org/download/openresty-1.21.4.1.tar.gz
tar -xzvf openresty-1.21.4.1.tar.gz
cd openresty-1.21.4.1

2. 编译 Nginx with BoringSSL、HTTP/3、OpenResty

2.1 编译配置 Nginx

在 Nginx 编译时,我们需要确保同时支持 BoringSSL、HTTP/3、QUIC 以及 OpenResty 模块。执行以下命令进行配置:

cd /usr/local/src/nginx-1.27.1

./configure \
--prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_v3_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-http_sub_module \
--with-http_addition_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_auth_request_module \
--with-http_secure_link_module \
--with-http_random_index_module \
--with-http_slice_module \
--with-stream \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-cc-opt='-I/usr/local/src/boringssl/include' \
--with-ld-opt='-L/usr/local/src/boringssl/build/ssl -L/usr/local/src/boringssl/build/crypto' \
--add-module=/usr/local/src/quiche \
--add-module=/usr/local/src/openresty-1.21.4.1/bundle/nginx-1.21.4

在这个配置中,我们启用了 HTTP/3、QUIC 支持,并加载了 OpenResty 模块。特别需要注意的是,--with-cc-opt--with-ld-opt 指定了 BoringSSL 的路径,确保 SSL 支持基于 BoringSSL。

2.2 编译和安装

完成配置后,使用以下命令编译和安装 Nginx:

make -j$(nproc)
sudo make install

-j$(nproc) 表示使用所有可用的 CPU 核心进行编译,以加快编译过程。

3. 配置 Nginx 支持 HTTP/3 和 BoringSSL

3.1 编辑 Nginx 配置文件

/etc/nginx/nginx.conf 中配置 Nginx 支持 HTTP/3 和 BoringSSL。修改或添加以下内容:

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    # HTTP/3 (QUIC) support
    listen 443 quic reuseport;
    listen [::]:443 quic reuseport;
  
    # SSL configuration
    ssl_certificate /etc/nginx/ssl/nginx.crt;
    ssl_certificate_key /etc/nginx/ssl/nginx.key;
    ssl_protocols TLSv1.3;
    ssl_prefer_server_ciphers off;
  
    # HTTP/3-specific configuration
    ssl_preread on;
    ssl_ciphers TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_128_GCM_SHA256;
    ssl_ecdh_curve X25519:P-256;
    add_header Alt-Svc 'h3-23=":443"; ma=86400'; # Advertise HTTP/3 support
    add_header QUIC-Status $quic;
  
    # OpenResty Lua module integration
    lua_package_path "/usr/local/share/lua/5.1/?.lua";
    lua_package_cpath "/usr/local/lib/lua/5.1/?.so";
  
    location / {
        root /usr/share/nginx/html;
        index index.html;
    }
}

3.2 创建 SSL 证书

如果尚未为服务器生成 SSL 证书,可以使用以下命令创建自签名证书:

sudo mkdir -p /etc/nginx/ssl
sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/nginx/ssl/nginx.key -out /etc/nginx/ssl/nginx.crt

按提示填写必要的信息。生成的证书仅用于测试,生产环境应使用经过认证的 SSL 证书。

3.3 配置防火墙

确保服务器的防火墙允许 HTTP 和 HTTPS 访问:

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

4. 启动 Nginx

完成所有配置后,启动或重启 Nginx 服务:

sudo systemctl start nginx

检查 Nginx 是否正确运行:

sudo systemctl status nginx

确认 Nginx 已成功启动,并且可以通过浏览器访问服务器来验证是否支持 HTTP/3。

5. 验证 HTTP/3 和 QUIC 支持

使用浏览器或命令行工具验证 HTTP/3 和 QUIC 是否成功启用。

5.1 使用 curl 验证

使用 curl 工具可以检查 HTTP/3 是否启用:

curl -I -k --http3 https://your_domain

如果 HTTP/3 成功启用,curl 应该返回包含 Alt-Svc 标头的响应。

5.2 使用浏览器验证

通过最新版本的 Chrome 或 Firefox 浏览器,打开浏览器开发者工具,检查 Network 选项卡中的 Protocol 列,确认网站是否通过 HTTP/3 协议加载。

6. 总结

本文详细介绍了在 CentOS 8 上构建 Nginx 1.27.1、集成 BoringSSLHTTP/3OpenResty 的完整流程。通过这些技术,您可以大幅提高网站的安全性和性能,同时支持最新的互联网协议和高效的 Lua 扩展。


Viewing all articles
Browse latest Browse all 3155

Trending Articles