Requirement details:
A web service is divided into a formal server and a test server. Through the nginx proxy, the user will submit a login request (post method) to nginx when first accessing. Nginx determines whether the logged-in user is a test user. If it is a test user, it will be forwarded to the test server. .
Problems encountered:
1. After determining that the user has implemented it, how can subsequent requests be sent to the same server? I want to achieve this by saving a variable, but nginx variables cannot exist across requests. Can I save the variable by reading and writing files? .
2. How to implement the jump method?
Below is part of my code
First visit 127.0.0.1/smdb
location /smdb {
default_type 'text/plain';
set $jump 0;
access_log /var/log/nginx/smdb_access.log smdb;
error_log /var/log/nginx/error.log;
log_subrequest on;
lua_need_request_body on;
client_max_body_size 50k;
client_body_buffer_size 50k;
content_by_lua '
ngx.req.read_body()
local args, err = ngx.req.get_post_args()
if not args then
ngx.say("failed to get post args: ", err)
return
end
for key, val in pairs(args) do
if val == "test" then
ngx.var.jump = "1"
end
end
ngx.exec("/tosmdb")
';
}
location /tosmdb {
default_type 'text/plain';
echo $jump;#注释掉下面,这里显示为1即判断出了用户
if ($jump = "1"){
proxy_pass http://smdbtest;
}
if ($jump = "0"){
proxy_pass http://smdb;
}
}
迷茫2017-05-16 17:28:13
You need to put all the credentials of the user after logging in into the cookie, and then it will be convenient for verification.
See the examples in https://github.com/cloudflare/lua-resty-cookie for details.
Using proxy_pass
to jump should be no problem, it’s pretty good.