nignx负载均衡:
EPEL是yum 的一个软件源,里面包含了许多基本源里没有的软件,下载所需的ngnix所需的包
将nginx用作非常有效的HTTP负载平衡器,以将流量分配到多个应用程序服务器,并使用nginx改善Web应用程序的性能,可伸缩性和可靠性。
yum install epel-release
yum install nginx
cp nginx.conf nginx.conf.bak
nginx支持以下负载平衡机制(或方法):
轮询-对应用程序服务器的请求以轮询方式分发,
最少连接-将下一个请求分配给活动连接最少的服务器,
ip-hash-哈希函数用于确定应为下一个请求选择哪个服务器(基于客户端的IP地址)。
http {
server srv1.example.com;
upstream myapp1 { #默认rr轮询
server srv2.example.com;
server srv3.example.com;
}
server {
listen 80;
location / {
proxy_pass http://myapp1;
}
}
}
http在/etc/nginx/nginx.conf文件中,由于http指令仅应发生一次,
直接在http 中间插入,并且删除默认值
vi /etc/nginx/nginx.conf
upstream myapp1 {
server www.t003.com;
server www.t002.com;
}
server {
listen 80;
location / {
proxy_pass http://myapp1;
}
}
server {
listen 80;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
vi /etc/hosts加入解析
192.168.20.10 www.t003.com
192.168.20.20 www.t002.com
systemctl restart nginx.service
测试:
rr 轮询方式成功!!!