顾乔芝士网

持续更新的前后端开发技术栈

centos 生产环境安装 nginx,包含各种模块http3

企业级生产环境 Nginx 全模块构建 的大部分功能,包括 HTTP/2、HTTP/3、流媒体、SSL、缓存清理、负载均衡、DAV 扩展、替换过滤、静态压缩等。

下面我给出一个完整的生产环境安装流程(CentOS 7/8/9 兼容),并附上对应的 模块源码准备、编译、依赖安装、启动配置和优化建议


一、安装依赖环境

yum install -y gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel git wget unzip

二、创建源码目录

mkdir -p /www/server/nginx/src
cd /www/server/nginx/src

三、下载依赖源码包

# Nginx 主程序
wget https://nginx.org/download/nginx-1.25.5.tar.gz
tar zxvf nginx-1.25.5.tar.gz

# pcre(正则支持)
wget https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz
tar zxvf pcre-8.43.tar.gz

# openssl(支持TLS1.3与HTTP/3)
wget https://www.openssl.org/source/openssl-3.2.0.tar.gz
tar zxvf openssl-3.2.0.tar.gz -C /www/server/nginx/src/openssl --strip-components=1

# ngx_cache_purge 模块
git clone https://github.com/FRiCKLE/ngx_cache_purge.git

# sticky 模块(会话保持)
wget https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/master.tar.gz -O nginx-sticky-module-ng.tar.gz
tar zxvf nginx-sticky-module-ng.tar.gz
mv nginx-goodies-nginx-sticky-module-ng-* nginx-sticky-module-ng-1.3.0

# 替换过滤模块
git clone https://github.com/yaoweibin/ngx_http_substitutions_filter_module.git ngx_http_substitutions_filter_module-master

# DAV 扩展模块(WebDAV 支持)
git clone https://github.com/arut/nginx-dav-ext-module.git

四、编译 Nginx

cd /www/server/nginx/src/nginx-1.25.5

./configure \
--user=www \
--group=www \
--prefix=/www/server/nginx \
--add-module=/www/server/nginx/src/ngx_cache_purge \
--add-module=/www/server/nginx/src/nginx-sticky-module-ng-1.3.0 \
--add-module=/www/server/nginx/src/ngx_http_substitutions_filter_module-master \
--add-module=/www/server/nginx/src/nginx-dav-ext-module \
--with-openssl=/www/server/nginx/src/openssl \
--with-pcre=/www/server/nginx/src/pcre-8.43 \
--with-http_v2_module \
--with-http_v3_module \
--with-stream \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_image_filter_module \
--with-http_gzip_static_module \
--with-http_gunzip_module \
--with-http_sub_module \
--with-http_flv_module \
--with-http_addition_module \
--with-http_realip_module \
--with-http_mp4_module \
--with-http_auth_request_module \
--with-http_dav_module \
--with-ld-opt='-Wl,-E' \
--with-cc-opt='-Wno-error' \
--with-ipv6

编译并安装:

make -j$(nproc)
make install

五、创建 www 用户和目录

useradd -M -s /sbin/nologin www
mkdir -p /www/server/nginx/{logs,conf,html,temp}
chown -R www:www /www/server/nginx

六、配置 systemd 服务

创建服务文件:

vim /usr/lib/systemd/system/nginx.service

内容如下:

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

[Service]
Type=forking
PIDFile=/www/server/nginx/logs/nginx.pid
ExecStartPre=/www/server/nginx/sbin/nginx -t
ExecStart=/www/server/nginx/sbin/nginx
ExecReload=/www/server/nginx/sbin/nginx -s reload
ExecStop=/www/server/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

启动并开机自启:

systemctl daemon-reload
systemctl enable nginx
systemctl start nginx

七、验证安装

/www/server/nginx/sbin/nginx -V

输出中应包含:

--with-http_v3_module
--with-http_ssl_module
--add-module=...ngx_cache_purge
--add-module=...sticky
...

八、优化建议

优化方向

建议

worker配置

worker_processes auto;

连接优化

worker_connections 65535; use epoll;

日志分离

access、error日志按天切割

缓存优化

启用 proxy_cache_path 并结合 ngx_cache_purge 管理

TLS优化

推荐 TLSv1.2 TLSv1.3,并启用 http3(QUIC)

压缩

启用 gzip on; gzip_types text/css application/javascript image/svg+xml;

安全头

添加 add_header X-Frame-Options SAMEORIGIN; add_header X-Content-Type-Options nosniff;

模块扩展

若需 Brotli,可再加 --add-module=/path/to/ngx_brotli


九、HTTP/3 配置示例

在 nginx.conf 的 listen 处:

server {
    listen 443 ssl http2;
    listen 443 quic reuseport;

    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_certificate /etc/ssl/cert.crt;
    ssl_certificate_key /etc/ssl/cert.key;

    add_header Alt-Svc 'h3=":443"; h3-29=":443";';
    add_header QUIC-Status $quic;

    location / {
        root html;
        index index.html;
    }
}


脚本特点:

  • 自动安装依赖
  • 自动下载 Nginx + 所有模块源码
  • 自动编译(支持 HTTP/3、TLS1.3、缓存、粘性会话、替换过滤、DAV 等)
  • 自动创建 systemd 服务并开机自启
  • 自动检测并显示编译参数

一键安装脚本(nginx_install.sh)

#!/bin/bash
# =========================================================
#  CentOS 一键安装 Nginx (支持 HTTP/3 + 常用模块)
# 适配: CentOS 7 / 8 / 9
# 作者: ChatGPT 自动生成
# =========================================================

set -e

# ----------- 基础路径 -----------
NGINX_VERSION="1.25.5"
NGINX_PREFIX="/www/server/nginx"
SRC_DIR="${NGINX_PREFIX}/src"
USER="www"

echo "==== 安装依赖 ===="
yum install -y epel-release
yum install -y gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel git wget unzip

# ----------- 创建目录 -----------
mkdir -p ${SRC_DIR}
cd ${SRC_DIR}

# ----------- 下载源码包 -----------
echo "==== 下载 Nginx 源码及模块 ===="
[ ! -f nginx-${NGINX_VERSION}.tar.gz ] && wget https://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz
tar zxvf nginx-${NGINX_VERSION}.tar.gz

[ ! -f pcre-8.43.tar.gz ] && wget https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz
tar zxvf pcre-8.43.tar.gz

[ ! -d openssl ] && mkdir openssl && \
wget -O openssl.tar.gz https://www.openssl.org/source/openssl-3.2.0.tar.gz && \
tar zxvf openssl.tar.gz -C openssl --strip-components=1

[ ! -d ngx_cache_purge ] && git clone https://github.com/FRiCKLE/ngx_cache_purge.git
[ ! -d nginx-sticky-module-ng-1.3.0 ] && wget https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/master.tar.gz -O sticky.tar.gz && \
tar zxvf sticky.tar.gz && mv nginx-goodies-nginx-sticky-module-ng-* nginx-sticky-module-ng-1.3.0

[ ! -d ngx_http_substitutions_filter_module-master ] && git clone https://github.com/yaoweibin/ngx_http_substitutions_filter_module.git ngx_http_substitutions_filter_module-master
[ ! -d nginx-dav-ext-module ] && git clone https://github.com/arut/nginx-dav-ext-module.git

# ----------- 创建用户 -----------
if ! id -u ${USER} >/dev/null 2>&1; then
  useradd -M -s /sbin/nologin ${USER}
fi

# ----------- 编译安装 -----------
echo "==== 编译安装 Nginx ${NGINX_VERSION} ===="
cd ${SRC_DIR}/nginx-${NGINX_VERSION}

./configure \
--user=${USER} \
--group=${USER} \
--prefix=${NGINX_PREFIX} \
--add-module=${SRC_DIR}/ngx_cache_purge \
--add-module=${SRC_DIR}/nginx-sticky-module-ng-1.3.0 \
--add-module=${SRC_DIR}/ngx_http_substitutions_filter_module-master \
--add-module=${SRC_DIR}/nginx-dav-ext-module \
--with-openssl=${SRC_DIR}/openssl \
--with-pcre=${SRC_DIR}/pcre-8.43 \
--with-http_v2_module \
--with-http_v3_module \
--with-stream \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_image_filter_module \
--with-http_gzip_static_module \
--with-http_gunzip_module \
--with-http_sub_module \
--with-http_flv_module \
--with-http_addition_module \
--with-http_realip_module \
--with-http_mp4_module \
--with-http_auth_request_module \
--with-http_dav_module \
--with-ld-opt='-Wl,-E' \
--with-cc-opt='-Wno-error' \
--with-ipv6

make -j$(nproc)
make install

# ----------- 创建目录与权限 -----------
mkdir -p ${NGINX_PREFIX}/{logs,temp,conf,html}
chown -R ${USER}:${USER} ${NGINX_PREFIX}

# ----------- 创建 Systemd 服务 -----------
cat > /usr/lib/systemd/system/nginx.service <<EOF
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=${NGINX_PREFIX}/logs/nginx.pid
ExecStartPre=${NGINX_PREFIX}/sbin/nginx -t
ExecStart=${NGINX_PREFIX}/sbin/nginx
ExecReload=${NGINX_PREFIX}/sbin/nginx -s reload
ExecStop=${NGINX_PREFIX}/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable nginx
systemctl start nginx

echo
echo " Nginx 安装完成!"
echo " 安装路径: ${NGINX_PREFIX}"
echo " 查看版本与模块: ${NGINX_PREFIX}/sbin/nginx -V"
echo

使用方法

1 保存脚本:

vim nginx_install.sh

2 赋予执行权限:

chmod +x nginx_install.sh

3 运行安装:

./nginx_install.sh

4 验证:

/www/server/nginx/sbin/nginx -V
systemctl status nginx

可选增强模块(如需可加上)

模块

功能

安装方式

ngx_brotli

Brotli 压缩

git clone
https://github.com/google/ngx_brotli.git --recursive 并添加 --add-module=ngx_brotli

ngx_pagespeed

页面加速

不推荐生产使用

headers-more

更灵活添加 HTTP 头

git clone https://github.com/openresty/headers-more-nginx-module.git

lua-nginx-module

支持 Lua 脚本

需配合 OpenResty 构建


常见问题定位

问题

检查点

nginx: [emerg] unknown directive "..."

模块未编译或未加载

nginx: command not found

未安装或路径错误

nginx: [warn] module "..." version mismatch

模块版本与 Nginx 不匹配

启动失败

检查 /var/log/nginx/error.log


如果想查看完整模块清单

nginx -V 2>&1 | tr ' ' '\n' | grep -- '--with'

输出示例:

--with-http_ssl_module
--with-http_v2_module
--with-stream
--with-threads

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言