1.基于uri请求调度到不同集群
- 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
- 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
- 负载均衡根据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.基于终端设备调度
- 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
- 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
- 负载均衡判断设备,调度到不同的集群
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后面带 / 的区别
- 带 / 意味着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