Nginx负载均衡部署——ip_hash方式
·
环境
Linux版本:CentOS7.6 64位
db:192.168.90.171
web01: 192.168.90.172
web02: 192.168.90.173
服务器准备工作(三台服务器一样的操作)
1.关闭selinux
vim /etc/selinux/config
SELINUX=disabled
2.关闭firewall,清空iptables
systemctl stop firewalld.service
systemctl disable firewalld.service
iptables -L
iptables -F
编译安装nginx
1.下载nginx-1.16.0.tar.gz
wget https://nginx.org/download/nginx-1.16.0.tar.gz
2.解压nginx包并配置安装
tar -zxvf nginx-1.16.0.tar.gz
cd nginx-1.16.0
./configure --prefix=/usr/local/nginx
make
make install
3.测试是否安装成功
cd /usr/local/nginx/
/usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx
配置db负载均衡服务器
[root@db nginx]# vim /usr/local/nginx/conf/nginx.conf
#配置域名为test.com的主机
server {
listen 80;
server_name www.test.com;
location / {
proxy_pass http://web_server;
}
}
#配置负载均衡服务器组名称和地址
upstream web_server {
ip_hash;
server 192.168.90.72;
server 192.168.90.73;
}
location / {
#root html;
index index.php index.html index.htm;
}
[root@db ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@db ~]# /usr/local/nginx/sbin/nginx -s reload
配置web01和web02
1.web01配置
[root@web01 nginx]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name 192.168.90.172;
root /var/www/html;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root html;
#添加index.php
index index.php index.html index.htm;
}
[root@web01 ~]# echo "192.168.90.72" >> /var/www/html/index.html
[root@web01 ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@web01 ~]# /usr/local/nginx/sbin/nginx -s reload
2.web02跟web01一样的操作
测试
找另外一台虚拟机测试,需要在该虚拟机对192.168.90.71的域名做解析
[root@centos7 ~]# vim /etc/hosts
192.168.90.71 www.test.com
[root@centos7 ~]# curl www.test.com
192.168.90.73
[root@centos7 ~]# curl www.test.com
192.168.90.73
[root@centos7 ~]# curl www.test.com
192.168.90.73
总结
ip_hash测试是成功的,经过多次测试,发现一个IP仅被调度到一个服务器上,这是因为ip_hash方式为每一个用户IP绑定一个Web服务器处理,这样每个用户都固定访问一个后端服务器.
更多推荐



所有评论(0)