Day-43 七层负载均衡

七层负载均衡

根据url不同来调度不同集群
#web端设置
[root@web1 conf.d]# vim url.conf
server {
    listen 80;
    servername url.com;
    root /code;
    client_max_body_size 100m;
    
    location / {
    index index.html;
    }
}
#负载均衡 
[root@lb01 conf.d]# vim url.conf
upstream url {
    server 172.16.1.7;
    server 172.16.1.8;
}
upstream pass {
    server 172.16.1.9;
}
server {
    listen 80;
    server_name url.com;
    
    location / {
        proxy_pass http://url;
        include proxy_params;
    }
    
    location /user {
        proxy_pass http://url
        include proxy_params;
    }
    
    location /pass {
        proxy_pass http://pass;
        include proxy_params;
    }
}

根据不同设备调度不同集群
#web
[root@web01 conf.d]# vim she.conf
server {
    listen 80;
    server_name she.com;
    root /code ;
    client_max_body_size 100m;
    
    location / {
        index index.html;
    }
}
#负载均衡
[root@lb01 conf.d]# vim she.conf
upstream pc {
    server 172.16.1.7;
    server 172.16.1.8;
}
upstream phone {
    server 172.16.1.9;
}

server {
    listen 80;
    server_name she.com;
    
    location / {
        proxy_pass http://pc;
        include proxy_params;
        default_type text/html;
        charset utf-8;
        
        if ( $http_user_agent ~* "android|iphone|iPad" ) {
            proxy_pass http://phone;
        if ( $http_user_agent ~* "MSIE" ) {
            return 200 '点击下载';
        }
        }
    }
}


四层负载均衡
  • 概念: OSI 传输层 TCP/IP UDP/TCP
  • 方式:

  • 使用场景

    1.四层负载均衡 + 七层负载均衡
    2.dns + 多机房 + 四层负载均衡+七层负载均衡
    3.SOA 松耦合架构

  • 基于端口的转发

#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;
        }
    }
    
nginx四层+nginx七层+web集群--->场景
#1.定义四层配置文件路径:
[root@lb-4 nginx]# vim /etc/nginx/nginx.conf
include /etc/nginx/conf.c/*.conf;   
#2.进行初始化操作
[root@lb-4 ~]# rm -f /etc/nginx/conf.d/default.conf
[root@lb-4 nginx]# mkdir /etc/nginx/conf.c
#3.配置四层负载均衡
[root@lb-4 ~]# cat /etc/nginx/conf.c/all.conf 
stream {
    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;
        }
}

基于端口的转发:

需求: 用户连接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 {
        server 172.16.1.7:22;
    }
    upstream mysql {
        server 172.16.1.51:3306;
    }
    
    server {
        listen 6666;
        proxy_pass ssh;
    }

    server {
        listen 5555;
        proxy_pass mysql;
    }
}

定义日志

#必须在stream层
log_format  proxy '$remote_addr -  [$time_local]  $status $protocol''"$upstream_addr" "$upstream_bytes_sent" "$upstream_connect_time"';
access_log /var/log/nginx/tcp.log proxy;

你可能感兴趣的:(Day-43 七层负载均衡)