Nginx部署

zy2025
2026-01-06 / 0 评论 / 11 阅读 / 正在检测是否收录...

1.环境准备:所有服务器需安装必要依赖:

yum install -y gcc gcc-c++ make pcre-devel zlib-devel openssl-devel wget
#如果虚拟机不通外网,可以在本地虚拟机下载离线安装包
yum install -y gcc gcc-c++ make pcre-devel zlib-devel openssl-devel wget --downloadonly --downloaddir=./
#下载完成后将所有的单rpm包传到服务器上,然后安装
yum -y localinstall ./*.rpm

2.编译安装 Nginx

在所有 Nginx 服务器上执行:
(1) 下载并解压源码
wget https://nginx.org/download/nginx-1.24.0.tar.gz
tar -zxvf nginx-1.24.0.tar.gz -C /data && cd /data/nginx-1.24.0
(2) 配置编译参数
./configure \
  --prefix=/usr/local/nginx \
  --with-http_ssl_module \
  --with-http_gzip_static_module \
  --with-http_stub_status_module
(3) 编译并安装
make && make install
(4) 创建软链接方便使用
ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx

3. 配置 Nginx 负载均衡 (主备负载均衡配置相同)

cat /usr/local/nginx/conf/load.conf
http {
  upstream backend {
    server 10.0.0.143:80 weight=5;  # 应用服务器1
    server 10.0.0.71:80 weight=5;  # 应用服务器2
  }

  server {
    listen 80;
    server_name 10.0.0.100;

    location / {
      proxy_pass http://backend;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
    }
  }
}

4.安装并配置 Keepalived

yum install -y keepalived
# 不通外网同上
(1)主负载均衡器配置
cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
  router_id LVS_DEVEL
}

vrrp_instance VI_1 {
  state MASTER
  interface eth0
  virtual_router_id 51
  priority 100
  advert_int 1
  authentication {
    auth_type PASS
    auth_pass 1111
  }
  virtual_ipaddress {
    10.0.0.100  # VIP地址
  }
}

virtual_server 10.0.0.100 80 {
  delay_loop 6
  lb_algo rr
  lb_kind NAT
  persistence_timeout 50
  protocol TCP

  real_server 10.0.0.141 80 {
    weight 1
    SSL_GET {
      url { path /; digest ff20ad2481f8cd3a1864ed534ff456 }
      url { path /mrtg/; digest 9b3a0c85a887256d6939da88aabd8cd }
      connect_timeout 3
      retry 3
      delay_before_retry 3
    }
  }
}

(2)备用负载均衡器配置
cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
  router_id LVS_DEVEL
}

vrrp_instance VI_1 {
  state BACKUP
  interface eth0
  virtual_router_id 51
  priority 90
  advert_int 1
  authentication {
    auth_type PASS
    auth_pass 1111
  }
  virtual_ipaddress {
    10.0.0.100  # VIP地址
  }
}

virtual_server 10.0.0.100 80 {
  delay_loop 6
  lb_algo rr
  lb_kind NAT
  persistence_timeout 50
  protocol TCP

  real_server 10.0.0.141 80 {
    weight 1
    SSL_GET {
      url { path /; digest ff20ad2481f8cd3a1864ed534ff456 }
      url { path /mrtg/; digest 9b3a0c85a887256d6939da88aabd8cd }
      connect_timeout 3
      retry 3
      delay_before_retry 3
    }
  }
}

# router_id主备要保持一致;state主是MASTER,备是BACKUP;priority优先级备低于主服务器

5.启动服务

1.启动nginx
/usr/sbin/nginx 
2.启动Keepalived并设置开机自启
systemctl start keepalived
systemctl enable keepalived

6.验证部署

1.检查 VIP 状态
ip -c a 或者 ip addr show eth0
inet 10.0.0.100/32 scope global eth0 # 主服务器应显示VIP
2.测试负载均衡
curl http://10.0.0.100  # 多次请求应轮询到不同后端服务器
3.模拟故障转移
# 在主服务器上停止Keepalived
systemctl stop keepalived
# 检查VIP是否漂移到备用服务器
ip -c a 或者 ip addr show eth0
1

评论 (0)

取消