部署后端Web1服务器
[root@web1 ~]# yum -y install httpd[root@web1 ~]# echo "192.168.2.100" > /var/www/html/index.html[root@web1 ~]# systemctl restart httpd[root@web1 ~]# firewall-cmd --set-default-zone=trusted[root@web1 ~]# setenforce 0
部署后端Web2服务器
[root@web2 ~]# yum -y install httpd[root@web2 ~]# echo "192.168.2.200" > /var/www/html/index.html[root@web2 ~]# systemctl restart httpd[root@web2 ~]# firewall-cmd --set-default-zone=trusted[root@web2 ~]# setenforce 0
配置Nginx服务器,添加服务器池,实现反向代理功能
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf.. ..http {.. ..#使用upstream定义后端服务器集群,集群名称任意(如webserver)#使用server定义集群中的具体服务器和端口upstream webserver { server 192.168.2.100:80; server 192.168.2.200:80; }.. ..server { listen 80; server_name localhost; location / { #通过proxy_pass将用户的请求转发给webserver集群 proxy_pass http://webserver; }}
配置upstream服务器集群池属性
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf.. ..http {.. ..upstream webserver { server 192.168.2.100 weight=1 max_fails=1 fail_timeout=30; server 192.168.2.200 weight=2 max_fails=2 fail_timeout=30; server 192.168.2.101 down; }#weight设置服务器权重值,默认值为1#max_fails设置最大失败次数#fail_timeout设置失败超时时间,单位为秒#down标记服务器已关机,不参与集群调度.. ..server { listen 80; server_name localhost; location / { proxy_pass http://webserver; }}
配置upstream服务器集群的调度算法
[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf.. ..http {.. ..upstream webserver {#通过ip_hash设置调度规则为:相同客户端访问相同服务器 ip_hash; server 192.168.2.100 weight=1 max_fails=2 fail_timeout=10; server 192.168.2.200 weight=2 max_fails=2 fail_timeout=10; }.. ..server { listen 80; server_name www.tarena.com; location / { proxy_pass http://webserver; }}