【问题标题】:How to configure Krakend so it return http redirect response as-is instead of following the http redirect?如何配置 Krakend 使其按原样返回 http 重定向响应而不是遵循 http 重定向?
【发布时间】:2020-11-06 00:12:45
【问题描述】:

我目前正在使用 Krakend (https://krakend.io) API Gateway 将请求代理到我的后端服务。我的后端服务 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 303 响应返回给客户端(带有位置响应标头)as-is,实际上是在遵循 http 重定向并返回重定向 Url 的响应,这是https://www.detik.com/的html响应。

我当前的 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 响应从我的后端服务返回到客户端?

谢谢

【问题讨论】:

  • 嗨!你有没有找到这个问题的答案?因为我目前遇到了完全相同的问题。

标签: api http-redirect api-gateway krakend


【解决方案1】:

我假设您正在调用此端点 /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"
        }

【讨论】:

  • 像魅力一样工作!你是最棒的!
猜你喜欢
  • 2015-03-01
  • 2019-05-10
  • 2021-04-29
  • 2016-12-01
  • 2018-05-11
  • 2011-11-11
  • 1970-01-01
相关资源
最近更新 更多