nginx_lua 根据传参访问不同的系统1 nginx配置 server { listen 81; server_name _; charset utf-8
nginx_lua 根据传参访问不同的系统1
nginx配置
server { listen 81; server_name _; charset utf-8; access_log logs/tt.access.log; location /dubbo1 { proxy_pass http://192.168.2.41:6060; } location /lua { # content_by_lua_block { # local mydata = require "mydata" # ngx.say(mydata.get_age("dog")) # } default_type 'text/html'; content_by_lua 'ngx.say("hello world")'; } location /test { default_type 'text/plain'; set $foo hello; echo "foo: $foo"; echo "method: $echo_request_method."; } location /dubbo/ { set $service ''; rewrite_by_lua ' local request_method = ngx.var.request_method if request_method == "GET" then local arg = ngx.req.get_uri_args()["service"] or 0 ngx.var.service = arg elseif request_method == "POST" then ngx.req.read_body() local arg = ngx.req.get_post_args()["service"] or 0 ngx.var.service = arg end;'; #default_type 'text/plain'; #echo "service: $service"; if ( $service != 'register') { echo_before_body "echo_before_body"; echo_before_body "service: $service"; proxy_pass http://192.168.2.41:6060; echo_after_body "echo_after_body"; echo_after_body "service: $service"; #break; } #echo "aaaaa"; proxy_redirect off; 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 /gateway { access_by_lua_file sbin/route.lua; error_page 433 = @433; error_page 434 = @434; error_page 435 = @435; } location @433 { rewrite ^/gateway(.*)$ /dubbo$1 last; proxy_pass http://192.168.2.41:6060; } location @434 { rewrite ^/ http://www.baidu.com; } location @435 { rewrite ^/ http://www.163.com; } error_page 500 502 503 504 404 /50x.html; location = /50x.html { root html; }}
route.lua脚本
local method = ngx.var.request_methodlocal code = nil-- 判断请求方式,获取code值if method == "GET" then code = ngx.req.get_uri_args()["code"] or 0elseif method == "POST" then ngx.req.read_body() code = ngx.req.get_post_args()["code"] or 0end-- 通过code寻找route返回码local route = routes:get(code)if route == nil then ngx.exit(routes:get("default"))end-- 返回码ngx.exit(route)
===============================================
nginx_lua 根据传参访问不同的系统2
server { listen 82; server_name _; charset utf-8; access_log logs/ttt.access.log; location /demo { #proxy_pass http://192.168.2.41:8080/demo/demo/user/add/v1.0; echo ngx.req.get_post_args()["method"] ; } location /paymer { echo "paymer ---"; } location /api { echo "api ---"; } location = /gateway { set $method ''; rewrite_by_lua ' local request_method = ngx.var.request_method if request_method == "GET" then -- local Args, err = ngx.req.get_post_args() local arg = ngx.req.get_uri_args()["method"] or 0 ngx.var.method = arg -- ngx.var.args = Args elseif request_method == "POST" then ngx.req.read_body() local method = ngx.req.get_post_args()["method"] or 0 ngx.var.method = method end; if ngx.var.method == "api" then -- ngx.say("api"); local t = ngx.location.capture("/api"); ngx.say(t.body); elseif ngx.var.method == "demo" then -- ngx.say("paymer"); local t = ngx.location.capture("/demo",{method=ngx.HTTP_POST,body=""}) local tt = "appid=ngx.var.appid&method=ngx.var.method&format=ngx.var.format&charset=ngx.var.charset&sign_type=ngx.var.sign_type&sign=ngx.var.sign×tamp=ngx.var.timestamp&version=ngx.var.version&biz_content=ngx.var.biz_content" ngx.say(t.body); -- ngx.say(ngx.var.method .. ngx.var.appid); elseif ngx.var.method == "paymer" then -- ngx.say("paymer"); local t = ngx.location.capture("/paymer"); ngx.say(t.body); elseif ngx.var.method == "" then ngx.say(""); elseif ngx.var.method == nul then ngx.say("nul"); else ngx.say("other"); end '; #default_type 'text/plain'; #echo "service: $service"; }}