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
成功!