Nginx 负载均衡配置

1. 环境准备

假设你有以下环境:

  • 负载均衡服务器:运行 Nginx 服务器,写好配置文件。
  • 后端服务器:至少两台,运行相同的网站(如 WordPress)监听 80 端口,写好配置文件。
  • 每台后端服务器已安装 LEMP 环境,网站文件和数据库一致(生产环境部署的时候,数据库和文件一致更是必须的)。

验证后端服务器

  • 确保后端服务器的 Nginx 和 PHP-FPM 正常运行:

    sudo systemctl status nginx
    sudo systemctl status php8.2-fpm #改成你自己装的版本
    
    sudo apt install php-fpm -y #如果没装记得装上
    

    访问 http://192.168.0.141http://192.168.0.142,确认网站正常运行。

2. 配置 Nginx 负载均衡

在负载均衡服务器上配置 Nginx,将请求分发到后端服务器。

步骤

  • 编辑 Nginx 配置文件(如 /etc/nginx/sites-available/default):

    upstream backend {
        # 后端服务器列表(根据实际替换)
        server 192.168.0.141:80;
        server 192.168.0.142:80;
    }
    
    server {
        listen 80;
        server_name 替换为服务器IP;
    
        location / {
            proxy_pass http://backend;
            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;
        }
    }
    

    后端服务器nginx的配置(两个服务器相同就行)

    server {
      listen 80 default_server;
      root /var/www/html; # 替换为实际网站路径
      index index.php index.html;
    
      location ~ \.php$ {
          include snippets/fastcgi-php.conf;
          fastcgi_pass unix:/run/php/php8.2-fpm.sock; # 根据PHP版本调整(php -v查看版本,例如8.2.28就取前面的8.2)
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          include fastcgi_params;
      }
    }
    

    记得删掉默认配置,避免与default_server冲突,一般文件位于/etc/nginx/sites-enabled/default

    sudo rm /etc/nginx/sites-enabled/default
    

    配置完重启nginx

    sudo nginx -t
    sudo systemctl reload nginx
    

配置说明

  • upstream backend:定义后端服务器池,请求将分发到列出的服务器。
  • proxy_pass http://backend:将请求转发到 backend 池。
  • proxy_set_header:传递客户端的真实 IP 和请求头,确保后端服务器记录正确信息。

  • 测试配置:

    sudo nginx -t
    

    如果无错误,重载 Nginx:

    sudo systemctl reload nginx
    

3. 负载均衡策略(可选)

Nginx 默认使用 轮询(round-robin) 策略,依次将请求分发到后端服务器。你可以根据需求调整配置文件:

  • 轮询(round-robin) 将客户端的请求依次分发到后端的每台服务器,按顺序循环进行:

    upstream backend {
        server 192.168.0.141:80;
        server 192.168.0.142:80;
    }
    
  • 权重(weight): 为性能较强的服务器分配更多请求:

    upstream backend {
        server 192.168.0.141:80 weight=3;
        server 192.168.0.142:80 weight=1;
    }
    

    192.168.1.101 将接收 75% 的请求。

  • 最少连接(least_conn): 将请求分发到当前连接数最少的服务器:

    upstream backend {
        least_conn;
        server 192.168.0.141:80;
        server 192.168.0.142:80;
    }
    
  • IP 哈希(ip_hash): 根据客户端 IP 固定分发到同一服务器,适合需要会话保持的场景:

    upstream backend {
        ip_hash;
        server 192.168.0.141:80;
        server 192.168.0.142:80;
    }
    

选择策略后,更新配置并重载:

sudo nginx -t
sudo systemctl reload nginx

5. 测试负载均衡

要求:访问 http://负载均衡服务器IP,多次刷新页面,观察请求是否分发到不同后端服务器。

  • 验证客户端 IP 传递: 在后端服务器的每台服务器添加 PHP 文件(如 /var/www/html/index.php)添加:

    <?php
    echo "服务器 Server IP: " . $_SERVER['SERVER_ADDR'] . "<br>";
    echo "客户端 Client IP: " . $_SERVER['HTTP_X_REAL_IP'];
    ?>
    

    访问 http://负载服务器IP,(不要写成后端服务器的IP)确认显示后端服务器 IP 和客户端真实 IP。 load

  • 检查每个后端服务器的访问日志:

    sudo tail -f /var/log/nginx/access.log
    

    192.168.0.141192.168.0.142 上运行,确认请求分布。 load_gif


6. 同步后端服务器

后端服务器的网站文件和数据库需保持同步:

  • 文件同步:使用 rsync

    rsync -avz /var/www/html/ user@192.168.0.223:/var/www/html/
    
  • 数据库同步:配置 MySQL 主从复制或使用共享数据库:

    mysql -u root -p
    GRANT REPLICATION SLAVE ON *.* TO 'repl_user'@'192.168.0.223' IDENTIFIED BY 'repl_password';
    

    在从服务器上配置同步(具体步骤可另行提供)。

梦葉樱 all right reserved,powered by Gitbook该文件最后修改时间: 2025-07-21 12:54:03

results matching ""

    No results matching ""