1. 创建Lua脚本

vi /home/services/openresty/conf.d/toggle.lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
-- 引入json解析
local cjson = require "cjson"
-- 获取请求头JSON
local comm_params = ngx.req.get_headers()["comm_params"]
-- URL解码(如果对象是UrlEncode编码)
local data = string.gsub(comm_params, '%%(%x%x)', function(h) return string.char(tonumber(h, 16)) end)
-- 解析JSON对象并获取属性
local app_version = cjson.decode(data)["version"]
local platform = cjson.decode(data)["platform"]

if app_version ~= nil and platform ~= nil then
if app_version == "1.50.3" and platform == "2" then
-- IOS APP
ngx.exec("@review");
elseif app_version == "1.50.2" and platform == "3" then
-- Android APP
ngx.exec("@review");
else
ngx.exec("@prod");
end
else
ngx.exec("@prod");
end

2. 修改nginx配置引用lua脚本

vi /home/services/openresty/conf.d/api-example.conf

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
server {
listen 80;
server_name example.com www.example.com;
return 301 https://example.com$request_uri;
}

# HTTPS server
server {
listen 443 ssl;
server_name www.example.com;

ssl_certificate /etc/nginx/ssl/8024687_example.com.pem;
ssl_certificate_key /etc/nginx/ssl/8024687_example.com.key;

ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;

underscores_in_headers on;

location / {
access_by_lua_file /etc/nginx/conf.d/toggle.lua;
}

location @review {
# 脚本测试
default_type 'text/plain';
content_by_lua 'ngx.say("review")';
# 服务转发
# proxy_pass http://192.168.1.11:8080;
# proxy_set_header Host $host;
# proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

location @prod {
# 脚本测试
default_type 'text/plain';
content_by_lua 'ngx.say("prod")';
# 服务转发
# proxy_pass http://192.168.1.12:8080;
# proxy_set_header Host $host;
# proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}


proxy_intercept_errors on;
error_page 404 /404.html;

location = /404.html {
root /usr/local/openresty/nginx/html;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/openresty/nginx/html;
}
}

最后更新: 2022年08月29日 11:37

原始链接: https://www.lmaye.com/2022/07/29/20220729141428/

× 多少都行~
打赏二维码