不知道你的意思是不是想通过header的Host字段来做不同的后端请求分发:
你可以在access_by_lua阶段把Host字段取出(比如用 ngx.req.get_headers)
然后设置变量$backend_server(根据你自己的映射规则)
最后在proxy_pass http://$backend_server 当作upstream转发出去。
location / {
set $backend_server '';
access_by_lua_block {
local Host = ngx.req.get_headers()["Host"]
ngx.var.backend_server = findyourupstreamname(Host) //可以从shdict中甚至通过cosocket从外部servere中获取。
}
proxy_pass http://$backend_server;
}
其实玩法太多了。
在 2017年7月20日星期四 UTC+8上午9:46:43,周宏成写道:
//在nginx里的server中
server {
location / {
lua_code_cache off;
access_by_lua_file /path/to/filter.lua;
proxy_pass other_server;
}
}
在filter.lua中,代码很简单
local sock, err = ngx.req.socket(true);
if not sock then
ngx.eof();
return;
end
sock:settimeout(1000)
local data, err, partial = sock:receive(4029)
local str = data or err
如果是GET请求,sock:
receive肯定是获取不到任何信息的,因为HTTP头已经读完了
在lua中,我想区分网址,并修改一些参数之后,分发到proxy_pass other_server;上,
请问proxy_pass 的后端服务器还能能获取到http头这些部分的内容吗?