apisix
apisix copied to clipboard
help request: proxy-rewrite plugin didn't work as expected
Description
I used proxy-rewrite plugin want to forward request, like a.com/xx to b.com/yy,this is my configration:
{
"uri": "/sq-baidu",
"name": "proxy-rewrite",
"methods": [
"GET"
],
"host": "sqtest.matrix.dev.xxxxx.com.cn",
"plugins": {
"proxy-rewrite": {
"host": "10.200.106.112:30369",
"uri": "/project/getProjectDomainName"
}
},
"upstream": {
"nodes": [
{
"host": "34535.top", #is httpbin server
"port": 8530,
"weight": 1
}
],
"type": "roundrobin"
},
"status": 1
}
this is curl result from 10.200.106.112:30369
$ curl http://10.200.106.112:30369/project/getProjectDomainName
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 82 0 82 0 0 1555 0 --:--:-- --:--:-- --:--:--
{"code":0,"data":"http://matrix.dev.xxxxxxx.com.cn","success":true,"message":""}
so when I curl sqtest.matrix.dev.xxxxx.com.cn/sq-baidu, I expect the same result as above, but actually requested 34535.top. Why was it not forwarded to 10.200.106.112:30369?
$ curl --location --request GET 'sqtest.matrix.dev.xxxxxxx.com.cn/sq-baidu'
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 475 100 475 0 0 1357 0 --:--:-- --:--:-- --:--:-- 1361
{"args":{},"header":{"x-real-ip":"172.18.5.244","x-request-id":"d46faa7c-3ed1-4e39-b850-71a6f01368b7","x-forwarded-proto":"http","x-forwarded-host":"sqtest.matrix.dev.xxxxxxxx.com.cn","host":"10.200.106.162:30469","x-forwarded-port":"9080","x-forwarded-user-agent":"curl/7.80.0","user-agent":"curl/7.80.0","accept":"*/*"},"host":"58.218.131.211","uri":"/project/getProjectDomainName"}
This is access log, it looks normal. I wonder why the plug-in doesn't work properly. Is it my wrong way of using it?
172.11.52.44 - - [10/Aug/2022:21:10:33 +0800] sqtest.matrix.dev.xxxxxxx.com.cn "GET /sq-baidu HTTP/1.0" 200 475 0.015 "-" "curl/7.80.0" 111.91.156.229:8530 200 0.013 "http://10.200.106.112:30369/project/getProjectDomainName"
Environment
- APISIX version (run
apisix version): 2.13.2 - Operating system (run
uname -a): Linux localhost.localdomain 3.10.0-862.14.4.el7.x86_64 - OpenResty / Nginx version (run
openresty -Vornginx -V): openresty/1.21.4.1 - etcd version, if relevant (run
curl http://127.0.0.1:9090/v1/server_info): "etcd_version":"3.4.0" - APISIX Dashboard version, if relevant: 2.10.1
proxy-rewrite doesn't change the destination, but modifies the request.
You can use traffic split plugin to proxy to another upstream, or using redirect plugin and let the client redirects to the new location.
proxy-rewrite doesn't change the destination, but modifies the request.
You can use traffic split plugin to proxy to another upstream, or using redirect plugin and let the client redirects to the new location. @tokers Thanks, I got it. I misunderstood the usagfe of proxy-rewrite plugin. Redirect plugin relying on the 302 jump is not considered. And traffic split plugin can not modify uri ? I don't see the URI configuration in the documentation.
proxy-rewrite doesn't change the destination, but modifies the request. You can use traffic split plugin to proxy to another upstream, or using redirect plugin and let the client redirects to the new location. @tokers Thanks, I got it. I misunderstood the usagfe of proxy-rewrite plugin. Redirect plugin relying on the 302 jump is not considered. And traffic split plugin can not modify uri ? I don't see the URI configuration in the documentation.
Try to combine the proxy-rewrite and traffic-split plugin?
Thanks for your suggestion, it's feasible.