我目前正在使用 Krakend (https://krakend.io) API 网关将请求代理到我的后端服务。我的后端服务 API 响应之一是带有 http 303 的重定向响应。重定向响应如下所示:
HTTP/1.1 303 See Other
content-length: 48
content-type: text/plain; charset=utf-8
date: Thu, 16 Jul 2020 10:25:41 GMT
location: https://www.detik.com/
vary: Accept
x-powered-by: Express
x-envoy-upstream-service-time: 17
server: istio-envoy
问题是,Krakend 实际上是遵循 http 重定向并返回重定向 Url 的响应,而不是向客户端返回 http 303 响应(带有位置响应标头)as-is,这是 的 html 响应https://www.detik.com/.
我当前的 krakend 配置如下所示:
{
"version": 2,
"extra_config": {
"github_com/devopsfaith/krakend-cors": {
"allow_origins": [],
"expose_headers": [
"Content-Length",
"Content-Type",
"Location"
],
"allow_headers": [
"Content-Type",
"Origin",
"X-Requested-With",
"Accept",
"Authorization",
"secret",
"Host"
],
"max_age": "12h",
"allow_methods": [
"GET",
"POST",
"PUT"
]
},
"github_com/devopsfaith/krakend-gologging": {
"level": "ERROR",
"prefix": "[GATEWAY]",
"syslog": false,
"stdout": true,
"format": "default"
},
"github_com/devopsfaith/krakend-logstash": {
"enabled": false
}
},
"timeout": "10000ms",
"cache_ttl": "300s",
"output_encoding": "json",
"name": "api-gateway",
"port": 8080,
"endpoints": [
{
"endpoint": "/ramatestredirect",
"method": "GET",
"extra_config": {},
"output_encoding": "no-op",
"concurrent_calls": 1,
"backend": [
{
"url_pattern": "/",
"encoding": "no-op",
"sd": "static",
"extra_config": {},
"method": "GET",
"host": [
"http://ramatestredirect.default.svc.cluster.local"
],
"disable_host_sanitize": false
}
]
}
]
}
那么我怎样才能让 krakend 将后端服务未更改的原始 http 303 响应返回给客户端呢?
谢谢你
如果您使用 Lura Framework(以前称为 Kraken 框架),那么您可能必须禁用 http 客户端的重定向。
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
我假设您正在调用此端点/ramatestredirect
要获取后端http状态代码(如您所说,它返回303 http状态代码),您可以使用以下方式:
{
"endpoint": "/ramatestredirect",
"method": "GET",
"extra_config": {},
"output_encoding": "no-op",
"concurrent_calls": 1,
"backend": [
{
"url_pattern": "/",
"encoding": "no-op",
"sd": "static",
"extra_config": {
"github.com/devopsfaith/krakend/http": {
"return_error_details": "authentication"
}
},
"method": "GET",
"host": [
"http://ramatestredirect.default.svc.cluster.local"
],
"disable_host_sanitize": false
}
]
}
所以,基本上通过这个插件你可以获得原始的后端http状态码
"github.com/devopsfaith/krakend/http": {
"return_error_details": "authentication"
}
我在处理 google 身份验证时遇到了同样的问题,api 网关没有将客户端重定向到 google 登录网站,而是获取 url 的 html 响应并将其发送到客户端: https://www.krakend.io/docs/enterprise/backends/client-redirect/
解决方案是告诉您的浏览器有来自服务器的重定向:
return res.status(300).header('Location', yourUrl).send('Redirecting to...');