利用openresty动态设置host

利用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;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章