tyk
tyk copied to clipboard
[TT-10516] Tyk ignores the hostname rewrite when an API calls an internal proxy
Branch/Environment/Version
- Branch/Version: Master
- Environment: On-prem
Describe the bug When an API is configured to proxy requests to an internal Tyk API, the proxy ignores host rewrites and doesn't forward them to the configured server.
Reproduction steps Steps to reproduce the behavior:
- Create two APIs with the following configurations:
{
"name": "primary-api",
"api_id": "primary-api",
"org_id": "primary-org",
"use_keyless": true,
"version_data": {
"not_versioned": true,
"versions": {
"Default": {
"name": "Default",
"use_extended_paths": true,
"extended_paths": {
"url_rewrites": [
{
"path": "^/headers/(.*)$",
"method": "GET",
"match_pattern": "^/headers/(.*)$",
"rewrite_to": "https://httpbin.org/headers",
"triggers": []
}
]
}
}
}
},
"proxy": {
"listen_path": "/",
"target_url": "tyk://fallback-api/_fallback",
"preserve_host_header": false
}
}
{
"name": "fallback-api",
"api_id": "fallback-api",
"org_id": "fallback-org",
"internal": true,
"use_keyless": true,
"version_data": {
"not_versioned": true,
"versions": {
"Default": {
"name": "Default",
"use_extended_paths": true,
"extended_paths": {
"mock_response": [
{
"method": "GET",
"ignore_case": true,
"code": 404,
"body": "{\"error\":\"Not found\"}",
"headers": {
"Content-Type": "application/json"
}
}
]
}
}
}
},
"proxy": {
"listen_path": "/_fallback"
}
}
- Spin up an Tyk instance
- Call the
GET /headers/
endpoint
Actual behavior Server returns the mock response.
Expected behavior
Server proxies the request to httpbin.org
.
Logs (debug mode or log file):
time="Jun 15 13:16:52" level=debug msg=Started api_id=primary-api api_name=primary-api mw=VersionCheck org_id=primary-org origin=127.0.0.1 path=/headers/123 ts=1686827812658815000
time="Jun 15 13:16:52" level=debug msg=Finished api_id=primary-api api_name=primary-api code=200 mw=VersionCheck ns=53529 org_id=primary-org origin=127.0.0.1 path=/headers/123
time="Jun 15 13:16:52" level=debug msg=Started api_id=primary-api api_name=primary-api mw=RateCheckMW org_id=primary-org origin=127.0.0.1 path=/headers/123 ts=1686827812658888000
time="Jun 15 13:16:52" level=debug msg=Finished api_id=primary-api api_name=primary-api code=200 mw=RateCheckMW ns=22854 org_id=primary-org origin=127.0.0.1 path=/headers/123
time="Jun 15 13:16:52" level=debug msg=Started api_id=primary-api api_name=primary-api mw=URLRewriteMiddleware org_id=primary-org origin=127.0.0.1 path=/headers/123 ts=1686827812658946000
time="Jun 15 13:16:52" level=debug msg="Rewriter active"
time="Jun 15 13:16:52" level=debug msg=/headers/123
time="Jun 15 13:16:52" level=debug msg="Inbound path: /headers/123"
time="Jun 15 13:16:52" level=debug msg="Rewriter checking matches, len is: 1"
time="Jun 15 13:16:52" level=debug msg="[[/headers/123 123]]"
time="Jun 15 13:16:52" level=debug msg="[]"
time="Jun 15 13:16:52" level=debug msg="URL Re-written from: /headers/123"
time="Jun 15 13:16:52" level=debug msg="URL Re-written to: https://httpbin.org/headers"
time="Jun 15 13:16:52" level=debug msg="Detected a host rewrite in pattern!"
time="Jun 15 13:16:52" level=debug msg=Finished api_id=primary-api api_name=primary-api code=200 mw=URLRewriteMiddleware ns=112242 org_id=primary-org origin=127.0.0.1 path=/headers/123
time="Jun 15 13:16:52" level=debug msg=Started api_id=fallback-api api_name=fallback-api mw=VersionCheck org_id=fallback-org origin=127.0.0.1 path=/headers ts=1686827812659082000
time="Jun 15 13:16:52" level=debug msg=Finished api_id=fallback-api api_name=fallback-api code=666 mw=VersionCheck ns=18956 org_id=fallback-org origin=127.0.0.1 path=/headers
Configuration (tyk config file): Use default configuration.
@LowCostCustoms thanks for raising this up. The url rewrite middleware matcher "match_pattern": "^/headers/(.*)$",
will attempt to match the entire path, and I think the reason this doesn't work is the presence of ^
. Can you please try it out with "match_pattern": "/headers/(.*)$"
.