Nginx-负载均衡部署

Nginx的负载均衡是比较常见的,通过反向代理把相应的请求发给不同的server;

Nginx的一个优点:Nginx可以自己进行健康检查,发现故障server会自动剔除,修复后自动添加;

这里我们需要5台虚拟机进行部署;

1台nginxserver负责反向代理的负载均衡;

4台作为Apache server;其中2台模拟html;2台模拟php;

配置Nginx.conf

[root@sxb-1 conf]# vim nginx.conf
upstream htmlserver {
        server 192.168.88.102:80;
        server 192.168.88.103:80;
}

upstream phpserver {
        server 192.168.88.104:80;
        server 192.168.88.105:80;
}


    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;


        location ~* \.html$ {
                proxy_pass      http://htmlserver;

        }

        location ~* \.php$ {
                proxy_pass      http://phpserver;
        }

基础搭建我们就可以使用了

测试:

[root@sxb-1 ~]# curl 192.168.88.101/index.html
103 html
[root@sxb-1 ~]# curl 192.168.88.101/index.html
102 html
[root@sxb-1 ~]# curl 192.168.88.101/index.html
103 html
[root@sxb-1 ~]# curl 192.168.88.101/index.html
102 html
[root@sxb-1 ~]# curl 192.168.88.101/index.php
104 php
[root@sxb-1 ~]# curl 192.168.88.101/index.php
105 php
[root@sxb-1 ~]# curl 192.168.88.101/index.php
104 php
[root@sxb-1 ~]# curl 192.168.88.101/index.php
105 php

测试 自动剔除、自动添加:

对102 进行防火墙策略 如果添加的策略是DROP,测试是会发生卡顿;(DROP为丢弃,Nginx会持续发送,直到超时;)

[root@sxb-1 ~]# iptables -t filter -A INPUT -p tcp --dport 80 -j REJECT
[root@sxb-1 ~]# curl 192.168.88.101/index.html
103 html
[root@sxb-1 ~]# curl 192.168.88.101/index.html
103 html
[root@sxb-1 ~]# curl 192.168.88.101/index.html
103 html
[root@sxb-1 ~]# curl 192.168.88.101/index.html
103 html

清除策略:

[root@sxb-1 ~]# iptables -F
[root@sxb-1 ~]# curl 192.168.88.101/index.html
103 html
[root@sxb-1 ~]# curl 192.168.88.101/index.html
103 html
[root@sxb-1 ~]# curl 192.168.88.101/index.html
102 html
[root@sxb-1 ~]# curl 192.168.88.101/index.html
103 html
[root@sxb-1 ~]# curl 192.168.88.101/index.html
102 html
[root@sxb-1 ~]# curl 192.168.88.101/index.html
103 html
[root@sxb-1 ~]# curl 192.168.88.101/index.html
102 html
[root@sxb-1 ~]# curl 192.168.88.101/index.html
103 html

猜你喜欢

转载自www.cnblogs.com/loganSxb/p/11278769.html