Nginx与LVS的区别

nginx是一个代理,与LVS的NAT工作模式有点相似,但是在数据包传递的过程中不存在包转发的过程

nginx操作的是请求数据包,可针对域名、URL、目录代理到不同的RS,工作在7层;

LVS操作的是ICP/IP、MAC,工作在4层,操作简单速度快

配置 

1、 之前安装过nginx,就直接添加一个配置文件

[root@dr ~]# cd /usr/local/nginx/conf/vhosts/

[root@dr vhosts]# ls

default.conf

[root@dr vhosts]# vim lb.conf

upstream aming {

     server 192.168.10.13:80;

     server 192.168.10.14:80;

}

 

server {

       listen 80;

       server_name www.123.com;

       location / {            

         proxy_pass   http://aming/;

         proxy_set_header Host $host;

       }

}

 

2、 启动nginx

[root@dr vhosts]# /etc/init.d/nginx restart

 

3、 清空之前的规则

[root@dr vhosts]# ipvsadm -C

[root@dr vhosts]# iptables -t nat -F

[root@dr vhosts]# iptables -F

 

4、 查看监听端口

[root@dr vhosts]# netstat -lnp|grep nginx

tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      8517/nginx

 

5、 测试

[root@dr vhosts]# curl -xlocalhost:80 www.123.com

11111111

[root@dr vhosts]# curl -xlocalhost:80 www.123.com

222222

[root@dr vhosts]# curl -xlocalhost:80 www.123.com

11111111

[root@dr vhosts]# curl -xlocalhost:80 www.123.com

222222

[root@dr vhosts]# curl -xlocalhost:80 www.123.com

11111111

[root@dr vhosts]# curl -xlocalhost:80 www.123.com

222222

[root@dr vhosts]# curl -xlocalhost:80 www.123.com

 

6、 设置权重

[root@dr vhosts]# vim lb.conf

upstream aming {

     server 192.168.10.13:80 weight=2;

     server 192.168.10.14:80 weight=1;

}

 

server {

       listen 80;

       server_name www.123.com;

       location / {

         proxy_pass   http://aming/;

         proxy_set_header Host $host;

       }

}

 

7、 重启nginx

[root@dr vhosts]# /etc/init.d/nginx restart

 

8、 测试

[root@dr vhosts]# curl -xlocalhost:80 www.123.com

11111111

[root@dr vhosts]# curl -xlocalhost:80 www.123.com

11111111

[root@dr vhosts]# curl -xlocalhost:80 www.123.com

222222

[root@dr vhosts]# curl -xlocalhost:80 www.123.com

11111111

[root@dr vhosts]# curl -xlocalhost:80 www.123.com

11111111

[root@dr vhosts]# curl -xlocalhost:80 www.123.com

222222

 

9、 停掉其中一个

[root@rs1 ~]# /etc/init.d/nginx stop

 

10、 测试

[root@dr vhosts]# curl -xlocalhost:80 www.123.com

222222

[root@dr vhosts]# curl -xlocalhost:80 www.123.com

222222

[root@dr vhosts]# curl -xlocalhost:80 www.123.com

222222

[root@dr vhosts]# curl -xlocalhost:80 www.123.com

222222

解释说明:

能够把宕掉的给剔除掉

 

11、 再把停掉的给启动了

[root@rs1 ~]# /etc/init.d/nginx start

 

12、 再测试

[root@dr vhosts]# curl -xlocalhost:80 www.123.com

11111111

[root@dr vhosts]# curl -xlocalhost:80 www.123.com

11111111

[root@dr vhosts]# curl -xlocalhost:80 www.123.com

222222

[root@dr vhosts]# curl -xlocalhost:80 www.123.com

11111111

[root@dr vhosts]# curl -xlocalhost:80 www.123.com

11111111

[root@dr vhosts]# curl -xlocalhost:80 www.123.com

222222