You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@apisix.apache.org by cyxinda <cy...@163.com> on 2020/04/10 10:48:06 UTC

the apisix function:[core.request.set_header("Host","xx_value") not work

hi
各位好:
因为怕英文叙述不好,用中文叙述下吧:
我在自定义的插件中,编写access函数,在request中设置请求头Host的值为“xxxxxxxxxxxx:29800”
调用的代码是
function _M.access(conf, ctx)
    core.request.set_header("Host", "xxxxxxxxxxxx:29800")
end
然后我在被代理的后端服务中,打印出请求头Host的值:
调用的代码是:
System.out.println("Host:::::::::::::::::::::"+request.getHeader("Host"));
我在浏览器上面发出请求:http://10.110.149.175:9080/xxl-job-admin/
后端服务打印日志如下:
Host:::::::::::::::::::::10.110.149.175
可以看到日志打印的不是我在自定义插件中设置的值,所以,我怀疑,通过core.request.set_header("Host","xxvalue")设置请求头Host的值,是不是apisix的一个bug,或者是apisix对具体的Header(如Host)的值设定有限制。


为了验证我的怀疑,我在原生的nginx.conf中做如下设置:
location /xxl-job-admin/ {
    proxy_pass http://xxlServer;
    proxy_set_header   Host             xxxxxxxxxx:$server_port;
    proxy_set_header   X-Real-IP        $remote_addr;
    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
}


通过浏览器访问:http://10.110.152.175:8000/xxl-job-admin/
后端服务打印日志如下:
Host:::::::::::::::::::::xxxxxxxxxx:8000
与我在nginx.conf中设置的Host是相同的


综上,可以看出我在自定义插件access方法中,通过函数 core.request.set_header("Host","xxvalue") 设置请求头Host的值,是有问题的,请各位帮忙看下问题所在,谢谢!













--

此致!

                                                                                                                             敬礼

----曹勇

TEL:15910514083

Re: the apisix function:[core.request.set_header("Host","xx_value") not work

Posted by Ming Wen <we...@apache.org>.
Hi, cyxinda,
Please use English only in the mailing list.

This is not a bug of Apache APISIX.
There should be two points here that need your attention:
1. you are setting up an illegal host, `ngx.req.set_header` will do a
judgment and filter.
2. you can't set the host directly from the header in Apache APISIX,
you need to modify the variable `upstream_host`, here is an example[1].

[1]
https://github.com/apache/incubator-apisix/blob/master/apisix/plugins/proxy-rewrite.lua#L114

Thanks,
Ming Wen, Apache APISIX & Apache SkyWalking
Twitter: _WenMing


cyxinda <cy...@163.com> 于2020年4月10日周五 下午6:48写道:

> hi
> 各位好:
> 因为怕英文叙述不好,用中文叙述下吧:
> 我在自定义的插件中,编写access函数,在request中设置请求头Host的值为“xxxxxxxxxxxx:29800”
> 调用的代码是
> function _M.access(conf, ctx)
>     core.request.set_header("Host", "xxxxxxxxxxxx:29800")
> end
> 然后我在被代理的后端服务中,打印出请求头Host的值:
> 调用的代码是:
>
> System.out.println("Host:::::::::::::::::::::"+request.getHeader("Host"));
>
> 我在浏览器上面发出请求:http://10.110.149.175:9080/xxl-job-admin/
> 后端服务打印日志如下:
> Host:::::::::::::::::::::10.110.149.175
>
> 可以看到日志打印的不是我在自定义插件中设置的值,所以,我怀疑,通过core.request.set_header("Host","xxvalue")设置请求头Host的值,是不是apisix的一个bug,或者是apisix对具体的Header(如Host)的值设定有限制。
>
> 为了验证我的怀疑,我在原生的nginx.conf中做如下设置:
> location /xxl-job-admin/ {
>     proxy_pass http://xxlServer;
>     *proxy_set_header   Host             xxxxxxxxxx:$server_port;*
>     proxy_set_header   X-Real-IP        $remote_addr;
>     proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
> }
>
> 通过浏览器访问:http://10.110.152.175:8000/xxl-job-admin/
> 后端服务打印日志如下:
> Host:::::::::::::::::::::xxxxxxxxxx:8000
> 与我在nginx.conf中设置的Host是相同的
>
> 综上,可以看出我在自定义插件access方法中,通过函数 core.request.set_header("Host","xxvalue")
> 设置请求头Host的值,是有问题的,请各位帮忙看下问题所在,谢谢!
>
>
>
>
>
> --
>
> 此致!
>
>
>                                                                                                                              敬礼
>
> ----曹勇
>
> TEL:15910514083
>
>
>
>