负载均衡

反向代理【跳转不行?】

携带头信息[Host 客户端真实IP]
连接、响应、返回、buff

前端监听80端口 -> 后端的8080

1.是否必须统一前端的代理端口和后端的web服务器端口【不需要】
2.前端使用80端口,后端使用8080,8081,8082,就不在需要域名【可以|不是很建议】
3.最常见,最建议的方式
前端配置好blog.oldboy.com 域名,配置好监听的80端口,配置好proxy_pass
后端配置好blog.oldboy.com 域名,配置好监听的80端口

负载均衡具有反向代理的功能

反向代理  -> 仅能代理一台服务器
负载均衡  -> 可以代理(多台,集群)
proxy_pass      http协议
fastcgi_pass    fastcgi协议

负载均衡的名词

调度
    前端
    SLB  Server Load Balance
    LB   Load Balance
    SLB  阿里云
    CLB  腾讯云
    ULB  Ucloud

Nginx负载均衡按层划分【OSI】

    负载均衡按层划分应用场景: 四层负载均衡 tcp/udp协议   只能转发 端口
    负载均衡按层划分应用场景: 七层负载均衡, http协议      Nginx最常用

web01操作如下:

[root@web01 ~]# cd /etc/nginx/conf.d/
[root@web01 conf.d]# cat node.conf 
server {
    listen 80;
    server_name node.oldboy.com;
    location / {
        root /node;
        index index.html;
    }
}
[root@web01 conf.d]# mkdir /node
[root@web01 conf.d]# echo "Web01..." > /node/index.html
[root@web01 conf.d]# systemctl restart nginx

web02操作如下:

[root@web02 ~]# cd /etc/nginx/conf.d/
[root@web02 conf.d]# cat node.conf 
server {
    listen 80;
    server_name node.oldboy.com;
    location / {
        root /node;
        index index.html;
    }
}
[root@web02 conf.d]# mkdir /node
[root@web02 conf.d]# echo "Web02..." > /node/index.html
[root@web02 conf.d]# systemctl restart nginx

lb01操作如下:

[root@lb01 ~]# cd /etc/nginx/conf.d/
[root@lb01 conf.d]# vim node_proxy.conf
[root@lb01 conf.d]# cat node_proxy.conf 
upstream node {
    server 172.16.1.7:80;
    server 172.16.1.8:80;
}

server {
    listen 80;
    server_name node.oldboy.com;

    location / {
        proxy_pass http://node;
        include proxy_params;
    }
}
[root@lb01 conf.d]# systemctl restart nginx

状态         概述

down 当前的server暂时不参与负载均衡
backup 预留的备份服务器
max_fails 允许请求失败的次数
fail_timeout 经过max_fails失败后, 服务暂停时间
max_conns 限制最大的接收TCP连接数

测试down状态, 测试该Server不参与负载均衡的调度

upstream load_pass {
    //不参与任何调度, 相当于注释
    server 10.0.0.7:80 down;
}

测试backup以及down状态

upstream load_pass {
    server 10.0.0.7:80;
    server 10.0.0.8:80 backup;
}

测试max_fails失败次数和fail_timeout 多少时间内失败多少次则标记down

upstream load_pass {
    server 10.0.0.7:80;
    server 10.0.0.8:80 max_fails=2 fail_timeout=10s;
}

测试max_conns最大TCP连接数

upstream load_pass {
    server 10.0.0.7:80;
    server 10.0.0.8:80 max_conns=1;
}

Nginx调度策略

调度算法 概述

轮询          按时间顺序逐一分配到不同的后端服务器(默认)
weight        加权轮询,weight值越大,分配到的访问几率越高
ip_hash       每个请求按访问IP的hash结果分配,这样来自同一IP的固定访问一个后端服务器
url_hash      按照访问URL的hash结果来分配请求,是每个URL定向到同一个后端服务器
least_conn    最少链接数,那个机器链接数少就分发

加权轮询-->针对http请求

upstream node {
        server 172.16.1.7:80 weight=5;
        server 172.16.1.8:80;
}

.Nginx负载均衡ip_hash具体配置, 不能和weight一起使用。

upstream node {
//如果客户端都走相同代理, 会导致某一台服务器连接过多
        ip_hash;
        server 172.16.1.7:80;
        server 172.16.1.8:80;
}

Nginx负载均衡url_hash具体配置

upstream load_pass {
    hash $request_uri;
    server 192.168.56.11:8001;
    server 192.168.56.11:8002;
    server 192.168.56.11:8003;
}
//针对三台服务器添加相同文件
/soft/code1/url1.html url2.html url3.html
/soft/code2/url1.html url2.html url3.html
/soft/code3/url1.html url2.html url3.html

192.168.56.100/url1.html
ur1.html   》 192.168.56.11:8080 -> url1.html
ur1.html   》 192.168.56.11:8081 -> url1.html
ur1.html   》 192.168.56.11:8082 -> url1.html

示例:

upstream blog {
        server 172.16.1.7:80 max_fails=2 fail_timeout=10s;
        server 172.16.1.8:80 max_fails=2 fail_timeout=10s;
        server 172.16.1.9:80 backup;
}

将能正常请求的blog\edu\zh的两台服务器,前端增加一个负载均衡 

[root@lb01 conf.d]# cat proxy.conf 
upstream php{
    server 172.16.1.7:80;
    server 172.16.1.8:80;
    server 172.16.1.9:80;
}
server {
    listen 80;
    server_name blog.oldboy.com;
    location / {
        proxy_pass http://php;
        include proxy_params;
    }
}
server {
    listen 80;
    server_name edu.oldboy.com;
    location / {
        proxy_pass http://php;
        include proxy_params;
    }
}
server {
    listen 80;
    server_name zh.oldboy.com;
    location / {
        proxy_pass http://php;
        include proxy_params;
    }
}

负载均衡具有反向代理的功能

反向代理  -> 仅能代理一台服务器
负载均衡  -> 可以代理(多台,集群)
proxy_pass      http协议
fastcgi_pass    fastcgi协议

 

posted @ 2018-09-26 16:57  w浅时光  阅读(304)  评论(0编辑  收藏  举报