作为PHP服务端开发工程师,掌握Nginx运维技能是提升系统稳定性和排障效率的关键。本文将聚焦PHP开发者日常需要掌握的Nginx核心运维技能,涵盖流量监控、服务管理、配置优化等实战场景,并提供可直接落地的代码示例。
一、Nginx基础运维操作
1. 服务启停与重启
1 2 3 4 5 6 7 8 9 10 11 12 13
| systemctl status nginx
service nginx status
sudo nginx -s reload
sudo systemctl restart nginx
sudo systemctl stop nginx
|
注意:生产环境优先使用reload
而非restart
,避免请求中断。若配置有误,可通过nginx -t
预先测试。
2. 查看实时流量与连接状态
(1)通过Nginx状态模块
需在配置中启用status_zone
:
1 2 3 4 5 6 7 8 9 10 11 12
| http { server { listen 80; server_name example.com; location /nginx_status { stub_status; allow 192.168.1.0/24; deny all; } } }
|
访问
http://example.com/nginx_status
返回数据示例:
1 2 3 4
| Active connections: 15 server accepts handled requests 100000 100000 200000 Reading: 2 Writing: 5 Waiting: 8
|
- Active connections:当前活跃连接数
- Accepted/Handled:已接受/处理的连接数
- Reading/Writing/Waiting:读写中/等待中的连接数
(2)通过命令行工具
1 2 3 4 5 6 7 8
| ps aux | grep nginx | wc -l
netstat -antp | grep nginx | wc -l
sudo iftop -i eth0 -f "port 80"
|
二、PHP相关配置实战
1. PHP-FPM与Nginx联动配置
(1)基础Server配置示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| server { listen 80; server_name api.example.com; root /var/www/php-app/public; index index.php;
location / { try_files $uri $uri/ /index.php?$query_string; }
location ~ \.php$ { include fastcgi_params; fastcgi_pass unix:/run/php/php8.2-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_read_timeout 300; }
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ { expires 30d; access_log off; } }
|
(2)PHP-FPM池优化配置(/etc/php/8.2/fpm/pool.d/www.conf)
1 2 3 4 5 6
| pm = dynamic pm.max_children = 50 pm.start_servers = 5 pm.min_spare_servers = 2 pm.max_spare_servers = 8 pm.max_requests = 500
|
调优建议:
pm.max_children
= (可用内存MB / 单个PHP进程内存占用MB)
- 通过
top
命令查看PHP-FPM进程内存占用(RES
列)
2. 日志分析与错误排查
(1)Nginx日志配置
1 2 3 4 5 6 7 8
| http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main; error_log /var/log/nginx/error.log warn; }
|
(2)常用日志分析命令
1 2 3 4 5 6 7 8 9 10 11
| tail -f /var/log/nginx/access.log
awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -nr | head -10
grep ' 404 ' /var/log/nginx/access.log
tail -f /var/log/php8.2-fpm.log
|
三、高级运维技巧
1. 动态配置热加载
1 2 3 4 5 6 7 8
| sudo nginx -t
sudo nginx -s reload
sudo nginx -s reopen
|
2. 负载均衡配置示例
1 2 3 4 5 6 7 8 9 10 11 12 13
| upstream php_servers { server 192.168.1.10:9000 weight=3; server 192.168.1.11:9000; server 192.168.1.12:9000 backup; }
server { location / { proxy_pass http://php_servers; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } }
|
3. 安全加固措施
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| server { server_tokens off;
if ($request_method !~ ^(GET|POST|HEAD)$ ) { return 405; }
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
client_max_body_size 10M; }
|
四、监控与性能优化
1. 关键指标监控清单
指标 |
监控工具 |
告警阈值建议 |
Nginx活跃连接数 |
Prometheus+Grafana |
>500(根据服务器配置调整) |
PHP-FPM进程数 |
netdata |
>pm.max_children的80% |
请求响应时间 |
New Relic |
P95 > 500ms |
错误率 |
ELK Stack |
>1% |
2. 性能优化 checklist
- [ ] 开启Gzip压缩
1 2
| gzip on; gzip_types text/plain text/css application/json application/javascript;
|
- [ ] 配置静态文件缓存
1 2 3 4
| location ~* \.(woff2|eot|ttf)$ { expires 1y; add_header Cache-Control "public"; }
|
- [ ] 调整PHP-FPM进程回收策略
五、典型问题处理流程
场景:PHP页面响应缓慢
检查Nginx连接状态
1
| curl http://localhost/nginx_status
|
- 若
Waiting
连接数持续高位 → 可能后端PHP处理慢
分析PHP-FPM状态
1 2
| sudo systemctl status php8.2-fpm sudo tail -f /var/log/php8.2-fpm.log
|
数据库查询优化
- 检查慢查询日志(MySQL的
slow_query_log
)
缓存策略验证
掌握这些Nginx运维技能后,PHP开发者可以独立完成从部署、调优到排障的全流程工作。建议定期进行以下演练:
- 模拟Nginx配置错误后的恢复流程
- 压力测试(
ab -n 10000 -c 100 http://test.com/
)
- 故障转移测试(关闭一台PHP-FPM服务器验证负载均衡)