在ondrej不维护Nginx仓库之后的无奈选择:自行编译Nginx以及第三方模块支持
前言
之前系统Nginx使用的是 Ondřej Surý in Launchpad维护的Nginx PPA仓库,但是前段时间正式停止维护了,主要原因是:Nginx PPA 维护成本太高 。
他在多次 Launchpad / GitHub / 邮件列表中提到过几个核心问题:
Nginx 官方更新频繁,且模块生态复杂
社区对 Nginx PPA 的期望过高,但贡献者极少
“大家都想要最新的 Nginx,但几乎没人愿意帮忙测试或提交补丁。”
换句话说: 伸手党太多,只想拿现成的,却没人愿意帮忙维护。
所以啊,现在大家喝不到汤了。他也将自己的重心放到PHP中去了。
版本
目前Nginx基本可以分为以下几类安装方式:
第一类:类似Ubuntu等官方维护的版本
版本非常稳定,但更新极慢
只会在 LTS 生命周期内做安全补丁
不追新功能
至今版本为 :1.24 。
第二类:Nginx官方仓库(官方源)
Nginx 官方自己提供 nginx.org 官方仓库,版本会非常新:
稳定版(stable)
主线版(mainline)
最新的版本。安装方式官方有Document:nginx: Linux packages。
官方仓库的最大痛点: 官方仓库只提供“纯净版 Nginx”,不包含任何第三方模块。
如果通过apt等形式安装的模块,无法使用。也就是说使用官方源安装Nginx,就基本上只使用自带的功能,告别第三方模块支持。
第三类:自行编译完整的Nginx
对于第三方模块,比如 brotli + fancyindex + headers-more + echo + stream 等等, 那么最干净的方式是就是自行编译!
模块全部内置或动态加载
与系统兼容
可用 apt 卸载
可用 systemd 管理
这是专业运维团队最常用的方式。
无奈的选择
正是由于上面所说,想要体验一下新功能(比如HTTP/3),或者自身需求(比如stram转发),基本只能自行编译Nginx这条路。
编译示例
下文演示的一些基础的需求,比如:
BoringSSL支持的HTTP/3
修改响应头的
headers-more-nginx-module比 gzip 更高效
BrotliWebDAV的
nginx-dav-ext-module目录支持的
fancyindex官方未编译进去的
stream模块
这些也是我平时会使用的,HTTP/3 用的少,感觉国内网络支持一般,其他几个常用,比如家里Ubuntu Server部署的WebDAV,文件浏览用 Fancyindex,Windows远程桌面使用 stream 转发。
第一步:编译环境的准备
sudo apt install -y git gcc make libpcre3 libpcre3-dev zlib1g-dev libssl-dev libxml2-dev libxslt1-dev libbrotli-dev libzstd-dev cmake g++ build-essential autoconf libtool pkg-config libpsl-dev
第二步:下载相关的源码及模块
直接在服务器中运行:
创建编译总目录
mkdir /root/nginx-build && cd nginx-build # 编译总目录下载nginx源码
wget https://nginx.org/download/nginx-<version>.tar.gz # 下载Nginx源码 tar -zxvf nginx-<version>.tar.gz # 解压nginx mv nginx-<version> nginx # 将nginx-<version> 重命名为 nginx
Nginx源码下载地址:[nginx: download](https://nginx.org/en/download.html)
下载第三方模块
git clone https://gitee.com/mirrors/boringssl.git # boringssl git clone https://github.com/openresty/headers-more-nginx-module.git # headers-more-nginx-module git clone --recurse-submodules -j8 https://github.com/google/ngx_brotli # brotli git clone https://github.com/aperezdc/ngx-fancyindex.git # ngx-fancyindex git clone https://github.com/arut/nginx-dav-ext-module.git # nginx-dav-ext-module
最终目录结构为:
/root/nginx-build
├── nginx/ # Nginx 源码目录
├── boringssl/ # BoringSSL
├── headers-more-nginx-module/ # headers-more 模块
├── ngx_brotli/ # brotli 模块
├── ngx-fancyindex/ # fancyindex 模块
└── nginx-dav-ext-module/ # WebDAV 扩展模块
第三步:编译相关模块
编译boringssl
cd boringssl mkdir build && cd build cmake .. -DCMAKE_BUILD_TYPE=Release make -j$(nproc) cd ../..编译brotli
cd ngx_brotli/deps/brotli mkdir out && cd out cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF -DCMAKE_C_FLAGS="-Ofast -m64 -march=native -mtune=native -flto -funroll-loops -ffunction-sections -fdata-sections -Wl,--gc-sections" -DCMAKE_CXX_FLAGS="-Ofast -m64 -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 ../../../..
第四步:编译Nginx
配置全局编译优化参数:
cd nginx
export CFLAGS="-m64 -march=native -mtune=native -Ofast -flto -funroll-loops -ffunction-sections -fdata-sections -Wl,--gc-sections"
export LDFLAGS="-m64 -Wl,-s -Wl,-Bsymbolic -Wl,--gc-sections"
执行配置脚本生成 Makefile:
./configure \
--prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/etc/nginx/modules \
--conf-path=/etc/nginx/nginx.conf \
--http-client-body-temp-path=/var/cache/nginx/client_body_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/run/nginx.lock \
--with-compat \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_v3_module \
--with-stream \
--with-stream_realip_module \
--with-stream_ssl_preread_module \
--with-http_dav_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-pcre \
--with-pcre-jit \
--with-mail \
--with-mail_ssl_module \
--with-cc-opt="-I/root/nginx-build/boringssl/include" \
--with-ld-opt="-L/root/nginx-build/boringssl/build -L/root/nginx-build/boringssl/build/crypto -lssl -lcrypto -lstdc++ -lpthread -lm" \
--add-module=/root/nginx-build/headers-more-nginx-module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_slice_module \
--with-http_auth_request_module \
--add-module="/root/nginx-build/ngx_brotli" \
--add-module="/root/nginx-build/ngx-fancyindex" \
--add-module="/root/nginx-build/nginx-dav-ext-module" \
--user=www-data \
--group=www-data \
--with-threads \
--with-file-aio \
--with-debug
注意其中的几个模块路径,不要错,而且要使用绝对路径。
接着:
make -j$(nproc)
make install
结束后可以通过以下几个命令确定是否成功:
ls /etc/nginx
/usr/sbin/nginx -V
第五步:配置systemd服务
vim /etc/systemd/system/nginx.service
内容为:
[Unit]
Description=A high performance web server and a reverse proxy server
Documentation=man:nginx(8)
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;'
ExecStartPost=/bin/sleep 0.1
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
[Install]
WantedBy=multi-user.target
先创建配置的临时目录:
mkdir -p /var/cache/nginx
再启动Nginx:
systemctl daemon-reload
systemctl enable nginx
systemctl start nginx
systemctl status nginx
搞定收工!
最后
编译完之后源码的目录就不能删除,之前通过 /usr/sbin/nginx -V 看到boringssl是动态链接 到源码库文件的,删除后会造成崩溃。
最终自己编译时间也不长,几分钟时间。
总体来说,使用包安装器安装比较稳定、省事,但是功能有限,自己编译可以达到系统的最优化,更多需求的模块功能,更新的版本、更高吞吐、更低CPU占用等优势。





