一、Web 服务介绍
1.1 Apache 模型
1.1.1 prefork 模型(预派生模式)
- 核心机制:主控制进程派生多个独立子进程,使用
select模型,最大并发 1024;每个子进程单线程响应用户请求 - 资源特性:占用内存较多,但稳定性极高
- 配置特点:可设置进程数的最大值和最小值
- 适用场景:访问量中等的场景
- 优缺点
- ✅ 优点:极致稳定,故障隔离性好
- ❌ 缺点:每个请求对应一个进程,资源占用高,并发能力弱,不适合高并发场景
Web 服务核心模型(Apache prefork/worker/event 及 Nginx),详细解析了五种网络 I/O 模型(阻塞、非阻塞、信号驱动、异步、多路复用)的区别与应用场景。重点阐述了零拷贝技术原理及优化手段。最后提供了 Nginx 源码编译安装、平滑升级回滚、配置优化(进程数、CPU 绑定、句柄限制)等实战操作指南,帮助读者掌握高性能 Web 服务器部署与维护技能。
select 模型,最大并发 1024;每个子进程单线程响应用户请求epoll 事件驱动;每个进程响应多个请求,专门线程管理 keepalive 连接read 系统调用发起 I/O 读操作,从用户态切换到内核态read 完成后才能处理数据EWOULDBLOCK 错误,数据就绪后拷贝到进程缓冲区sigaction 系统调用注册信号处理回调函数,调用立即返回,主程序继续执行SIGIO 信号,触发回调函数recvfrom 将数据从内核拷贝到用户空间aio_read 后立即返回,可执行其他操作select/poll/epoll 系统调用实现select,等待其返回select 返回,用户线程调用 read 读取数据select 调用上,而非实际 IO 操作select,worker 用 pollselect,但可同时监控多个 IO,相比单线程单 IO 更高效| 模型类型 | 阻塞特性 | 同步 / 异步 | 核心特点 |
|---|---|---|---|
| 阻塞 IO | 全程阻塞 | 同步 | 简单但并发差 |
| 非阻塞 IO | 非阻塞(轮询) | 同步 | 耗 CPU,极少单独使用 |
| 信号驱动 IO | 等待非阻塞 | 同步 | 信号队列易溢出 |
| 异步 IO | 全程非阻塞 | 异步 | 性能最优,实现复杂 |
| 多路复用 IO | 阻塞在 select | 同步 | 高并发主流方案,异步阻塞 |
注:前四种均为同步 IO(
recvfrom会阻塞),仅异步 IO 符合 POSIX 异步定义
# 下载软件包
[root@Nginx ~]# wget https://nginx.org/download/nginx-1.28.1.tar.gz
# 解压
[root@Nginx ~]# tar zxf nginx-1.28.1.tar.gz
[root@Nginx ~]# cd nginx-1.28.1/
[root@Nginx nginx-1.28.1]# ls
# 验证解压结果
auto CHANGES.ru conf contrib html man SECURITY.md
CHANGES CODE_OF_CONDUCT.md configure CONTRIBUTING.md LICENSE README.md src
# 安装依赖
[root@Nginx ~]# dnf install gcc openssl-devel.x86_64 pcre2-devel.x86_64 zlib-devel -y
# 配置编译参数
[root@Nginx nginx-1.28.1]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module
# 编译
[root@Nginx nginx-1.28.1]# make
# 安装
[root@Nginx nginx-1.28.1]# make install
# 设置环境变量
[root@Nginx sbin]# vim ~/.bash_profile
export PATH=$PATH:/usr/local/nginx/sbin # 添加此行
[root@Nginx sbin]# source ~/.bash_profile # 生效
# 创建 nginx 用户
[root@Nginx logs]# useradd -s /sbin/nologin -M nginx
# 启动 nginx
[root@Nginx logs]# nginx
# 验证启动
[root@Nginx logs]# ps aux | grep nginx
root 44012 0.0 0.1 14688 2356 ? Ss 17:01 0:00 nginx: master process nginx
nginx 44013 0.0 0.2 14888 3892 ? S 17:01 0:00 nginx: worker process
# 测试访问
[root@Nginx logs]# echo timinglee > /usr/local/nginx/html/index.html
[root@Nginx logs]# curl 172.25.254.100
# 预期输出:timinglee
[root@Nginx ~]# vim /lib/systemd/system/nginx.service
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
# 重载配置
[root@Nginx ~]# systemctl daemon-reload
# 设置开机自启并启动
[root@Nginx ~]# systemctl enable --now nginx
# 验证状态
[root@Nginx ~]# systemctl status nginx.service
# 下载高版本
[root@Nginx ~]# wget https://nginx.org/download/nginx-1.29.4.tar.gz
# 解压并修改版本信息(隐藏版本)
[root@Nginx ~]# tar zxf nginx-1.29.4.tar.gz
[root@Nginx ~]# cd nginx-1.29.4/src/core/
[root@Nginx core]# vim nginx.h
#define nginx_version 1029004
#define NGINX_VERSION ""
#define NGINX_VER "TIMINGLEE/" NGINX_VERSION
# 编译(参数与旧版本一致)
[root@Nginx core]# cd ../../
[root@Nginx nginx-1.29.4]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module
[root@Nginx nginx-1.29.4]# make # 仅编译,不安装
# 替换二进制文件
[root@Nginx nginx-1.29.4]# cd objs/
[root@Nginx objs]# cp -f nginx /usr/local/nginx/sbin/nginx
# 查看旧 master 进程 ID
[root@Nginx sbin]# ps aux | grep nginx
root 1643 0.0 0.1 14688 2360 ? Ss 09:55 0:00 nginx: master process /usr/local/nginx/sbin/nginx
# 发送 USR2 信号启动新 master 进程
[root@Nginx sbin]# kill -USR2 1643
# 验证双 master 进程
[root@Nginx sbin]# ps aux | grep nginx
root 1643 0.0 0.1 14688 2744 ? Ss 09:55 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 1644 0.0 0.2 14888 3896 ? S 09:55 0:00 nginx: worker process
root 4919 0.0 0.4 14716 7936 ? S 10:24 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx 4921 0.0 0.2 14916 4156 ? S 10:24 0:00 nginx: worker process
# 验证版本
[root@Nginx sbin]# nginx -V
nginx version: TIMINGLEE/ # 已升级为新版本
# 回收旧版本 worker 进程
[root@Nginx sbin]# kill -WINCH 1643
# 备份新版本二进制文件
[root@Nginx sbin]# cp nginx nginx.new -p
# 恢复旧版本二进制文件(需提前备份)
[root@Nginx sbin]# cp nginx.old nginx -pf
# 发送 HUP 信号重启旧 master 进程
[root@Nginx sbin]# kill -HUP 1643
# 验证回滚结果
[root@Nginx sbin]# nginx -V
nginx version: nginx/1.28.1 # 回滚至旧版本
# 回收新版本 master 进程
[root@Nginx sbin]# kill -WINCH 4919
[root@Nginx ~]# vim /usr/local/nginx/conf/nginx.conf
user nginx; # 指定运行用户
# 验证配置并重载
[root@Nginx ~]# nginx -t
[root@Nginx ~]# nginx -s reload
# 手动设置进程数
[root@Nginx ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes 2; # 设置 2 个工作进程
[root@Nginx ~]# nginx -s reload
# 自动适配 CPU(推荐)
[root@Nginx ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes auto; # 自动匹配 CPU 核心数
worker_cpu_affinity 0001 0010 0100 1000; # 进程绑定 CPU 核心(4 核心示例)
# 验证绑定结果
[root@Nginx ~]# ps axo pid,cmd,psr | grep nginx
887 nginx: master process /usr/
3 1635 nginx: worker process 0
1636 nginx: worker process 1
1637 nginx: worker process 2
1638 nginx: worker process 3
[root@Nginx ~]# vim /usr/local/nginx/conf/nginx.conf
events {
worker_connections 10000; # 单个进程最大连接数
use epoll; # 使用 epoll 事件模型
accept_mutex on; # 防止惊群效应
multi_accept on; # 一次性接受所有新连接
}
[root@Nginx ~]# nginx -s reload
# 报错场景:ab 测试时提示 "socket: Too many open files (24)"
[root@Nginx ~]# ab -n 100000 -c5000 http://172.25.254.100/index.html
# 修改系统限制
[root@Nginx ~]# vim /etc/security/limits.conf
* - nofile 100000 # 所有用户最大文件句柄数
* - noproc 100000 # 所有用户最大进程数
root - nofile 100000 # root 用户单独设置
# 验证生效
[root@Nginx ~]# sudo -u nginx ulimit -n 100000
# 重新测试高并发
[root@Nginx ~]# ab -n 100000 -c10000 http://172.25.254.100/index.html

微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online
将 Markdown(GFM)转为 HTML 片段,浏览器内 marked 解析;与 HTML 转 Markdown 互为补充。 在线工具,Markdown 转 HTML在线工具,online
将 HTML 片段转为 GitHub Flavored Markdown,支持标题、列表、链接、代码块与表格等;浏览器内处理,可链接预填。 在线工具,HTML 转 Markdown在线工具,online
通过删除不必要的空白来缩小和压缩JSON。 在线工具,JSON 压缩在线工具,online
将JSON字符串修饰为友好的可读格式。 在线工具,JSON美化和格式化在线工具,online