一、Nginx资源分离概述
Nginx通过负载均衡实现手机与PC调度至不通的后端节点应用案例
使用pc访问时跳转到pc配置的页面,使用手机访问时可以跳转不同的页面

二、资源分离环境准备
| 主机 | 主机作用 | 外网ip | 内网ip | 端口 |
| Lb01 | 负载均衡 | 10.0.0.4 | 172.16.1.4 | 80 |
| web01 | 基于多端口多虚拟主机 | 10.0.0.7 | 172.16.1.7 | 8081-8083 |
三、编写nginx的conf文件
[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服务
[root@lb01 conf.d]# systemctl restart nginx
四、配置站点
[root@web01 conf.d]# mkdir /code/{android,pc,iphone}
[root@web01 conf.d]# echo "我是安卓" > /code/android/index.html
[root@web01 conf.d]# echo "我是iphone" > /code/iphone/index.html
[root@web01 conf.d]# echo "我是computer" > /code/pc/index.html
五、配置负载均衡
[root@lb01 conf.d]# vim sj.linux.com.conf
upstream anzhuo {
server 172.16.1.7:8081;
}
upstream iphone {
server 172.16.1.7:8082;
}
upstream pc {
server 172.16.1.7:8083;
}
server {
listen 80;
server_name sj.linux.com;
charset 'utf-8';
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;
}
proxy_pass http://pc;
}
}




