拓冰建站拓冰建站
首页 / 资讯中心 / 正文

Nginx集群部署及运维

高可用负载均衡与反向代理集群 · 生产级部署指南版本:Nginx 1.26.x(Stable)方案:Keepalived + Nginx 双机热备一、什么是 Nginx1.1 概述Nginx(Engine-X)是一款开源的高性能 HTTP 服务器、反向代理服务器及邮件(IMAP/POP3/SMTP)代理服务器。由俄罗斯工程师 Igor Sysoev 于 2004 年首次发布,采用 C 语言编写,以高并发、低内存消耗、高稳定性著称。截至 2026 年,Nginx 是全球使用最广泛的 Web 服务器之一,市场份额超过 34%。1.2 核心功能功能分类能力说明典型场景HTTP 服务器静态文件服务、虚拟主机、HTTPS/TLS 终结静态网站托管、前端 SPA 部署反向代理请求转发、Header 改写、WebSocket 代理、gRPC 代理微服务网关、API 聚合负载均衡Round Robin / IP Hash / Least Connections / Random / 一致性哈希后端服务流量分发缓存加速磁盘级缓存、代理缓存、FastCGI 缓存CDN 边缘节点、API 响应缓存SSL/TLS证书管理、OCSP Stapling、HTTP/2、HTTP/3(QUIC)HTTPS 加密、证书卸载安全防护限速、限连接、IP 黑白名单、防盗链、WAF 集成DDoS 防护、API 限流流媒体MP4/FLV 流、HLS 切片代理、RTMP(三方模块)视频点播 / 直播TCP/UDP 代理四层 Stream 模块,支持 TCP/UDP 协议代理数据库代理、MQTT 代理1.3 Nginx vs 其他 Web 服务器特性NginxApache HTTPDHAProxyEnvoy并发模型事件驱动(epoll)多进程/多线程事件驱动事件驱动(非阻塞)静态文件性能★★★★★★★★★★★(不支持)★★★反向代理/LB★★★★★★★★★★★★★★★★★★动态模块支持Lua (OpenResty)原生 PHP/CGI有限脚本WASM/Lua配置复杂度低中中高(YAML/xDS)HTTP/3 支持✅(1.25+)实验性❌✅生态/社区非常活跃成熟成熟快速成长适用场景Web + LB + 网关传统 Web/PHP纯 L4/L7 LB云原生 Service Mesh💡 版本选择建议Mainline(主线版):包含最新特性,如 HTTP/3 完整支持,适合新项目尝鲜。Stable(稳定版):经过充分测试,适合生产环境。本手册基于Nginx 1.26.x Stable。OpenResty:基于 Nginx + LuaJIT,支持在 Nginx 内编写 Lua 脚本,适合需要动态逻辑的场景。如需复杂 WAF / API 网关能力可选用。二、基本原理与架构2.1 事件驱动架构Nginx 采用Master-Worker 多进程 + 事件驱动架构。Master 进程负责管理 Worker 进程、读取配置、绑定端口;Worker 进程处理实际的客户端请求,每个 Worker 使用非阻塞 I/O(Linux 上为 epoll)可同时处理数万个并发连接。↑ 客户端连接(HTTP/HTTPS/TCP/UDP)由 Worker 直接处理Shared Memory 跨 Worker 共享:缓存键、限速区、SSL 会话、upstream 状态进程角色说明进程数量职责Master1读取/验证配置、创建/管理 Worker、绑定监听端口、热升级(reload/upgrade)Worker= CPU 核数处理客户端请求、执行反向代理/负载均衡、读写缓存、SSL 卸载Cache Manager1周期性检查缓存条目,删除过期/超量缓存文件Cache Loader1(启动时)Nginx 启动时加载磁盘缓存元数据到共享内存,运行完成后退出2.2 请求处理流程Nginx 请求处理 11 个阶段(Phase)post-read → server-rewrite → find-config → rewrite → post-rewrite→ preaccess → access → post-access → precontent → content → log每个阶段由对应模块链依次执行,content 阶段产生响应,log 阶段记录访问日志2.3 反向代理与负载均衡原理Nginx 作为反向代理时,客户端请求先到达 Nginx,Nginx 根据配置的upstream块选择一台后端服务器转发请求,并将后端响应返回给客户端。整个过程对客户端透明。负载均衡算法对比算法指令原理适用场景轮询(默认)(无需指定)按顺序逐一分发请求后端性能均匀加权轮询weight=N按权重比例分发后端性能不均IP 哈希ip_hash按客户端 IP 哈希固定到某台后端Session 粘滞最少连接least_conn分发给当前连接数最少的后端长连接 / 请求耗时差异大一致性哈希hash $request_uri consistent按指定 Key 一致性哈希缓存命中率优化随机random随机选择后端简单场景 / 配合two方法2.4 健康检查机制Nginx 开源版的被动健康检查:当后端返回错误或超时(由proxy_next_upstream定义),Nginx 标记该后端为不可用,在fail_timeout时间内不再分发请求。主动健康检查需要nginx_upstream_check_module三方模块或 Nginx Plus。upstream backend { server 10.0.2.11:8080 max_fails=3 fail_timeout=30s; server 10.0.2.12:8080 max_fails=3 fail_timeout=30s; server 10.0.2.13:8080 max_fails=3 fail_timeout=30s; } # 被动健康检查参数: # max_fails=3 → 连续 3 次失败则标记为 down # fail_timeout=30s → 标记 down 后 30s 内不再分发,30s 后重试 # proxy_next_upstream → 哪些错误触发切换到下一台后端2.5 Keepalived + VRRP 高可用原理单台 Nginx 存在单点故障风险。生产环境通过Keepalived实现 Nginx 双机热备:两台 Nginx 服务器运行 Keepalived,通过VRRP(虚拟路由冗余协议)协商一个虚拟 IP(VIP)。MASTER 节点持有 VIP 并对外提供服务,BACKUP 节点持续监听 VRRP 心跳。当 MASTER 故障时,BACKUP 在秒级内接管 VIP,实现无缝切换。三、服务部署详细步骤3.1 集群规划本手册以2 节点 Keepalived + Nginx 高可用负载均衡集群为例,后端挂载 3 台应用服务器。节点IP 地址角色Keepalived 状态运行服务lb-0110.0.1.11Nginx 负载均衡MASTER (priority=100)Nginx + Keepalivedlb-0210.0.1.12Nginx 负载均衡BACKUP (priority=90)Nginx + KeepalivedVIP10.0.1.100虚拟 IP(浮动)——app-0110.0.2.11:8080后端应用—Tomcat / Node.js / Goapp-0210.0.2.12:8080后端应用—Tomcat / Node.js / Goapp-0310.0.2.13:8080后端应用—Tomcat / Node.js / Go端口规划端口用途说明80HTTP对外 HTTP 入口(重定向到 443)443HTTPSTLS 终结 + 反向代理8080后端应用后端服务监听端口10050Prometheus Metricsstub_status / vts 指标暴露112VRRP 协议Keepalived 心跳(协议号,非端口)3.2 环境准备(两台 LB 节点均执行)3.2.1 系统依赖安装# CentOS / RHEL / Rocky Linux yum install -y gcc gcc-c++ make pcre pcre-devel zlib zlib-devel \ openssl openssl-devel libxml2 libxml2-devel libxslt libxslt-devel \ gd gd-devel GeoIP GeoIP-devel perl-devel perl-ExtUtils-Embed # Ubuntu / Debian apt update apt install -y build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev \ libssl-dev libxml2-dev libxslt1-dev libgd-dev libgeoip-dev \ libperl-dev libpam0g-dev3.2.2 创建 Nginx 运行用户groupadd -r nginx useradd -r -g nginx -s /sbin/nologin -d /var/cache/nginx -M nginx # 创建必要目录 mkdir -p /var/cache/nginx/{client_temp,proxy_temp,fastcgi_temp,uwsgi_temp,scgi_temp} mkdir -p /var/log/nginx mkdir -p /etc/nginx/{conf.d,ssl} mkdir -p /var/www/html chown -R nginx:nginx /var/cache/nginx /var/log/nginx /var/www/html3.2.3 系统参数优化# 编辑 /etc/sysctl.conf,追加以下内容 cat /etc/sysctl.conf 'EOF' # ===== Nginx 性能优化 ===== # 最大文件描述符数 fs.file-max = 1048576 # TCP 连接相关 net.core.somaxconn = 65535 net.core.netdev_max_backlog = 65535 net.ipv4.tcp_max_syn_backlog = 65535 net.ipv4.tcp_max_tw_buckets = 1048576 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_fin_timeout = 15 net.ipv4.tcp_keepalive_time = 600 net.ipv4.tcp_keepalive_intvl = 30 net.ipv4.tcp_keepalive_probes = 3 # 允许绑定非本地 IP(Keepalived VIP 漂移所需) net.ipv4.ip_nonlocal_bind = 1 # TCP 缓冲区 net.ipv4.tcp_rmem = 4096 87380 16777216 net.ipv4.tcp_wmem = 4096 65536 16777216 net.core.rmem_max = 16777216 net.core.wmem_max = 16777216 EOF # 生效 sysctl -p # 文件描述符限制 cat /etc/security/limits.conf 'EOF' nginx soft nofile 1048576 nginx hard nofile 1048576 root soft nofile 1048576 root hard nofile 1048576 * soft nproc 65535 * hard nproc 65535 EOF # 临时生效 ulimit -n 10485763.3 Nginx 安装方式一:源码编译安装(推荐,可定制模块)# 下载 Nginx 源码 cd /usr/local/src wget https://nginx.org/download/nginx-1.26.2.tar.gz tar -xzf nginx-1.26.2.tar.gz cd nginx-1.26.2 # 配置编译参数(包含常用模块) ./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 \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/var/run/nginx.pid \ --lock-path=/var/run/nginx.lock \ --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 \ --user=nginx \ --group=nginx \ --with-compat \ --with-file-aio \ --with-threads \ --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_random_index_module \ --with-http_secure_link_module \ --with-http_degradation_module \ --with-http_slice_module \ --with-http_stub_status_module \ --with-stream \ --with-stream_ssl_module \ --with-stream_realip_module \ --with-stream_ssl_preread_module \ --with-mail \ --with-mail_ssl_module \ --with-pcre-jit \ --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2' # 编译安装 make -j$(nproc) make install # 验证版本 nginx -V nginx -v # 预期输出: # nginx version: nginx/1.26.2 # built by gcc ... # built with OpenSSL ... # TLS SNI support enabled # configure arguments: --prefix=/etc/nginx ...(上述配置参数)方式二:官方 yum 仓库安装(快速部署)# 添加 Nginx 官方仓库 cat /etc/yum.repos.d/nginx.repo 'EOF' [nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basear
分享:

看完干货,该让你的企业上线了

免费需求沟通 · 48 小时内出具建站方案 · 河南本地可上门