利用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;