nginx 判断移动端跳转不同页面

配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
location /admin {
// 判断是不是移动端(是则定义参数mob为1)
if ($http_user_agent ~* (mobile|iphone|ipad|android)) {
set $mob 1;
}
// 此处是处理移动端坚持访问PC版的逻辑,即判断cookie中是否存在platform=pc(也可使用其他cookie判断)
if ($http_cookie ~* "platform=pc") {
set $mob 0;
}
if ($mob = 1) {
// 重置配置为/adminmob
rewrite ^/admin(.*) /adminmob$1 last;
}
// 重置配置为/adminpc
rewrite ^/admin(.*) /adminpc$1 last;
}

// pc版的实际配置
location /adminpc {
alias /home/work/v1zhushou/adminpc;
index index.html;
try_files $uri /adminpc/index.html;
}

// 移动版的实际配置
location /adminmob {
alias /home/work/v1zhushou/adminmob;
index index.html;
try_files $uri /adminmob/index.html;
}