nginx负载均衡的不同终端调度

1.基于uri请求调度到不同集群
  1. web01提供/user,使用多个端口模拟多个web节点
vim /etc/nginx/conf.d/agent.oldxu.conf
--------------/etc/nginx/conf.d/agent.oldxu.conf-------------
server {
	llisten 8080;
	server_name agent.oldxu.com;
	root /agent/8080;
	location / {
		index index.html;
	}
}
server {
	llisten 8081;
	server_name agent.oldxu.com;
	root /agent/8081;
	location / {
		index index.html;
	}
}
-------------/etc/nginx/conf.d/agent.oldxu.conf结束-----------
mkdir /agent/{8080,8081} -p
echo "user-8080" > /agent/8080/index.html;
echo "user-8081" > /agent/8081/index.html;
nginx -t
systemctl reload nginx
  1. web02提供/pass,使用多个端口模拟多个web节点
vim /etc/nginx/conf.d/agent.oldxu.conf
--------------/etc/nginx/conf.d/agent.oldxu.conf-------------
server {
	llisten 8080;
	server_name agent.oldxu.com;
	root /agent/8080;
	location / {
		index index.html;
	}
}
server {
	llisten 8081;
	server_name agent.oldxu.com;
	root /agent/8081;
	location / {
		index index.html;
	}
}
-------------/etc/nginx/conf.d/agent.oldxu.conf结束-----------
mkdir /agent/{8080,8081} -p
echo "pass-8080" > /agent/8080/index.html;
echo "pass-8081" > /agent/8081/index.html;
nginx -t
systemctl reload nginx
  1. 负载均衡根据uri调度
upstream agent-user {
	server 172.16.1.7:8080;
	server 172.16.1.7:8081;
}
upstream agent-pass {
	server 172.16.1.8:8080;
	server 172.16.1.8:8081;
}
server {
	listen 80;
	server_name agent.oldxu.com;
	location /user {
		proxy_pass http://agent-user/;
		include proxy_params;
	}
	location /pass {
		proxy_pass http://agent-pass/;
		include proxy_params;
	}
}

Windows的c:\windows\system32\drives\etc\hosts中添加:

10.0.0.7 agent.oldxu.com

在windows浏览器中输入域名agnet.oldxu.com即可访问。

2.基于终端设备调度
  1. web01作为手机端
vim /etc/nginx/conf.d/useragnet.oldxu.conf
------------/etc/nginx/conf.d/useragnet.oldxu.conf--------------
server {
	listen 80;
	server_name useragent.oldxu.com;
	root /useragnet;
	location / {
		index index.html;
	}
}
-----------/etc/nginx/conf.d/useragnet.oldxu.conf结束------------
mkdir /useragent
echo "phone..." > /useragent/index.html
nginx -t
systemctl reload nginx
  1. web02作为电脑端
vim /etc/nginx/conf.d/useragnet.oldxu.conf
------------/etc/nginx/conf.d/useragnet.oldxu.conf--------------
server {
	listen 80;
	server_name useragent.oldxu.com;
	root /useragnet;
	location / {
		index index.html;
	}
}
-----------/etc/nginx/conf.d/useragnet.oldxu.conf结束------------
mkdir /useragent
echo "pc..." > /useragent/index.html
nginx -t
systemctl reload nginx
  1. 负载均衡判断设备,调度到不同的集群
vim /etc/nginx/conf.d/proxy_useragnet.oldxu.conf
----------/etc/nginx/conf.d/proxy_useragnet.oldxu.conf----------
upstream pc {

}
upstream phone {

}
server {
	listen 80;
	server_name useragent.oldxu.com;
	charset utf-8;
	location / {
		default_type text/html;	#默认不支持中文,打印到浏览器,需要调整
		proxy_pass http://pc;
		include proxy_params;
		#判断来源设备
		if ( $http_user_agent ~* "android|iphone|ipad" ) {
			proxy_pass http://phone;
		}
		#如果不支持某些浏览器,在nginx拒接连接
		if ( $http_user_agnet ~* "Firefox|MSIE" ) {
			return 200 "你的浏览器不支持!!!"
		}
	}
}
---------/etc/nginx/conf.d/proxy_useragnet.oldxu.conf结束--------
3.proxy_pass后面带 / 的区别
  1. 带 / 意味着Nginx会修改用户请求的URL,将location匹配的URL进行删除。
location /user {
	proxy_pass http://172.16.1.7:80/;
}

用户请求URL: /user/test/index.html
请求到达Nginx负载均衡: /user/test/index.html
Nginx负载均衡到后端节点: /test/index.html
2. 不带 / 意味着Nginx不会修改用户请求的URL,而是直接代理到后端应用服务器。

location /user {
	proxy_pass http://172.16.1.7:80;
}

用户请求URL: /user/test/index.html
请求到达Nginx负载均衡: /user/test/index.html
Nginx负载均衡到后端节点: /user/test/index.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值