nginx_负载均衡_集群


1 .yum install -y nginx   /etc/nginx/conf.d/  名字必须是.conf为后缀名

2 .编译安装nginx一台 ,其余俩台yum安装的

3 .vi /etc/hosts 写入 ip

192.168.1.123 rs1

192.168.1.128.rs2

#192.168.1.125 www.aaa.com 分发器机器上的ip


编译安装有这个目录;

[root@cenots003 ~]# cd /usr/local/nginx/conf/     yum安装的 #cd /etc/nginx/conf.d/
[root@cenots003 conf]# mkdir vhosts
[root@cenots003 conf]# cd vhosts

[root@cenots003 vhosts]# vim lb.conf 写入如下:

upstream aming {
        server 192.168.1.113:80 weight=2 权重;
        server 192.168.1.114:80 weight=1;
}


server {
   listen 80;
   server_name www.aaa.com;
   location / {
       proxy_pass http://aming/;
      # proxy_set_header Host $host;
   }
}

:wq保存退出,清除之前的规则。其他俩台机也要开启nginx

[root@cenots003 vhosts]# ipvsadm -C

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


编辑  /usr/local/nginx/conf/nginx.conf 插入俩段在下面

注销掉里面的sever 段。

    gzip_http_version 1.1;


    include   /usr/local/nginx/conf/vhosts/*.conf;
    gzip_types text/plain application/x-javascript text/css text/htm application/xml;


#server
#{
#    listen 80;
#    server_name localhost;
#    index index.html index.htm index.php;
#    root /usr/local/nginx/html;
#
#    location ~ \.php$ {
#        fastcgi_pass unix:/tmp/php-fcgi.sock;
#        fastcgi_index index.php;
#        fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
#    }
#
#}

}    这个不用注销,带井号的要注销           

 


整段中加入,include 文件路径

include   /usr/local/nginx/conf/vhosts/*.conf;
gzip_types text/plain application/x-javascript text/css text/htm application/xml;

保存退出


[root@cenots003 ~]# /usr/local/nginx/sbin/nginx -t


[root@cenots003 ~]# /etc/init.d/nginx reload


[root@cenots003 ~]# service nginx start


[root@cenots003 ~]# netstat -lnp|grep nginx


server 192.168.1.123的机也打开nginx

[root@cenots003 ~]# curl -x192.168.1.123:80 www.aaa.com
111111111111111111master

[root@cenots003 ~]# curl -x192.168.1.128:80 www.aaa.com
222222222222222222222slave


测试 :


[root@cenots003 vhosts]# curl -xlocalhost:80 www.aaa.com

会先2次 master 和一次 slave


[root@YQ002 vhosts]# curl -xlocalhost:80 www.aaa.com
111111111111111111master
[root@YQ002 vhosts]# curl -xlocalhost:80 www.aaa.com
222222222222222222222slave
[root@YQ002 vhosts]# curl -xlocalhost:80 www.aaa.com
111111111111111111master
[root@YQ002 vhosts]# curl -xlocalhost:80 www.aaa.com
111111111111111111master
[root@YQ002 vhosts]# curl -xlocalhost:80 www.aaa.com
222222222222222222222slave
[root@YQ002 vhosts]# curl -xlocalhost:80 www.aaa.com
111111111111111111master
[root@YQ002 vhosts]# curl -xlocalhost:80 www.aaa.com
111111111111111111master
[root@YQ002 vhosts]# curl -xlocalhost:80 www.aaa.com
222222222222222222222slave

成功!










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值