1.七层负载均衡

web服务器配置

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


1.web01和web02 配置 
[root@web01 ~]# vim /etc/nginx/conf.d/url.oldyang.com.conf

server {
        listen 80;
        server_name url.oldyang.com;
        root /code;

        location / {
                index index.html;
        }
}

[root@web01 ~]# echo "pass" > /code/index.html
[root@web01 ~]# systemctl restart nginx

[root@web02 ~]# vim /etc/nginx/conf.d/url.oldyang.com.conf 
server {
        listen 80;
        server_name url.oldyang.com;
        root /code;

        location / {
                index index.html;
        }
}


[root@web02 ~]# echo "user" > /code/index.html
[root@web02 ~]# systemctl restart nginx

lb负载均衡服务器配置

[root@lb-01 ~]# vim /etc/nginx/conf.d/proxy_url.oldyang.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 ~]# systemctl restart nginx

PS:在使用proxy_pass 反向代理时,最后结尾添加 / 和不添加 / 有什么区别

1.不添加  /
	用户如果请求 :http://url.oldyang.com/user
	会被代理后端 :http://url.oldyang.com/user
	
2.添加	/	
	用户如果请求 :http://url.oldyang.com/user
	会被代理后端 :http://url.oldyang.com/
	
加不加	/:	
PS:https://www.jianshu.com/p/2f88cbc5bcf1

根据不同的终端调度不同的集群

#web服务器配置
10.0.0.5
10.0.0.7		pc
10.0.0.8		phone

[root@web01 ~]# vim /etc/nginx/conf.d/agent.oldyang.com.conf 

server {
        listen 80;
        server_name agent.oldyang.com;
        root /code;

        location / {
                index index.html;
        }
}

[root@web01 ~]# echo "pc" > /code/index.html
[root@web01 ~]# systemctl restart nginx

[root@web02 ~]# vim /etc/nginx/conf.d/agent.oldyang.com.conf 

server {
        listen 80;
        server_name agent.oldxu.com;
        root /code;

        location / {
                index index.html;
        }

}

[root@web02 ~]# echo "phone" > /code/index.html
[root@web02 ~]# systemctl restart nginx

代理配置

[root@lb-01 ~]# vim /etc/nginx/conf.d/proxy_agent.oldyang.com.conf 

upstream pc {
        server 172.16.1.7:80;
}

upstream phone {
        server 172.16.1.8:80;
}

server {
        listen 80;
        server_name agent.oldyang.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>';
                }
        }
}

[root@lb-01 ~]# systemctl rstart nginx

多级负载如何透传真是客户端ip
X-Forwarded-For
realip  (知道经过了那些代理  代理的IP又是多少)

2.四层负载均衡

1.什么是四层负载均衡

四层指的是OSI模型的第四层
传输层
tcp/udp协议

2.四层负载均衡的使用场景

1.四层负载均衡 + 七层负载均衡
2.dns + 多机房 + 四层负载均衡 + 七层负载均衡
3.SOA 	松耦合架构
	最典型的是京东:
		登录		passport.jd.com
		注册		reg.jd.com
		商品详情	pro.jd.com

3.nginx四层负载均衡

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;
	}
}

4.nginx四层 + nginx七层 + web集群

1.定义四层配置文件路径
[root@lb-01 ~]# 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;
	}
}

4.基于端口的转发:   
		需求: 用户连接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;
			}
		}
		
		
5.四层负载均衡怎么记录日志   必须在stream层,不能出现在http层?
	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;

posted on 2021-12-24 15:56  杨港澳  阅读(255)  评论(0)    收藏  举报