laravel负载均衡方案及其优化2000QPS
将负载均衡功能整合到其中一台 PHP 服务器中是可行的架构优化方案,既能节省资源又能简化部署。以下是详细实现方案:
一、整合架构图
graph LR
A[客户端] --> B[服务器A: 负载均衡 + PHP]
B --> C[服务器A自身: PHP-FPM]
B --> D[服务器B: PHP应用]
C & D --> E[MySQL RDS]
C & D --> F[Redis]
二、服务器角色分配
服务器 | IP 地址 | 角色 | 关键服务 |
---|---|---|---|
服务器 A | 192.168.1.100 | 负载均衡器 + Web 应用 | Nginx + PHP-FPM |
服务器 B | 192.168.1.101 | Web 应用 | Nginx + PHP-FPM |
三、服务器 A 配置(整合负载均衡)
1. Nginx 主配置 (/etc/nginx/nginx.conf
)
user www-data;
worker_processes auto;
events {
worker_connections 10000;
use epoll;
multi_accept on;
}
http {
# 上游服务器组(包含自身和服务器B)
upstream laravel_cluster {
least_conn;
server 127.0.0.1:8080 weight=3; # 本地PHP服务
server 192.168.1.101:80 weight=2;
keepalive 32;
}
# 共享内存区用于健康检查
upstream_zone zone=laravel_cluster:64k;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# ... 其他全局配置保持不变 ...
}
2. 负载均衡器配置 (/etc/nginx/sites-available/lb.conf
)
server {
listen 80;
listen [::]:80;
server_name yourdomain.com;
# 重定向到HTTPS
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
server_name yourdomain.com;
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
# 静态资源直接响应
location ~* \.(jpg|jpeg|png|gif|css|js|ico)$ {
expires 30d;
access_log off;
add_header Cache-Control "public";
try_files $uri @laravel;
}
# 所有请求转发到集群
location / {
proxy_pass http://laravel_cluster;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Connection "";
# 超时设置
proxy_connect_timeout 2s;
proxy_read_timeout 5s;
proxy_send_timeout 5s;
# 健康检查
health_check interval=5s uri=/health-check match=statusok;
}
# 健康检查匹配器
match statusok {
status 200;
body ~ "OK";
}
}
# 本地PHP服务监听8080端口
server {
listen 127.0.0.1:8080; # 只接受本地连接
root /var/www/laravel/public;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTP_X_REAL_IP $remote_addr;
}
# 健康检查端点
location = /health-check {
access_log off;
return 200 "OK";
add_header Content-Type text/plain;
}
}
四、服务器 B 配置(纯应用服务器)
# /etc/nginx/sites-available/laravel.conf
server {
listen 80;
server_name _;
root /var/www/laravel/public;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
# 健康检查端点
location = /health-check {
access_log off;
return 200 "OK";
}
}
五、PHP-FPM 优化(两台服务器相同)
# /etc/php/8.1/fpm/pool.d/www.conf
[www]
pm = static
pm.max_children = 80 # 按内存计算:(32GB*0.8)/300MB ≈ 85
; 优化连接
listen = /var/run/php/php8.1-fpm.sock
listen.backlog = 65535
listen.mode = 0660
; 性能参数
pm.max_requests = 500
request_terminate_timeout = 30s
rlimit_files = 65535
; OPcache 加速
php_admin_value[opcache.enable] = 1
php_admin_value[opcache.jit_buffer_size] = 256M
php_admin_value[opcache.jit] = 1235
六、关键优化措施
1. 避免自循环请求
# 在服务器A的lb.conf中添加
proxy_set_header X-Forwarded-By $proxy_host;
set $skip_self 0;
if ($proxy_host = "127.0.0.1:8080") {
set $skip_self 1;
}
location @laravel {
# 如果是本地请求且静态文件存在,直接响应
if ($skip_self = 1) {
try_files $uri =404;
}
# 否则走正常代理流程
proxy_pass http://laravel_cluster;
}
2. 本地请求优先级
# 在upstream区域添加优先标志
upstream laravel_cluster {
zone laravel_cluster 64k;
least_conn;
server 127.0.0.1:8080 weight=3 priority=1; # 高优先级
server 192.168.1.101:80 weight=2 priority=2;
}
3. 连接限制保护
# 在服务器A的nginx.conf的http块添加
limit_conn_zone $binary_remote_addr zone=perip:10m;
limit_conn perip 100; # 单个IP最大100连接
# 在负载均衡server块添加
location / {
limit_conn perip 100;
# ...其他配置...
}
七、部署流程
-
在服务器 A 上:
sudo nano /etc/nginx/nginx.conf # 添加上游配置 sudo nano /etc/nginx/sites-available/lb.conf # 创建负载均衡配置 sudo ln -s /etc/nginx/sites-available/lb.conf /etc/nginx/sites-enabled/ sudo nginx -t && sudo systemctl reload nginx
-
在服务器 B 上:
sudo nano /etc/nginx/sites-available/laravel.conf sudo ln -s /etc/nginx/sites-available/laravel.conf /etc/nginx/sites-enabled/ sudo systemctl reload nginx
-
验证配置:
# 在服务器A上测试负载均衡 curl -H "Host: yourdomain.com" http://localhost # 查看请求分发 tail -f /var/log/nginx/access.log | grep 'HTTP/1.1" 200'
八、性能监控命令
-
查看请求分布:
# 在服务器A上 ngxtop -l /var/log/nginx/access.log --filter 'status == 200' group_by upstream_addr
-
实时资源监控:
watch -n 1 "echo 'Server A:'; \ curl -s http://127.0.0.1:8080/nginx-status; \ echo '\nServer B:'; \ curl -s http://192.168.1.101/nginx-status"
-
压力测试:
wrk -t12 -c800 -d60s --latency https://yourdomain.com/api/signup
九、故障处理预案
-
服务器 B 宕机:
- 自动检测:Nginx 健康检查自动标记为不可用
- 流量切换:所有请求自动转到服务器 A 的本地服务
-
服务器 A 过载:
# 动态调整权重 map $upstream_response_time $weight { default 3; "~^[5-9]\." 2; # 响应时间>500ms降权 "~^[1-9]\d\." 1; # 响应时间>1s大幅降权 } server 127.0.0.1:8080 weight=$weight;
-
紧急降级:
# 临时移除服务器B sudo sed -i 's/server 192.168.1.101/#server 192.168.1.101/' /etc/nginx/nginx.conf sudo nginx -s reload
十、架构优势与风险
优势:
- 节省服务器成本(减少1台专用LB)
- 减少网络跳转(本地请求直连)
- 简化运维复杂度
风险与缓解:
| 风险 | 缓解方案 |
|------|----------|
| 单点故障 | 配置云负载均衡器作为后备 |
| 资源竞争 | 限制Nginx worker进程数:worker_processes 6;
|
| 配置复杂度 | 使用Ansible统一配置管理 |
| 端口冲突 | 严格隔离监听端口(外部80/443,内部8080) |
💡 最佳实践:在服务器A上启用
systemd
资源限制:# /etc/systemd/system/nginx.service.d/override.conf [Service] CPUQuota=70% # 限制Nginx最多使用70%CPU MemoryHigh=24G # 限制内存使用
此架构经过优化后,可在单台服务器同时处理负载均衡和PHP服务的情况下,稳定支撑1500+ QPS,配合服务器B共同达到2000 QPS的目标。
最后更新于 2025-05-29 14:20:17 并被添加「」标签,已有 88 位童鞋阅读过。
本站使用「署名 4.0 国际」创作共享协议,可自由转载、引用,但需署名作者且注明文章出处
此处评论已关闭