原文地址:https://blog.51cto.com/10978134/2163757

检测客户端是通过电脑还是移动设备访问的,将请求重定向到适配的WEB站点

此配置方法由Detect Mobile Browsers网站提供,是美国的一个免费开源的针对开发者检测移动端的解决方案,支持各种环境下的规则适配。访问地址:http://detectmobilebrowsers.com/

1、检测客户端若是通过移动设备访问的,则将请求重定向到移动端适配的WEB站点

在PC端的配置中做判断,若客户端是通过一个移动设备上的浏览器访问的,则将其重定向到移动端适配的WEB站点中。

[root@localhost ~]# vim /etc/nginx/conf.d/chengfeng.conf
server {
server_name www.chengfeng.com;
listen default_server;
include /etc/nginx/conf.d/chengfeng_redirect/chengfeng_mobile_redirect.conf;
...
}
[root@localhost ~]# vim /etc/nginx/conf.d/chengfeng_redirect/chengfeng_mobile_redirect.conf
set $mobile_rewrite do_not_perform; if ($http_user_agent ~* "(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino") {
set $mobile_rewrite perform;
} if ($http_user_agent ~* "^(1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-)") {
set $mobile_rewrite perform;
} if ($mobile_rewrite = perform) {
rewrite ^ http://mobile.chengfeng.com redirect;
break;
}

2、检测客户端若是通过电脑访问的,则将请求重定向到电脑端适配的WEB站点

在移动端的配置中做判断,若客户端是通过PC浏览器访问的,则将其重定向到电脑端适配的WEB站点中。

[root@localhost ~]# vim /etc/nginx/conf.d/chengfeng.conf
server {
server_name mobile.chengfeng.com
listen default_server;
include /etc/nginx/conf.d/chengfeng_redirect/chengfeng_www_redirect.conf;
...
}
[root@localhost ~]# vim /etc/nginx/conf.d/chengfeng_redirect/chengfeng_www_redirect.conf
set $www_rewrite do_not_perform; if ($http_user_agent ~* "(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino") {
set $www_rewrite perform;
} if ($http_user_agent ~* "^(1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-)") {
set $www_rewrite perform;
} if ($www_rewrite != perform) {
rewrite ^ http://www.chengfeng.com redirect;
break;
}

[转]NGINX-检测客户端是通过电脑还是移动设备访问的,将请求重定向到适配的WEB站点的更多相关文章

  1. Keepalived+Nginx搭建主从高可用并带nginx检测

    应用环境:部分时候,WEB访问量一般,或者测试使用,利用Keepalived给Nginx做高可用即可满足要求. 测试环境:   搭建步骤: 1. 安装软件 在Nginx-A和Nginx-B上: ~]# ...

  2. Apache与Nginx对客户端请求的处理机制对比

    Apache与Nginx对客户端请求的处理机制对比 模块 大致为四个模块,核心模块.HTTP模块.邮件模块,以及第三方模块 核心模块主要包含两类功能的支持,一类是主体功能,包括进程管理,权限管理,错误 ...

  3. 检测客户端显示器分辨率、浏览器类型和客户端IP

    原文:检测客户端显示器分辨率.浏览器类型和客户端IP 下面的代码实现了检测客户端显示器分辨率.浏览器类型和客户端IP的功能.你可以把客户端信息保存到Session,ViewState等中以便在其它的页 ...

  4. 【转载】C#检测客户端输入的内容是否含有危险字符串

    用户在客户端提交的内容有时候并不可信,如果客户端提交的内容中含有危险字符串信息,则很有可能造成应用程序安全性问题,如SQL注入风险等.因此在接收客户端提交过来的数据后,我们首先需要判断数据中是否含有危 ...

  5. javascript检测客户端环境是否是pc端

    //isPC(): 检测客户端环境是否是pc端 function isPC(){ let userAgent = navigator.userAgent; let agents = ["An ...

  6. nginx 限制及指定IP或IP段访问

    nginx 限制及指定IP或IP段访问. location / { deny 192.168.1.1; allow ; allow ; deny all; } 企业问题案例:Nginx做反向代理的时候 ...

  7. Nginx 核心配置-新建一个web站点

    Nginx 核心配置-新建一个web站点 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Nginx基础配置常用参数说明 [root@node101.yinzhengjie.or ...

  8. Nginx实战之让用户通过用户名密码认证访问web站点

    1.Nginx实战之让用户通过用户名密码认证访问web站点 [root@master ~]# vim /usr/local/nginx/conf/extra/www.conf server { lis ...

  9. python服务器端、客户端的模型,客服端发送请求,服务端进行响应(web.py)

    服务器端.客户端的模型,客服端发送的请求,服务端的响应 相当于启动了一个web server install web.py 接口框架用到的包 http://webpy.org/tutorial3.zh ...

随机推荐

  1. (转)接口测试工具Postman使用实践

    一.接口定义 软件不同部分之间的交互接口.通常就是所谓的API――应用程序编程接口,其表现的形式是源代码. —— [ 百度百科 ]我们常说的接口一般指两种:(1)API:应用程序编程接口.程序间的接口 ...

  2. 五大开源 Web 代理服务器横评:Squid、Privoxy、Varnish、Polipo、Tinyproxy

    https://linux.cn/article-7119-1.html Web 代理软件转发 HTTP 请求时并不会改变数据流量.它们可以配置成透明代理,而无需客户端配置.它们还可以作为反向代理放在 ...

  3. tensorflow2.0 学习(三)

    用tensorflow2.0 版回顾了一下mnist的学习 代码如下,感觉这个版本下的mnist学习更简洁,更方便 关于tensorflow的基础知识,这里就不更新了,用到什么就到网上取搜索相关的知识 ...

  4. am335x system upgrade kernel tf(五)

    1      Scope of Document This document describes TF hardware design 2      Requiremen 2.1     Functi ...

  5. circus security 来自官方的安全建议

    转自:https://circus.readthedocs.io/en/latest/design/security/ Circus is built on the top of the ZeroMQ ...

  6. linux命令之------Less命令

    Less命令 1)作用:less与more类似,但使用less可以随意浏览文件,而more仅能向前移动,却不能向后移动,而且less在查看之前不会加载整个文件. 2)ctrl+F 向前移动一屏: 3) ...

  7. 洛谷P2432 zxbsmk爱查错

    题目 DP,需要注意边界上的问题. 状态定义\(dp[i]\)为句子第i位去除字母的最小值,答案就是\(dp[len]\). 易得状态转移方程为: \[dp[i]=min(dp[i-1]+1,dp[l ...

  8. ASP复古随机验证码

    <% Call Com_CreatValidCode("ValidCode") Sub Com_CreatValidCode(pSN) Response.Expires = ...

  9. Ubuntu 安装MySQL报共享库找不到

    错误信息1: ./mysqld: error : cannot open shared object file: No such file or directory 解决办法:安装改库 # apt-g ...

  10. mybatis or的用法

    @Test public void test3(){ CaseSmallListExample caseSmallListExample = new CaseSmallListExample(); c ...