nginx判断微信端跳转不同页面

nginx判断微信端跳转不同页面

一般视频分享链接,分享到微信或者QQ。如果不支持打开的话。就跳转到微信提示页面。通过其他浏览器打开。

配置文件
nginx主配置文件http模块需要添加

1
2
resolver                  1.1.1.1 1.0.0.1 8.8.8.8 8.8.4.4 208.67.222.222 208.67.220.220 valid=60s;
resolver_timeout 2s;
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
server {
listen 443 ssl http2;
server_name www.wxfx.com;
root /software/files/wxfx.com;
default_type text/html;

# SSL
ssl_certificate /software/域名证书/wxfx.com/Nginx/wxfx.com.nginx.crt;
ssl_certificate_key /software/域名证书/wxfx.com/Nginx/wxfx.com.key;

include /software/站点配置文件/ssl_general;

# index.html fallback
location / {
try_files $uri $uri/ /proc.html?u=$uri;
}

location = /wx.html {}
location = /index.html {}
location = /proc.html {}
location = /qq.html {}

location ~* \.html$ {
content_by_lua_block {
--local ua = ngx.var.http_user_agent;
--if string.find(string.lower(ua), "weixin") then
-- ngx.redirect("/wx.html");
--elseif string.find(string.lower(ua), "qqtheme") then
-- ngx.redirect("/qq.html");
--else
ngx.exit(404);
--end
}
}

location /test {
content_by_lua_block {
if not ngx.var.arg_u then
ngx.redirect("/");
end

local http = require "resty.http";
local httpc = http.new();
local res, err = httpc:request_uri("http://www.spgetaway.com/videosite/short-link/getShortLink2/"..ngx.var.arg_u, {});

if not res then
ngx.say(err);
end

if 200 ~= res.status then
ngx.exit(res.status);
end

local json = require("cjson.safe");
local t = json.decode(res.body);

if not t or t.data == json.null or not t.data.longs then
ngx.exit(404);
end
#分享跳转后的地址
ngx.redirect("https://www.www-wxsp.app/?"..t.data.longs);
}
}

# favicon.ico
location = /favicon.ico {
log_not_found off;
access_log off;
}

# robots.txt
location = /robots.txt {
log_not_found off;
access_log off;
}

# assets, media
location ~* \.(?:css(\.map)?|js(\.map)?|jpe?g|png|gif|ico|cur|heic|webp|tiff?|mp3|m4a|aac|ogg|midi?|wav|mp4|mov|webm|mpe?g|avi|ogv|flv|wmv)$ {
expires 7d;
access_log off;
}

}

# non-www, subdomains redirect
server {
listen 443 ssl http2;
server_name .wxfx.com;

# SSL
ssl_certificate /software/域名证书/wxfx.com/Nginx/wxfx.com.nginx.crt;
ssl_certificate_key /software/域名证书/wxfx.com/Nginx/wxfx.com.key;
return 301 https://www.wxfx.com$request_uri;
}

# HTTP redirect
server {
listen 80;
server_name .wxfx.com;
return 301 https://www.wxfx.com$request_uri;
}

家目录对应的文件

1
2
ls /software/files/wxfx.com/
index.html proc.html qq.html wx.html

index.html

1
2
3
4
5
6
7
8
9
10
11
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>Page Title</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
</head>
<body>
</body>
</html>

proc.html

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
31
<!DOCTYPE html>
<html>

<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>Page Title</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<script>
var ua = window.navigator.userAgent.toLowerCase();

let s = window.location.pathname.split("/");
if (s.length > 0) {
if (-1 != ua.indexOf("weixin") || -1 != ua.indexOf("micromessenger")) {
window.location.href = "/wx.html?u=" + s[1];
} else if (-1 != ua.indexOf("qqtheme")) {
window.location.href = "/qq.html?u=" + s[1];
} else {
window.location.href = "/test?u=" + s[1];
}
}
</script>
</head>

<body>
<center>
<h2>正在处理...</h2>
</center>
</body>

</html>

qq.html

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<!doctype html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=0">
<title></title>
<script>
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split("=");
if (pair[0] == variable) { return pair[1]; }
}
return (false);
}
</script>
<script>
var ua = window.navigator.userAgent.toLowerCase();
if (-1 == ua.indexOf("qqtheme")) {
let u = getQueryVariable('u');
if (u) {
window.location.href = u;
} else {
window.location.href = "/index.html";
}
}
</script>
<style>
.center {
padding: 0 20px
}
</style>
</head>

<body>
<h1 align="right">👆</h1>
<div class="center">
<h2>
<font color="red">提示</font><br>手机QQ中无法直接打开<br>请点击页面右上角,在<font color="red">浏览器</font>中访问
</h2>
</div>
</body>

</html>

wx.html

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<!doctype html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=0">
<title></title>
<script>
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split("=");
if (pair[0] == variable) { return pair[1]; }
}
return (false);
}
</script>
<script>
var ua = window.navigator.userAgent.toLowerCase();
if (-1 == ua.indexOf("micromessenger") || -1 == ua.indexOf("weixin")) {
let u = getQueryVariable('u');
if (u) {
window.location.href = u;
} else {
window.location.href = "/index.html";
}
}
</script>
<style>
.center {
padding: 0 20px
}
</style>
</head>

<body>
<h1 align="right">👆</h1>
<div class="center">
<h2>
<font color="red">提示</font><br>微信中无法直接打开<br>请点击页面右上角,在<font color="red">浏览器</font>中访问
</h2>
</div>
</body>

</html>

评论


:D 一言句子获取中...

加载中,最新评论有1分钟缓存...