nginx负载均衡配置

nginx通常被用作后端服务器的反向代理,这样就可以很方便的实现动静分离以及负载均衡,从而大大提高服务器的处理能力

nginx实现动静分离,其实就是在反向代理的时候,如果是静态资源,就直接从nginx发布的路径去读取,而不需要从后台服务器获取了。

但是要注意,这种情况下需要保证后端跟前端的程序保持一致,可以使用Rsync做服务端自动同步或者使用NFS、MFS分布式共享存储。

Http Proxy模块,功能很多,最常用的是proxy_pass和proxy_cache

如果要使用proxy_cache,需要集成第三方的ngx_cache_purge模块,用来清除指定的URL缓存。这个集成需要在安装nginx的时候去做,如:

./configure --add-module=…/ngx_cache_purge-1.0 …

nginx通过upstream模块来实现简单的负载均衡,upstream需要定义在http段内

在upstream段内,定义一个服务器列表,默认的方式是轮询,如果要确定同一个访问者发出的请求总是由同一个后端服务器来处理,可以设置ip_hash,如:

upstream idfsoft.com {
  ip_hash;
  server 127.0.0.1:9080 weight=5;
  server 127.0.0.1:8080 weight=5;
  server 127.0.0.1:1111;
}

注意:这个方法本质还是轮询,而且由于客户端的ip可能是不断变化的,比如动态ip,代理,翻墙等,因此ip_hash并不能完全保证同一个客户端总是由同一个服务器来处理。

定义好upstream后,需要在server段内添加如下内容:

server {
  location / {
    proxy_pass http://idfsoft.com;
  }
}

nginx部署负载均衡
环境准备:

主机

IP

服务

系统

nginx

192.168.58.130

nginx

CentOS-8.5

web1

192.168.58.135

http

CentOS-8.5

web2

192.168.160.138

nginx

CentOS-8.5

web1主机部署httpd
参考: httpd部署 web2主机部署nginx
参考: nginx部署 nginx主机操作

//创建系统用户nginx
[root@nginx ~]# useradd -r -M -s /sbin/nologin nginx

//安装依赖环境
[root@nginx ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make wget
[root@nginx ~]# yum -y groups mark install 'Development Tools'

//创建日志存放目录
[root@nginx ~]# mkdir -p /var/log/nginx
[root@nginx ~]#  chown -R nginx.nginx /var/log/nginx

//下载nginx
[root@nginx ~]# cd /usr/src/
[root@nginx src]# wget http://nginx.org/download/nginx-1.20.2.tar.gz

//下载ngx_cache_purge模块,再编译安装
[root@nginx src]# tar xf nginx-1.20.2.tar.gz 
[root@nginx src]# cd nginx-1.20.2
[root@nginx nginx-1.20.2]# git clone https://gitee.com/ye-xiao-tian/ngx_cache_purge.git
[root@nginx nginx-1.20.2]# ls
auto     CHANGES.ru  configure  html     man              README
CHANGES  conf        contrib    LICENSE  ngx_cache_purge  src
[root@nginx nginx-1.20.2]# ./configure --prefix=/usr/local/nginx 
--user=nginx --group=nginx --with-debug --with-http_ssl_module 
--with-http_realip_module --with-http_image_filter_module 
--with-http_gunzip_module --with-http_gzip_static_module 
--with-http_stub_status_module --http-log-path=/var/log/nginx/access.log 
--error-log-path=/var/log/nginx/error.log 
--add-module=/usr/src/nginx-1.20.2/ngx_cache_purge
[root@nginx nginx-1.20.2]# make && make install

//配置环境变量
[root@nginx ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@nginx ~]# . /etc/profile.d/nginx.sh
[root@nginx ~]# nginx
[root@nginx ~]# ss -anlt
State    Recv-Q   Send-Q     Local Address:Port     Peer Address:Port  Process   
LISTEN   0        128              0.0.0.0:80            0.0.0.0:*               
LISTEN   0        128              0.0.0.0:22            0.0.0.0:*               
LISTEN   0        128                 [::]:22               [::]:*  

//编写service文件
[root@nginx ~]# cd /usr/lib/systemd/system
[root@nginx system]# vi nginx.service 

[Unit]
Description=nginx server daemon
After=network.target sshd-keygen.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp= true

[Install]
WantedBy=multi-user.target

//修改nginx配置文件,实现负载均衡
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
#在http段内添加
        upstream yxt.com {
            server 192.168.58.135;
            server 192.168.58.138;
       }

#在server段内添加
 location / {
            proxy_pass http://yxt.com;
        }
[root@nginx ~]# systemctl restart nginx
[root@nginx ~]# curl 192.168.58.130
apache
[root@nginx ~]# curl 192.168.58.130
nginx
[root@nginx ~]# curl 192.168.58.130
apache
[root@nginx ~]# curl 192.168.58.130
nginx

nginx负载均衡调度器高可用配置

主机

IP

服务

系统

master

192.168.58.139

nginx 、keepalived

CentOS-8.5

nginx

192.168.58.130

nginx

CentOS-8.5

web1

192.168.58.135

http

CentOS-8.5

web2

192.168.160.138

nginx

CentOS-8.5

master主机操作:
部署nginx服务
参考: nginx部署

//修改nginx配置文件,实现负载均衡
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
#在http段内添加
        upstream yxt.com {
            server 192.168.58.135;
            server 192.168.58.138;
       }

#在server段内添加
 location / {
            proxy_pass http://yxt.com;
        }
[root@nginx ~]# systemctl restart nginx

#由于设置了apache的weight值,每次访问先访问三次apache
[root@master ~]# curl 192.168.58.139
apache
[root@master ~]# curl 192.168.58.139
apache
[root@master ~]# curl 192.168.58.139
apache
[root@master ~]# curl 192.168.58.139
nginx

//部署keepalived服务
[root@master ~]# yum -y install keepalived
[root@master ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
  
global_defs {
   router_id lb02
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass yexiaotian
    }
    virtual_ipaddress {
        192.168.58.250
    }
}

virtual_server 192.168.58.250 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 192.168.58.135 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

    real_server 192.168.58.130 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
     real_server 192.168.58.139 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

}

nginx主机操作:

[root@nginx ~]# yum -y install keepalived
[root@nginx ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
  
global_defs {
   router_id lb01
}

vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass yexiaotian
    }
    virtual_ipaddress {
        192.168.58.250
    }
}

virtual_server 192.168.58.250 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 192.168.58.130 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }

        real_server 192.168.58.139 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}
[root@nginx ~]# systemctl  restart keepalived

[root@nginx ~]# ip addr |grep '250'
    inet 192.168.58.250/32 scope global ens33

[root@nginx ~]# curl 192.168.58.250
apache
[root@nginx ~]# curl 192.168.58.250
apache
[root@nginx ~]# curl 192.168.58.250
apache
[root@nginx ~]# curl 192.168.58.250
nginx