温馨提示×

ThinkPHP框架在Linux下的负载均衡方案

小樊
45
2025-11-16 18:22:13
栏目: 云计算

Linux下ThinkPHP负载均衡方案

一 架构总览

  • 前端入口使用NginxHAProxy做反向代理与负载均衡,将请求分发到多台运行PHP-FPMThinkPHP后端。
  • 多实例部署时,统一对外暴露1个或少量VIP/域名,后端横向扩容,避免单点。
  • 为保障高可用,可在负载均衡器前叠加Keepalived VIP,实现故障自动切换。
  • 典型数据流:客户端 → 负载均衡器(Nginx/HAProxy) → 多台后端(PHP-FPM + ThinkPHP) → 共享存储/缓存/数据库。

二 方案一 Nginx反向代理与负载均衡

  • 安装与启用
    • Ubuntu/Debian:sudo apt update && sudo apt install nginx
    • CentOS/RHEL:sudo yum install -y epel-release && sudo yum install -y nginx
  • 核心配置示例(/etc/nginx/nginx.conf 或 /etc/nginx/conf.d/upstream.conf)
    • 定义上游与反向代理,注意传递客户端真实IP与协议头:
      http {
        upstream thinkphp_servers {
          server 192.168.1.11:80 weight=1 max_fails=3 fail_timeout=30s;
          server 192.168.1.12:80 weight=1 max_fails=3 fail_timeout=30s;
          server 192.168.1.13:80 backup;
        }
      
        server {
          listen 80;
          server_name your.domain.com;
      
          location / {
            proxy_pass http:/php_servers;
            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_connect_timeout 5s;
            proxy_send_timeout 15s;
            proxy_read_timeout 15s;
            proxy_buffering off;
          }
        }
      }
      
  • 生效与验证
    • 检查配置:sudo nginx -t
    • 重载:sudo systemctl reload nginx
    • 观察 upstream 状态页(可选):http://your.domain.com/nginx_status

三 方案二 HAProxy负载均衡

  • 安装
    • Ubuntu/Debian:sudo apt update && sudo apt install haproxy
    • CentOS/RHEL:sudo yum install -y haproxy
  • 核心配置示例(/etc/haproxy/haproxy.cfg)
    global
      log /dev/log local0
      log /dev/log local1 notice
      daemon
      maxconn 4096
    
    defaults
      log global
      mode http
      option httplog
      option dontlognull
      timeout connect 5000ms
      timeout client  50000ms
      timeout server  50000ms
    
    frontend http_front
      bind *:80
      default_backend http_back
    
    backend http_back
      balance roundrobin
      option httpchk GET /health
      http-check expect status 200
      server tp1 192.168.1.11:80 check inter 5s fall 3 rise 2
      server tp2 192.168.1.12:80 check inter 5s fall 3 rise 2
      server tp3 192.168.1.13:80 check backup
    
  • 生效与验证
    • 检查:sudo haproxy -c -f /etc/haproxy/haproxy.cfg
    • 启动/重载:sudo systemctl reload haproxy
    • 访问统计页(可选):http://your.domain.com/haproxy?stats

四 高可用与容器化部署

  • 高可用 Keepalived VIP
    • 在两台或多台负载均衡器上部署Keepalived,配置VRRP虚拟IP(VIP),实现主备或集群高可用;对外统一使用VIP访问,后端指向多台ThinkPHP实例。
    • 示例思路:定义 vrrp_instance,设置 state/priority/advert_int,配置 virtual_ipaddress;健康检查可结合 TCP 或 HTTP。
  • Docker Compose一键编排
    • 多实例 + Nginx 示例要点:
      • PHP-FPM 服务 replicas: 3
      • Nginx 服务暴露 80:80,反向代理到 PHP-FPM 容器(如 fastcgi_pass thinkphp_app:9000)
      • 使用同一网络,代码与配置保持一致,便于横向扩容。

五 关键注意事项与最佳实践

  • 会话与缓存共享
    • 多实例下务必将Session缓存外置到Redis/Memcached,避免单机内存导致登录状态不一致。
  • 静态资源
    • Nginx直接托管静态资源(如 public/static),开启 gzip、长缓存与 CDN,减轻后端压力。
  • 健康检查与熔断
    • 为后端配置主动健康检查(HTTP/端口探活),设置合理的 fail_timeout、max_fails 与重试,避免雪崩。
  • 日志与可观测性
    • 统一访问日志与错误日志路径;在 Nginx/HAProxy 记录 X-Forwarded-For 以便追溯真实来源。
  • 文件上传与存储
    • 避免将上传文件写入本地磁盘,建议使用对象存储(如七牛/OSS)或分布式文件系统,保证多实例一致性。
  • 数据库与缓存高可用
    • 数据库建议主从/集群,缓存建议Redis 集群/哨兵,并配置超时与重连策略,提升整体稳定性。

0