nginx四層負載均衡


1、七層負載均衡:

根據url 調度不同的集群   url.yangdan.com
10.0.0.5
10.0.0.7		/pass
10.0.0.8		/user

#1.web01和web02配置  (只不過代碼不一樣)
[root@web01 conf.d]# cat url.yangdan.com.conf 
server {
	listen 80;
	server_name url.yangdan.com;
	root /code;

	location / {
		index index.html;
	}
}

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

server {
	listen 80;
	server_name url.yangdan.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
	
	PS: 在使用proxy_pass反向代理時,最后結尾添加/和不添加/有什么區別?
	
	1.不添加 / 
		用戶如果請求:    http://url.yangdan.com/user
		會被代理至后端:  http://url.yangdan.com/user
	
	1.添加 / 
		用戶如果請求: http://url.yangdan.com/user
		會被代理至后端:  http://url.yangdan.com/
根據設備調度不同的集群   			( 瀏覽器 )  ( 手機 )
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.yangdan.com.conf 
server {
	listen 80;
	server_name agent.yangdan.com;
	root /code;

	location / {
		index index.html;
	}

}

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

upstream phone {
	server 172.16.1.8:80;
}

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

2、四層負載均衡

#1.什么是四層  OSI  傳輸層  TCP/IP     UDP/TCP
	四層是基於轉發方式:
	
#2.四層負載均衡使用場景
	#1.四層負載均衡 + 七層負載均衡
	#2.dns + 多機房 + 四層負載均衡+七層負載均衡
	#3.SOA 松耦合架構
		登錄		passport.jd.com
		注冊		reg.jd.com
		商品詳情	pro.jd.com

#4.基於端口的轉發
				nginx 7層		web01		MySQL
nginx 4層  + 					web02		NFS
				nginx 7層		web03		Redis
				10.0.0.6

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

3、基於端口的轉發:

需求: 用戶連接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.yangdan.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;
	}
}

4、四層負載均衡怎么記錄日志 必須在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;

#配置阿里雲四層負載均衡   實現端口轉發  
		公網666轉到內網的22
		公網80 轉到內網的多台7層負載均衡的80  ?


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM