• 欢迎访问显哥博客,本网站纯属学习技术,绝无商业用途,欢迎小伙伴们共同学习!研究技术!QQ:52249909 加我QQ
  • 世界75亿人,这么小的概率,能认识你,是我一生的幸运,不妨加个QQ接触一下:52249909 加我QQ

【Linux运维架构】LNMP企业架构之nginx资源分离实战

Linux架构 lixian 4年前 (2020-03-08) 922次浏览 0个评论 扫描二维码
文章目录[隐藏]

一、Nginx资源分离概述

Nginx通过负载均衡实现手机与PC调度至不通的后端节点应用案例
使用pc访问时跳转到pc配置的页面,使用手机访问时可以跳转不同的页面
【Linux运维架构】LNMP企业架构之nginx资源分离实战

二、资源分离环境准备

主机 主机作用 外网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;
    }
}

六、配置hosts浏览器访问页面测试

【Linux运维架构】LNMP企业架构之nginx资源分离实战

【Linux运维架构】LNMP企业架构之nginx资源分离实战

【Linux运维架构】LNMP企业架构之nginx资源分离实战


本站博主 , 版权所有丨如未注明 , 均为原创
转载请注明原文链接:【Linux运维架构】LNMP企业架构之nginx资源分离实战
喜欢 (0)

您必须 登录 才能发表评论!