1. 环境规划:
主机名 | 主机地址 | 角色 |
node1 | 192.168.188.11 | 负载调度器 |
node2 | 192.168.188.12 | RS1 |
node3 | 192.168.188.13 | RS2 |
node4 | 192.168.188.14 | 测试主机 |
2. Nginx配置负载均衡加反向代理:
1>. node1,node2,node3安装Nginx:
[root@node1 ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com
/repo/epel-7.repo
[root@node1 ~]# yum install nginx -y
2>. node2,node3配置nginx主页:
##node2,node3配置相同
[root@node2 ~]# echo "`hostname -I`" > /usr/share/nginx/html/index.html
[root@node2 ~]# systemctl start nginx.service
3>. node1配置Nginx的负载均衡和反向代理:
[root@node1 ~]# vim /etc/nginx/nginx.conf
http {
......
upstream wwwServerPools { ##"wwwServerPools"可以自定义,但是新版本不能再
使用下划线
server 192.168.188.12:80 weight=1;
server 192.168.188.13:80 weight=1;
server 127.0.0.1:80 backup; ##表示如果两台后端服务器全部宕机,则访问127.0.0.1:80
}
......
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://wwwServerPools; ##访问node1时将请求发给wwwServerPools
里面的节点
}
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
注意:1. upstream是关键字必须有,后面的wwwServer