day42——Nginx四层负载均衡

1.七层根据url不同调度

根据url 调度不同的集群 url.oldxu.com
10.0.0.5
10.0.0.7 /pass
10.0.0.8 /user

1.web01和web02配置  (只不过代码不一样)
[root@web01 conf.d]# cat url.oldxu.com.conf 
server {
    listen 80;
    server_name url.oldxu.com;
    root /code;

    location / {
        index index.html;
    }
}

2.lb配置
[root@lb01 conf.d]# cat proxy_url.oldxu.com.conf 
upstream user {
    server 172.16.1.8;
}
upstream pass {
    server 172.16.1.7;
}

server {
    listen 80;
    server_name url.oldxu.com;
    location / {
        proxy_pass http://user;
        include proxy_params;
    }
    location /user {
                proxy_pass http://user/;
                include proxy_params;
    }
    location /pass {
                proxy_pass http://pass;
                include proxy_params;
    }
}
#根据配置初始化

[root@lb01 conf.d]# systemctl restart nginx

2.反向代理时加/与不加/的区别

PS: 在使用proxy_pass反向代理时,最后结尾添加/和不添加/有什么区别?
在nginx中配置proxy_pass时,当在后面的url加上了/,相当于是绝对根路径,
则nginx不会把location中匹配的路径部分代理走;
如果没有/,则会把匹配的路径部分也给代理走。

proxy_pass反向代理时,最后结尾添加/和不添加/有什么区别?

详细解释:https://www.jianshu.com/p/2f88cbc5bcf1

1.不添加 / 
        用户如果请求:    http://url.oldxu.com/user
        会被代理至后端:  http://url.oldxu.com/user
    
1.添加 / 
        用户如果请求: http://url.oldxu.com/user
        会被代理至后端:  http://url.oldxu.com/

3.七层根据不同的设备匹配调度

根据设备调度不同的集群 ( 浏览器 ) ( 手机 )
10.0.0.5
10.0.0.7 pc
10.0.0.8 phone

1.所有的web都需要配置   ( 代码不一样)
[root@web01 conf.d]# cat /etc/nginx/conf.d/agent.oldxu.com.conf 
server {
    listen 80;
    server_name agent.oldxu.com;
    root /code;

    location / {
        index index.html;
    }

}

2.代理的配置
[root@lb01 conf.d]# cat proxy_agent.oldxu.com.conf 
upstream pc {
    server 172.16.1.7:80;
}

upstream phone {
    server 172.16.1.8:80;
}

server {
    listen 80;
    server_name agent.oldxu.com;
    location / {
        #默认都走pc
        proxy_pass http://pc;
        include proxy_params;
        default_type text/html;
        charset utf-8;

        #如果是安卓或iphone,则走phone
        if ( $http_user_agent ~* "android|iphone|iPad" ) {
            proxy_pass http://phone;
        }

        #如果是IE浏览器,要么拒绝,要么返回一个好的浏览器下载页面
        if ( $http_user_agent ~*  "MSIE" ) {
            return 200 '<a href="http://download.xuliangwei.com/gitlab-ce-8.3.4-ce.0.el7.x86_64.rpm" target="_blank">点击下载正版浏览器google.exe</a>';
        }
    }
}
多级负载下如何透传真实客户端IP?

x-forwar
realip (知道经过了那些代理 代理的IP又是多少)


4.四层负载均衡

4.1什么是四层负载均衡

四层负载均衡是基于转发方式,在传输层工作。

4.2四层负载均衡作用

四层负责转发,所以它没有端口限制。因为一个七层负载均衡端口限制65535,所以我们一般会在七层负载均衡集群前面加上一个四层负载均衡作为转发,七层以轮询的方式作为调度下发任务。

四层负载均衡.png

4.3四层负载均衡适用的场景

1.四层负载均衡 + 七层负载均衡
2.dns + 多机房 + 四层负载均衡+七层负载均衡
3.SOA 松耦合架构:就是将一个网站的每个功能都拆开来,组成一个集群。比如:京东网站某一天的登录界面坏了,但注册界面还是可以注册的。

​ 登录 passport.jd.com
​ 注册 reg.jd.com
​ 商品详情 pro.jd.com

4.4基于端口的转发

                nginx 7层        web01       MySQL
nginx 4层  +                     web02       NFS
                nginx 7层        web03       Redis
                10.0.0.6

nginx是1.9版本以后才引入的四层负载均衡
stream模块实现,但stream不能出现在http层
--with-stream
-with-stream_ssl_module
-with-stream_realip_module

stream {
    upstream backend {
    hash $remote_addr consistent;
    server backend1.example.com:12345 weight=5;
    server 127.0.0.1:12345 max_fails=3 fail_timeout=30s;
    server unix:/tmp/backend3;
}

server {
    listen 12345;
    proxy_connect_timeout 1s;
    proxy_timeout 3s;
    proxy_pass backend;
    }
}

5.nginx四层负载均衡配置

5.1定义四层配置路径

[root@lb-4 nginx]# vim /etc/nginx/nginx.conf
.......
events {
    worker_connections  1024;
}

index /etc/nginx/conf.c/*.conf;         #不能写在http层

http {
.......

5.2初始化环境

[root@lb-4 ~]# rm -f /etc/nginx/conf.d/default.conf
[root@lb-4 nginx]# mkdir /etc/nginx/conf.c

5.3配置四层负载均衡

[root@lb-4 ~]# cat /etc/nginx/conf.c/all.conf 
stream {                     #四层的配置模块,里面包含着七层(http层)和server层
    upstream all {
        server 172.16.1.5:80;   #写的就是七层负载均衡的IP地址
        server 172.16.1.6:80;
    }

    server {
        listen 80;
        proxy_pass all;
        proxy_timeout 3s;
        proxy_connect_timeout 3s; #连接的超时时间
    }
}

6.基于端口转发

需求: 用户连接10.0.0.4的6666端口,其实连接的是172.16.1.7的22/TCP端口
需求: 用户连接10.0.0.4的5555端口,其实连接的是172.16.1.51的3306/TCP端口

[root@lb-4 conf.c]# cat blog.oldxu.com.conf 
stream{
    upstream ssh{
        172.16.1.7:22;
    }
    upstream mysql{
        172.16.1.51:3306;
    }
    server{
        listen 6666;
        proxy_pass ssh;
    }
    server{
        listen 5555;
        proxy_pass mysql;
    }
}

7.四层负载均衡的日志

必须在stream层(四层的配置文件中),不能出现在http层
[root@lb-4 ~]# vim /etc/nginx/conf.c/all.conf 

stream{
#定义日志名字和内容
log_format proxy '$remote_addr -  [$time_local]  $status $protocol'
                '  "$upstream_addr" "$upstream_bytes_sent" "$upstream_connect_time"' ;

        access_log /var/log/nginx/all.log proxy;    #定义日志位置,引用日志模块名字

        upstream blog {
                server 172.16.1.5:80;
                server 172.16.1.6:80;
        }

        server {
                listen 80;
                proxy_pass blog;
                proxy_timeout 3s;
                proxy_connect_timeout 3s;
        }
}

8.配置阿里云四层负载均衡 实现端口转发

​ 公网666转到内网的22
​ 公网80 转到内网的多台7层负载均衡的80 ?

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。