其他分类 · 27 1 月, 2026 0

手把手带你编译满血版Nginx

前言

用系统自带的包管理工具安装Nginx大家应该都会。但是这样安装的Nginx一般都是阉割版,缺乏诸如stream、webdav等模块。如果想用这些模块需要自己编译Nginx的源码。

我知道有些人一听说要自己编译就头大。别慌,这里就带你编译出满血版Nginx。除通过yum安装的Nginx已有的功能外,还包含以下功能:

  • Stream转发功能
  • WebDAV服务器
  • 使用OpenSSL 3.3.1 ※仅限Nginx 1.25以上版本

什么,你想用现成的?那可以 下载这个 已经用下面方法编译好的二进制文件。适用于x86/64的Linux系统。

马上开始!

本人使用的操作系统为已经作古的CentOS 7。

1. 安装编译工具

执行以下命令,安装编译Nginx所需的工具:

yum groupinstall -y "Development Tools"
yum install -y pcre pcre-devel zlib zlib-devel openssl openssl-devel
yum install -y perl-IPC-Cmd

如果你使用CentOS 7发现yum无法使用,请参考这篇文章解决。

2. 下载源码

首先,理所当然地下载最新版的Nginx源码并解压:

wget http://nginx.org/download/nginx-1.29.4.tar.gz
tar zxvf nginx-1.29.4.tar.gz

然后,下载OpenSSL 3.3.1和PCRE 2源码并解压:

# OpenSSL 3.3.1
wget https://www.openssl.org/source/openssl-3.3.1.tar.gz
tar zxvf openssl-3.3.1.tar.gz
# PCRE 2
wget https://github.com/PCRE2Project/pcre2/releases/download/pcre2-10.47/pcre2-10.47.tar.gz
tar zxvf pcre2-10.47.tar.gz
# WebDAV扩展
git clone https://github.com/arut/nginx-dav-ext-module.git

确保当前目录下有以下三个文件夹:

nginx-1.29.4/
openssl-3.3.1/
pcre2-10.47/
nginx-dav-ext-module/

3. 编译Nginx

进入Nginx源码目录,并执行configure

cd ./nginx-1.29.4
./configure \
--with-http_ssl_module \
--with-stream \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-http_dav_module \
--with-file-aio \
--with-threads \
--with-http_v2_module \
--with-openssl=../openssl-3.3.1 \
--with-openssl-opt=enable-ec_nistp_64_gcc_128 \
--with-http_realip_module \
--with-http_gzip_static_module \
--with-compat \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_degradation_module \
--with-http_gunzip_module \
--with-http_mp4_module \
--with-pcre=../pcre2-10.47 \
--add-module=../nginx-dav-ext-module \
# 以下内容为确保编译版Nginx与yum版参数一致,以便无缝替代。
# 不写的话编译版Nginx启动时会从默认路径读写设置、日志等内容。
# 如果文件原先不存在,启动就会报错。
--prefix=/usr/share/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 \
--http-log-path=/var/log/nginx/access.log \
--http-client-body-temp-path=/var/lib/nginx/tmp/client_body \
--http-proxy-temp-path=/var/lib/nginx/tmp/proxy \
--http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi \
--http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi \
--http-scgi-temp-path=/var/lib/nginx/tmp/scgi \
--pid-path=/run/nginx.pid \
--lock-path=/run/lock/subsys/nginx \
--user=nginx \
--group=nginx

开始编译!

# 为了使用多核CPU提升性能,这里使用了-j$(nproc)参数。
make -j$(nproc)

这里我们只编译不安装。如果需要安装的话请继续执行sudo make install。Nginx会被安装在--prefix指定的目录下。

4. 查看编译结果

编译好的Nginx二进制文件保存在objs/目录中。用./objs/nginx -V查看编译好的Nginx版本信息,能看到:

nginx version: nginx/1.29.4
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 3.3.1 4 Jun 2024
TLS SNI support enabled
configure arguments: --with-http_ssl_module --with-stream --with-stream_ssl_module --with-http_dav_module --with-file-aio --with-threads --with-http_v2_module --with-openssl=../openssl-3.3.1 --with-openssl-opt=enable-ec_nistp_64_gcc_128 --with-http_realip_module --with-http_gzip_static_module --with-compat --with-http_addition_module --with-http_auth_request_module --with-http_degradation_module --with-http_gunzip_module --with-http_mp4_module --with-pcre=../pcre2-10.47 --add-module=./nginx-dav-ext-module --prefix=/usr/share/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 --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx

5. 替代YUM/DNF版Nginx(可选)

如果你之前已经用yumdnf安装过Nginx,那么可通过以下方法实现无缝替代:

  • 停止正在运行的Nginx服务
systemctl stop nginx
  • 替换旧的二进制文件并启动新版Nginx
# 以下使用典型的安装位置。实际位置建议事先使用`which nginx`确认。
mv /usr/sbin/nginx /usr/sbin/nginx.old
cp ./objs/nginx /usr/sbin/nginx
systemctl start nginx

如果没有报错则大功告成~🎉

一些issue

缺少libxslt

如果你把编译好的Nginx移植到其他主机,发现启动失败,报错:

error while loading shared libraries: libxslt.so.1

那么你需要用yum install libxslt来安装XSLT动态库。这个库的作用是在Nginx中将XML转化为HTML等其他格式。

注意,如果是编译过程中报错提示缺少libxslt模块,此时需要安装的是libxslt-devel

缺少Perl标准模块

如果在编译中报错:

Can't locate FindBin.pm in @INC (you may need to install the FindBin module)

说明当前编译环境缺少FindBin.pm(Perl标准模块)。解决方法:安装所需模块:

install perl perl-core perl-Module-CoreList