15@nginx资源分离
Nginx资源分离
一、资源分离
Nginx通过负载均衡实现手机与PC调度至不通的后端节点应用案例 使用pc访问时跳转到pc配置的页面,使用手机访问时可以跳转不同的页面
二、资源分离环境配置
1、环境准备
主机 | 主机作用 | 外网ip | 内网ip | 端口 | 条件 |
---|---|---|---|---|---|
Lb01 | 负载均衡 | 10.0.0.5 | 172.16.1.5 | 80 | 关闭防火墙和selinux |
web01 | 提供Android手机页面 | 10.0.0.7 | 172.16.1.7 | 80 | 关闭防火墙和selinux |
web02 | 提供Iphone手机页面 | 10.0.0.8 | 172.16.1.8 | 80 | 关闭防火墙和selinux |
web03 | 提供电脑访问 | 10.0.0.9 | 172.16.1.9 | 80 | 关闭防火墙和selinux |
2、服务器配置
[root@web01 conf.d]# vim sj.linux.com.conf
server {
listen 8081;
server_name sj.linux.com;
location / {
root /code/android;
index index.html;
}
}
server {
listen 8082;
server_name sj.linux.com;
location / {
root /code/iphone;
index index.html;
}
}
server {
listen 8083;
server_name sj.linux.com;
location / {
root /code/pc;
index index.html;
}
}
#重启nginx
3、配置站点
[root@web01 conf.d]# mkdir /code/{android,pc,iphone}
[root@web01 conf.d]# echo "我是Android" > /code/android/index.html
[root@web01 conf.d]# echo "我是Iphone" > /code/iphone/index.html
[root@web01 conf.d]# echo "我是computer" > /code/pc/index.html
4、负载均衡配置
[root@lb01 conf.d]# vim sj.linux.com.conf
upstream anzhuo {
server 172.16.1.7:8081;
}
upstream iphone {
server 172.16.1.8:8082;
}
upstream pc {
server 172.16.1.9:8083;
}
server {
listen 80;
server_name sj.linux.com;
location / {
if ($http_user_agent ~* "Android") { #判断
proxy_pass http://anzhuo; #虚拟主机池名称
}
if ($http_user_agent ~* "iPhone") {
proxy_pass http://iphone;
}
if ($http_user_agent ~* "Chrome") { #判断是否为谷歌浏览器
return 403; #判断为谷歌,直接返回403
}
proxy_pass http://pc; #未匹配到以上内容,全部代理为pc虚拟池
}
}
#检查配置文件
[root@lb01 conf.d]#nginx -t
#重启nginx
[root@lb01 conf.d]# systemctl restart nginx
5、配置hosts访问页面测试
# 1.配置hosts
192.168.15.5 sj.linux.com
本文来自博客园,作者:ଲ一笑奈&何,转载请注明原文链接:https://www.cnblogs.com/zeny/p/15121565.html