nginx学习-负载均衡二

一、算法概念
rr算法:轮询(会有问题,当刷新时同一个地址可能到另一台主机上)默认算法。
ip_hash:刷新不会到另一台主机上,缺点是可能造成负载不均。

[root@elb conf]# vi nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
upstream backend {
    ip_hash;
    server 192.168.3.40:80      max_fails=3 fail_timeout=30s;
    server 192.168.3.49:80      max_fails=3 fail_timeout=30s;
}
    server {
        listen       80;
        server_name  www.wolf.com;
        index  index.html index.htm;
        location / {
           proxy_pass http://backend;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

[root@elb conf]# ../sbin/nginx -t
nginx: the configuration file /data/nginx1.6.2/conf/nginx.conf syntax is ok
nginx: configuration file /data/nginx1.6.2/conf/nginx.conf test is successful
[root@elb conf]# ../sbin/nginx -s reload
[root@elb tmp]# for n in `seq 100`;do curl 192.168.3.52;sleep 2;done
www.wolf.com
www.wolf.com
www.wolf.com

二、解决用户会话保持,但是又不要负载不均,所以在后端加用户的回话缓存信息。(redis和memcache)
一般方法有4-5种
1、lb层可以做会话保持
lvs -p,nginx  ip_hash,haproxy   cookie insert PHP,JAVA都可以
2、软件层可以做seesion复制,例如:tomcat,resin,couchbase
3、共享例如memcache的,或者其他的nosql工具,php常用这个    (常用)
4、门户站会用cookies或者cookis配合seesion把用户级会话缓存在用户本地。

三、dns配合
此时解析就需要在dns里配置指向elb,客户端访问elb即可。

四、代理多个主机的时候注意
proxy_pass http://backend;只能代理以上第一个
如下实验
[root@elb conf]# vi nginx.conf          
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;


    sendfile        on;
    keepalive_timeout  65;
upstream backend {
    server 192.168.3.40:80      max_fails=3 fail_timeout=30s;
    server 192.168.3.49:80      max_fails=3 fail_timeout=30s;
}
    server {
        listen       80;
        server_name  www.wolf.com;
        index  index.html index.htm;
        location / {
           proxy_pass http://backend;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }


server {
        listen       80;
        server_name  bbs.wolf.com;
        index  index.html index.htm;
        location / {
           proxy_pass http://backend;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
[root@elb conf]# for n in `seq 100`;do curl bbs.wolf.com;sleep 2;done
www.wolf.com
www.wolf.org
www.wolf.com
www.wolf.org
www.wolf.com
www.wolf.org

发现没出现bbs

这是需要增加以下参数,才能代理多个虚拟主机
proxy_pass http://backend;                                                         用于指向反向代理的服务器池
proxy_set_header Host $host;                                                  当后端web服务器上也配置有多个虚拟主机,需要用该Header来区分反向代理那个主机名
proxy_set_header   X-Forwarded-For  $remote_addr;          如果后端web服务器上的程序需要获取用户的ip,从该Header头获取

[root@elb conf]# vi nginx.conf   
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
upstream backend {
    server 192.168.3.40:80      max_fails=3 fail_timeout=30s;
    server 192.168.3.49:80      max_fails=3 fail_timeout=30s;
}
    server {
        listen       80;
        server_name  www.wolf.com;
        index  index.html index.htm;
        location / {
           proxy_pass http://backend;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }


server {
        listen       80;
        server_name  bbs.wolf.com;
        index  index.html index.htm;
        location / {
           proxy_pass http://backend;
           proxy_set_header Host $host;
           proxy_set_header   X-Forwarded-For  $remote_addr;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }


}

 ../sbin/nginx -t
../sbin/nginx -s reload
for n in `seq 100`;do curl bbs.wolf.com;sleep 2;done

[root@elb conf]# for n in `seq 100`;do curl bbs.wolf.com/index.html;sleep 2;done
http://bbs.wolf.com
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.8.1</center>
</body>
</html>
http://bbs.wolf.com
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.8.1</center>
</body>
</html>
发现配置成功,基于多域名的负载均衡

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值