基于nginx负载均衡的调度
1.基于 uri 请求调度至不同集群
- 提供 /user /pass ,准备两套集群
1 ) web01 提供/user,使用多端口方式来模拟多个web节点
[root@web01 ~]# cat /etc/nginx/conf.d/agent.wyk.com.conf
server {
listen 8080;
server_name agent.wyk.com;
root /agent/8080;
location / {
index wyk.html;
}
}
server {
listen 8081;
server_name agent.wyk.com;
root /agent/8081;
location / {
index index.html;
}
}
mkdir /agent/{8080,8081} -p
echo "user-8080" > /agent/8080/index.html
echo "user-8081" > /agent/8081/index.html
systemctl reload nginx
2 ) web02 提供/pass,使用多端口方式来模拟多个web节点
[root@web02 ~]# cat /etc/nginx/conf.d/agent.wyk.com.conf
server {
listen 8082;
server_name agent.wyk.com;
root /agent/8082;
location / {
index index.html;
}
}
server {
listen 8083;
server_name agent.wyk.com;
root /agent/8083;
location / {
index index.html;
}
}
mkdir /agent/{8082,8083} -p
echo "pass-80802" > /agent/8082/index.html
echo "pass-80803" > /agent/8083/index.html
systemctl reload nginx
3)使用负载均衡作为统一入口,根据用户请求的uri进行调度
[root@lb01 ~]# cat /etc/nginx/conf.d/proxy_agent.wyk.com.conf
upstream agent-user {
server 172.16.1.7:8080;
server 172.16.1.7:8081;
}
upstream agent-pass {
server 172.16.1.8:8082;
server 172.16.1.8:8083;
}
server {
listen 80;
server_name agent.wyk.com;
location /user {
proxy_pass http://agent-user/; #加跟和不加跟的区别
include proxy_params;
}
location /pass {
proxy_pass http://agent-pass/;
include proxy_params;
}
}
- proxy_pass 添加 / 和不添加 / 有什么区别?
proxy_pass有两种常用的写法:
proxy_pass http://localhost:8080;
proxy_pass http://localhost:8080/;
带 / 和 不带 / 有什么区别呢?
1、不带 / 示例:
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
2、带 / 示例
location /user {
proxy_pass http://172.16.1.7:80/;
}
用户请求URL: /user/test/index.html
请求到达Nginx负载均衡: /user/test/index.html
Nginx负载均衡到后端节点: /test/index.html
3.总结:
1.带 / 意味着Nginx会修改用户请求的URL,将location匹配的URL进行删除。
2.不带 / 意味着Nginx不会修改用户请求的URL,而是直接代理到后端应用服务器。
2.基于来源的终端设备调度不同的页面
- 网站:
pc: pc端百度
手机:显示是手机端的百度
1.web01作为手机端
[root@web01 ~]# cat /etc/nginx/conf.d/useragent.wyk.com.conf
server {
listen 80;
server_name useragent.wyk.com;
root /useragent;
location / {
index index.html;
}
}
[root@web01 ~]# mkdir /useragent
[root@web01 ~]# echo "Phone..." > /useragent/index.html
[root@web01 ~]# systemctl reload nginx
2.web02作为pc端
[root@web02 ~]# cat /etc/nginx/conf.d/useragent.wyk.com.conf
server {
listen 80;
server_name useragent.wyk.com;
root /useragent;
location / {
index index.html;
}
}
[root@web02 ~]# mkdir /useragent
[root@web02 ~]# echo "PC..." > /useragent/index.html
[root@web02 ~]# systemctl reload nginx
3.负载均衡判断设备,然后调度到不同的集群。
[root@lb01 ~]# cat /etc/nginx/conf.d/proxy_useragent.wyk.com.conf
upstream pc {
server 172.16.1.8:80;
}
upstream sj {
server 172.16.1.7:80;
}
server {
listen 80;
server_name useragent.wyk.com;
charset utf-8;
location / {
default_type text/html; #默认不支持将文字 打印到浏览器,所以需要调整默认的类型
proxy_pass http://pc; #默认走PC
include proxy_params;
#判断
if ( $http_user_agent ~* "android|iphone|ipad" ) {
proxy_pass http://sj;
}
#如果开发写的代码不支持某些浏览器,还可以直接在Nginx层面拒绝他比如:MSIE
if ( $http_user_agent ~* "Firefox|MSIE" ) {
return 200 "你当前使用的浏览器真棒!";
}
}
}