Nginx PHP-FPM调优
优化 Nginx + PHP-FPM,可以从几个核心维度进行调优,包括 并发性能、内存使用、响应速度 等。下面按系统化方法详细说明:
一、Nginx 调优
Nginx 主要是作为反向代理和静态文件服务器,其性能调优主要集中在 连接处理和缓存机制。
1. 工作进程与连接数
nginx
worker_processes auto; # 根据 CPU 核心数自动调整
worker_connections 1024; # 每个 worker 最大连接数worker_processes:一般设为 CPU 核心数或autoworker_connections:每个 worker 可以处理的最大连接数- 最大并发连接数 ≈
worker_processes * worker_connections
2. 网络参数优化
nginx
keepalive_timeout 65;
keepalive_requests 100;
tcp_nopush on;
tcp_nodelay on;
sendfile on;- keepalive_timeout:保持连接,减少 TCP 握手消耗
- tcp_nopush / tcp_nodelay:优化 TCP 包发送
- sendfile:发送静态文件使用零拷贝,提高性能
3. 压缩和缓存
nginx
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
# 静态文件缓存
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
add_header Cache-Control "public";
}- 开启 gzip 减少传输体积
- 静态资源使用 长缓存,减轻 PHP-FPM 压力
4. 慢请求日志
nginx
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$request_time"';
access_log /var/log/nginx/access.log main;- 通过
$request_time找慢请求,定位瓶颈
二、PHP-FPM 调优
PHP-FPM 是 PHP 的 FastCGI 进程管理器,主要调优方向是 进程数、请求处理模式、内存和超时。
1. 进程管理模式
ini
pm = dynamic # 可选 dynamic | ondemand | static
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500- dynamic:适合流量波动大
- ondemand:按需启动,节省内存
- static:适合流量稳定、高并发
参数说明:
pm.max_children:最大子进程数,限制 PHP 最大并发pm.start_servers:启动时的子进程数pm.min/max_spare_servers:空闲进程数量,避免频繁创建销毁pm.max_requests:防止内存泄漏,达到一定请求后重启子进程
计算 max_children:
text
max_children = 总内存 * 0.7 / 单个 PHP 进程平均内存2. 超时设置
ini
request_terminate_timeout = 30s
request_slowlog_timeout = 5s
slowlog = /var/log/php-fpm/slow.log- 避免 PHP 脚本长时间占用资源
- 找出慢请求
3. Opcache
PHP 性能提升核心:
ini
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=10000
opcache.revalidate_freq=60
opcache.validate_timestamps=1- 加速 PHP 代码执行
- 减少磁盘 I/O
三、结合 Nginx + PHP-FPM 的优化点
- FastCGI 缓存
nginx
fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=PHP:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
location ~ \.php$ {
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_cache PHP;
fastcgi_cache_valid 200 1m;
}- 对动态请求也可以做缓存,减轻 PHP 压力
- 连接池优化
nginx
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_connect_timeout 60s;
fastcgi_send_timeout 180s;
fastcgi_read_timeout 180s;- 调整缓冲区和超时,避免大请求阻塞
- 监控
- Nginx:
stub_status模块 - PHP-FPM:
pm.status_path+status页面 - 配合
htop/top/vmstat观察内存和 CPU
四、常见调优思路
- 先确定瓶颈:是 Nginx、PHP-FPM 还是数据库
- 优先开启缓存:静态资源 + Opcache + FastCGI Cache
- 调整 PHP-FPM 进程数,保证 CPU 和内存充分利用
- 优化 Nginx 连接和缓冲,减少慢请求
- 定期分析慢日志,持续优化
高并发小内存、高并发大内存 Nginx + PHP-FPM 调优配置模板:
- ✅ 方案 A:高并发 + 小内存型(2–4 GB 内存)
- ✅ 方案 B:高并发 + 大内存型(8–32 GB 内存)
每套都包括:
- Nginx 主配置优化
- PHP-FPM 池配置
- Opcache 配置
- 说明每个参数为何这样调
🟩 方案 A:小内存服务器(2–4 GB,CPU 2–4核)
🔹 Nginx 主配置 /etc/nginx/nginx.conf
nginx
user www-data;
worker_processes auto;
worker_rlimit_nofile 65535;
events {
worker_connections 1024;
use epoll;
multi_accept on;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
keepalive_requests 100;
gzip on;
gzip_disable "msie6";
gzip_comp_level 5;
gzip_min_length 1k;
gzip_proxied any;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
client_max_body_size 20m;
open_file_cache max=1000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
server {
listen 80;
server_name example.com;
root /var/www/html;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_index index.php;
fastcgi_connect_timeout 30s;
fastcgi_send_timeout 60s;
fastcgi_read_timeout 60s;
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
fastcgi_busy_buffers_size 64k;
fastcgi_temp_file_write_size 64k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff|woff2|svg)$ {
expires 30d;
access_log off;
add_header Cache-Control "public";
}
}
}🔹 PHP-FPM 配置 /etc/php/8.1/fpm/pool.d/www.conf
ini
[www]
user = www-data
group = www-data
listen = /run/php/php8.1-fpm.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
pm = dynamic
pm.max_children = 20
pm.start_servers = 3
pm.min_spare_servers = 2
pm.max_spare_servers = 6
pm.max_requests = 300
request_terminate_timeout = 30s
request_slowlog_timeout = 5s
slowlog = /var/log/php8.1-fpm/slow.log
catch_workers_output = yes
rlimit_files = 65535💡说明:
pm.max_children=20→ 约 100–150 MB/进程,总占用约 2–3 GBmax_requests=300→ 防止内存泄漏ondemand模式可选用于极低访问场景节省内存
🔹 Opcache /etc/php/8.1/fpm/conf.d/10-opcache.ini
ini
opcache.enable=1
opcache.memory_consumption=96
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=8000
opcache.validate_timestamps=1
opcache.revalidate_freq=60
opcache.fast_shutdown=1🟦 方案 B:大内存服务器(8–32 GB,CPU ≥ 8 核)
🔹 Nginx 主配置 /etc/nginx/nginx.conf
nginx
user www-data;
worker_processes auto;
worker_rlimit_nofile 200000;
events {
worker_connections 8192;
use epoll;
multi_accept on;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 75;
keepalive_requests 200;
gzip on;
gzip_comp_level 6;
gzip_min_length 1k;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml+rss text/javascript;
client_max_body_size 50m;
open_file_cache max=10000 inactive=60s;
open_file_cache_valid 120s;
open_file_cache_min_uses 5;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
# FastCGI 缓存
fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=PHP:256m inactive=60m max_size=5g;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
server {
listen 80;
server_name example.com;
root /var/www/html;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/run/php/php8.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_connect_timeout 60s;
fastcgi_send_timeout 180s;
fastcgi_read_timeout 180s;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_cache PHP;
fastcgi_cache_valid 200 302 1m;
fastcgi_cache_valid 404 30s;
fastcgi_cache_use_stale error timeout invalid_header updating;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff|woff2|svg)$ {
expires 90d;
add_header Cache-Control "public";
access_log off;
}
}
}🔹 PHP-FPM 配置 /etc/php/8.1/fpm/pool.d/www.conf
ini
[www]
user = www-data
group = www-data
listen = /run/php/php8.1-fpm.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0660
pm = static
pm.max_children = 120
pm.max_requests = 1000
rlimit_files = 65535
catch_workers_output = yes
request_terminate_timeout = 60s
request_slowlog_timeout = 5s
slowlog = /var/log/php8.1-fpm/slow.log💡说明:
static模式让每个 CPU 核心都长期有固定 worker,性能最稳定max_children=120(假设每个进程 80–100MB,可支持约 10–12 GB 内存占用)max_requests=1000适合大流量长运行场景
🔹 Opcache /etc/php/8.1/fpm/conf.d/10-opcache.ini
ini
opcache.enable=1
opcache.memory_consumption=512
opcache.interned_strings_buffer=32
opcache.max_accelerated_files=50000
opcache.validate_timestamps=1
opcache.revalidate_freq=120
opcache.fast_shutdown=1🚀 性能建议总结
| 项目 | 小内存型 | 大内存型 |
|---|---|---|
| PHP-FPM 模式 | dynamic / ondemand | static |
| pm.max_children | 10–30 | 100–200 |
| Opcache | 96–128 MB | 256–512 MB |
| Nginx 缓存 | 可不开启 | 建议开启 fastcgi_cache |
| gzip 压缩 | 开启,等级 4–5 | 开启,等级 5–6 |
| keepalive | 65 s | 75 s |
| 日志 | 每天轮转,避免磁盘压力 | 可长时间保留做分析 |
