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
| local cjson = require "cjson"
local comm_params = ngx.req.get_headers()["comm_params"]
local data = string.gsub(comm_params, '%%(%x%x)', function(h) return string.char(tonumber(h, 16)) end)
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 ngx.exec("@review"); elseif app_version == "1.50.2" and platform == "3" then 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; }
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")'; }
location @prod { default_type 'text/plain'; content_by_lua 'ngx.say("prod")'; }
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; } }
|