nginx负载均衡

针对域名作负载均衡(还可以针对目录跳转到相应的机器)

vi /etc/nginx/conf.d/lb.conf

upstream hy {
  server 192.168.11.160:80;
  server 192.168.11.20:80;
}

server {

   listen 80;
   server_name www.123.com;
   location / {
    proxy_pass      http://hy/;
    proxy_set_header Host   $host;
    }
}

 

测试一:

curl -xlocalhost:80 www.123.com

[root@dir conf.d]# curl -xlocalhost:80 www.123.com
rs1rs1
[root@dir conf.d]# curl -xlocalhost:80 www.123.com
rs2rs2
[root@dir conf.d]# curl -xlocalhost:80 www.123.com
rs1rs1
[root@dir conf.d]# curl -xlocalhost:80 www.123.com
rs2rs2
[root@dir conf.d]# curl -xlocalhost:80 www.123.com
rs1rs1
[root@dir conf.d]# curl -xlocalhost:80 www.123.com
rs2rs2
View Code

测试二:

upstream hy {
  server 192.168.11.160:80 weight=2;
  server 192.168.11.20:80 weight=1;
}

server {

   listen 80;
   server_name www.123.com;
   location / {
    proxy_pass      http://hy/;
    proxy_set_header Host   $host;
    }
}
View Code
[root@dir conf.d]# curl -xlocalhost:80 www.123.com
rs2rs2
[root@dir conf.d]# curl -xlocalhost:80 www.123.com
rs1rs1
[root@dir conf.d]# curl -xlocalhost:80 www.123.com
rs1rs1
[root@dir conf.d]# curl -xlocalhost:80 www.123.com
rs2rs2
[root@dir conf.d]# curl -xlocalhost:80 www.123.com
rs1rs1
[root@dir conf.d]# curl -xlocalhost:80 www.123.com
rs1rs1
View Code

 

posted @ 2016-02-19 17:29  沐风先生  阅读(231)  评论(0编辑  收藏  举报