15@nginx资源分离

Nginx资源分离

一、资源分离

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

二、资源分离环境配置

1、环境准备

主机主机作用外网ip内网ip端口条件
Lb01负载均衡10.0.0.5172.16.1.580关闭防火墙和selinux
web01提供Android手机页面10.0.0.7172.16.1.780关闭防火墙和selinux
web02提供Iphone手机页面10.0.0.8172.16.1.880关闭防火墙和selinux
web03提供电脑访问10.0.0.9172.16.1.980关闭防火墙和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

posted @ 2021-05-07 00:16  ଲ一笑奈&何  阅读(73)  评论(0编辑  收藏  举报