利用openresty动态设置host
因项目需要,nginx proxy需要动态设置http_host参数,原生nginx并不支持,经调研发现openresty可以实现:
1. 安装openresty
2. 启动nginx
3.修改nginx 配置
nginx.conf
set_by_lua $new_host '
local prefix = ngx.re.sub(ngx.var.http_host, "(xx-x-xxxxx-).*", "$1", "o")
if prefix == "xx-x-xxxxx-" then
local host = ngx.re.sub(ngx.var.http_host, "(.*):([0-9]{2,4})", "$1.xx.com:$2", "o")
return host;
else
return ngx.var.http_host;
end
'
proxy_set_header Host $new_host;
proxy_pass $scheme://$new_host$request_uri;