Skip to content

负载均衡配置

概述

负载均衡是将请求分发到多个服务器上,以提高系统的可用性、扩展性和性能。DSMall Pro支持通过Nginx实现负载均衡,将用户请求分发到多个应用服务器实例上。

负载均衡架构

1. 架构图

用户请求

Nginx负载均衡器

┌─────────────┬─────────────┬─────────────┐
│ 应用服务器1  │ 应用服务器2  │ 应用服务器3  │
│ 192.168.1.101│ 192.168.1.102│ 192.168.1.103│
│ PHP-FPM     │ PHP-FPM     │ PHP-FPM     │
└─────────────┴─────────────┴─────────────┘
    ↓                ↓                ↓
┌─────────────────────────────────────────┐
│           共享存储层                      │
│  MySQL主从  │  Redis  │  文件存储    │
└─────────────────────────────────────────┘

环境要求

1. 服务器要求

  • 负载均衡器:2核4G内存,推荐4核8G
  • 应用服务器:每台2核4G内存起步,推荐4核8G
  • 网络带宽:负载均衡器需要足够带宽处理所有请求

Nginx负载均衡配置

1. 基础配置

编辑Nginx配置文件 /etc/nginx/nginx.conf

nginx
http {
    # 定义上游服务器组
    upstream thinkphp_cluster {
        server 192.168.1.101:80 weight=3;
        server 192.168.1.102:80 weight=2;
        server 192.168.1.103:80 weight=1;
        server 192.168.1.104:80 backup;  # 备用服务器
    }

    server {
        listen 80;
        server_name yourdomain.com;

        # 静态文件处理(如果使用第三方存储,可删除此配置)
        # location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
        #     root /var/www/html/public/static;
        #     expires 30d;
        #     access_log off;
        # }

        # ThinkPHP应用请求
        location / {
            proxy_pass http://thinkphp_cluster;
            
            # 代理头设置
            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 30s;
            proxy_send_timeout 30s;
            proxy_read_timeout 30s;
        }
    }
}

2. SSL配置

nginx
server {
    listen 443 ssl http2;
    server_name yourdomain.com;

    # SSL证书配置
    ssl_certificate /etc/ssl/certs/yourdomain.com.crt;
    ssl_certificate_key /etc/ssl/private/yourdomain.com.key;
    
    # SSL优化
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;

    # 其他配置与HTTP相同...
}

3. 负载均衡算法

3.1 权重轮询(默认)

nginx
upstream thinkphp_cluster {
    server 192.168.1.101:80 weight=3;
    server 192.168.1.102:80 weight=2;
    server 192.168.1.103:80 weight=1;
}

3.2 IP哈希(会话保持)

nginx
upstream thinkphp_cluster {
    ip_hash;
    server 192.168.1.101:80;
    server 192.168.1.102:80;
    server 192.168.1.103:80;
}

注意:如果使用JWT认证,无需考虑会话保持,可以使用其他负载均衡算法。

3.3 最少连接

nginx
upstream thinkphp_cluster {
    least_conn;
    server 192.168.1.101:80;
    server 192.168.1.102:80;
    server 192.168.1.103:80;
}

应用服务器配置

1. PHP-FPM配置

编辑 /etc/php/8.0/fpm/pool.d/www.conf

ini
[www]
user = www-data
group = www-data
listen = 127.0.0.1:9000

pm = dynamic
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 20

2. Nginx应用服务器配置

编辑 /etc/nginx/sites-available/thinkphp

nginx
server {
    listen 80;
    server_name _;
    root /var/www/html/public;
    index index.php;

    # 静态文件处理(如果使用第三方存储,可删除此配置)
    # location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
    #     expires 30d;
    #     access_log off;
    # }

    # ThinkPHP路由
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    # PHP处理
    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

部署步骤

1. 准备服务器

bash
# 在所有服务器上安装基础环境
sudo apt update
sudo apt install nginx php8.0-fpm php8.0-mysql php8.0-redis mysql-client

2. 配置负载均衡器

bash
# 1. 编辑Nginx配置
sudo nano /etc/nginx/nginx.conf

# 2. 测试配置
sudo nginx -t

# 3. 重启Nginx
sudo systemctl restart nginx
sudo systemctl enable nginx

3. 配置应用服务器

bash
# 1. 上传项目文件
sudo rsync -av /path/to/dsadmin/ /var/www/html/

# 2. 设置权限
sudo chown -R www-data:www-data /var/www/html/
sudo chmod -R 755 /var/www/html/

# 3. 配置Nginx
sudo nano /etc/nginx/sites-available/thinkphp
sudo ln -s /etc/nginx/sites-available/thinkphp /etc/nginx/sites-enabled/

# 4. 重启服务
sudo systemctl restart nginx
sudo systemctl restart php8.0-fpm

监控和维护

1. 健康检查

bash
# 检查Nginx状态
sudo systemctl status nginx

# 检查PHP-FPM状态
sudo systemctl status php8.0-fpm

# 检查后端服务器
curl -I http://192.168.1.101/
curl -I http://192.168.1.102/
curl -I http://192.168.1.103/

2. 日志监控

bash
# 查看Nginx访问日志
sudo tail -f /var/log/nginx/access.log

# 查看Nginx错误日志
sudo tail -f /var/log/nginx/error.log

故障排除

1. 负载均衡问题

问题:请求分发不均匀

解决方案

  • 检查权重配置
  • 调整负载均衡算法
  • 检查服务器性能差异

2. 静态文件问题

问题:静态文件加载失败

解决方案

  • 检查第三方存储配置
  • 验证CDN配置
  • 检查文件URL生成

性能优化

1. Nginx优化

nginx
# 工作进程数
worker_processes auto;

# 连接数限制
worker_connections 1024;

# Gzip压缩
gzip on;
gzip_types text/plain text/css application/json application/javascript;

2. PHP-FPM优化

ini
; 进程管理
pm = dynamic
pm.max_children = 100
pm.start_servers = 20
pm.min_spare_servers = 10
pm.max_spare_servers = 30

部署检查清单

环境检查

  • [ ] 所有服务器网络连通
  • [ ] Nginx服务正常运行
  • [ ] PHP-FPM服务正常运行

配置检查

  • [ ] 负载均衡配置正确
  • [ ] 应用服务器配置正确
  • [ ] SSL证书配置正确

功能检查

  • [ ] 负载均衡分发正常
  • [ ] 第三方存储访问正常
  • [ ] API接口响应正常

最后更新:2024-01-20
维护者:DSPlatform技术团队(德尚网络)

获取帮助

如果您在使用过程中遇到问题,可以通过以下方式获取帮助:

  • 官方网站https://www.csdeshang.com
  • 电话咨询:15364080101(微信同号)
  • QQ咨询:858761000
  • 邮箱咨询:858761000@qq.com
  • 工作时间:工作日 9:00-18:00
  • 微信咨询:扫码添加微信
微信二维码

版权所有 © 2014-至今 德尚网络