date: 2020-03-03title: nginx根据http_useragent判断是手机端还是PC端 #标题
tags: nginx判断PC或手机端 #标签
categories: nginx # 分类
nginx的location判断用户端是手机还是PC端配置。
配置文件如下:
# 判断 pc 和 mobile 的 H5
location / {
set $is_mobile false; #设置一个初始值
if ( $http_cookie ~* "ACCESS_TERMINAL=mobile" ) { #判断匹配手机端
set $is_mobile true;
}
if ($http_user_agent ~* (android|ip(ad|hone|od)|kindle|blackberry|windows\s(ce|phone))) { #匹配手机端类型
set $is_mobile true;
}
if ($is_mobile = true) {
root /usr/local/openresty/nginx/html/mobile/;
break;
}
root /usr/local/openresty/nginx/html/pc/;
}