首页 / NGINX / nginx客户端请求分类
nginx客户端请求分类
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了nginx客户端请求分类,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2467字,纯文字阅读大概需要4分钟。
内容图文
客户端请求分类
用户请求分类使用一个域名把不同环境的用户请求根据用户环境使用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
# 域名解析
# 浏览器访问
原文:https://www.cnblogs.com/zabcd/p/13367312.html
内容总结
以上是互联网集市为您收集整理的nginx客户端请求分类全部内容,希望文章能够帮你解决nginx客户端请求分类所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。