nginx客户端请求分类

客户端请求分类

用户请求分类使用一个域名把不同环境的用户请求根据用户环境使用pc端或者不同型号的手机端进行分类跳转到相应的页面,提升用户体验

操作演示

通过不同端口跳转不同页面

环境准备

系统版本 主机角色 外网IP 内网IP 提供端口 服务器名称
CentOS7.6 负载均衡 10.0.0.5 172.16.1.5 80 lb01
CentOS7.6 提供Android页面 10.0.0.7 172.16.1.7 9090 web01
CentOS7.6 提供Iphone页面 10.0.0.7 172.16.1.7 9091 web01
CentOS7.6 提供pc页面 10.0.0.7 172.16.1.7 9092 web01

web01配置

# 安装nginx
[root@web01 ~]# yum install -y nginx
# 编辑nginx配置文件
[root@web01 ~]# cd /etc/nginx/conf.d/
[root@web01 conf.d]# vim pc_ios_android.conf

server{
    listen 9090; 
    root /code/android;
    index index.html;
}
        
server{
    listen 9091; 
    root /code/ios;
    index index.html;
}

server{
    listen 9092;                                                                                                       
    root /code/pc;
    index index.html;
}
# 检查语法
[root@web01 conf.d]# nginx -t
# 启动nginx并加入开机自启
[root@web01 conf.d]# systemctl start nginx && systemctl enable nginx

# 根据配置文件创建站点目录
[root@web01 conf.d]# mkdir /code/android
[root@web01 conf.d]# mkdir /code/ios
[root@web01 conf.d]# mkdir /code/pc
# 写默认页面
[root@web01 conf.d]# echo pc page >/code/pc/index.html
[root@web01 conf.d]# echo android page>/code/android/index.html
[root@web01 conf.d]# echo ios page>/code/ios/index.html

lb01代理配置

# 安装nginx
[root@lb01 ~]# yum install -y nginx
# 编辑nginx配置文件
[root@lb01 ~]# cd /etc/nginx/conf.d/
[root@lb01 conf.d]# cat www.hahaha.com.conf
upstream android {
        server 172.16.1.7:9090;
}

upstream ios {
        server 172.16.1.7:9091;
}

upstream pc {
        server 172.16.1.7:9092;
}


server {
	listen 80;
	server_name www.hahaha.com;

	location / {
	        #如果客户端来源是Android则跳转到Android的资源;
                if ($http_user_agent ~* "Android") {
                        proxy_pass http://android;
                }

                #如果客户端来源是Iphone则跳转到Iphone的资源;
                if ($http_user_agent ~* "Iphone") {
                        proxy_pass http://ios;
                }

                #如果客户端是IE浏览器则返回403错误;
                if ($http_user_agent ~* "MSIE") {
                        return 403;
                }

                #默认跳转pc资源;
                proxy_pass http://pc;
        }
}
# 检测配置文件
[root@lb01 conf.d]# nginx -t
# 启动并加入开机自启
[root@lb01 conf.d]# systemctl start nginx && systemctl enable nginx

# 域名解析
# 浏览器访问


使用rewrite跳转页面

# 修改web01 配置文件
[root@web01 conf.d]# !v
vim pc_ios_android.conf 

server{
    listen 80;
    server_name www.hahaha.com hahaha.com;
    root /code;
    index index.html;
    if ( $http_user_agent ~* "Android" ){
        rewrite ^/$ http://$host/android redirect;
    }
    if ( $http_user_agent ~* "iPhone|iPad" ){                                                                                          
        rewrite ^/$ http://$host/ios redirect;
    }
}
# 检查语法
[root@web01 code]# nginx -t
# 重新加载nginx
[root@web01 code]# systemctl reload nginx

# 编辑index.html文件
[root@web01 ~]# cd /code

[root@web01 code]# echo pc mo ren page >index.html

# 域名解析
# 浏览器访问


posted @ 2020-07-23 16:59  王顺子  阅读(454)  评论(0编辑  收藏  举报