如何让 HA 代理自行跟踪重定向?

Ser*_*rik 3 java redirect http haproxy web

我有 Java 客户端,它通过 HA 代理与 3rd 方服务对话。3rd 方服务最近发生了变化,所以现在它返回 302(临时移动)而不是 200(Ok),这会导致我的 java 客户端失败,因为它期望实际响应为 200。出于多种原因,我想避免对 Java 客户端进行任何代码更改。

那么,问题来了:有没有办法让 HA 代理自己跟踪重定向,并且只将结果(不是 3xx http 代码)返回给客户端?

还有一件事要提到:我通过 http 访问 HA 代理,HA 代理通过 https 访问 3rd 方资源,并在 https 上返回带有位置的 302。位置各不相同,因此无法将 HA 代理配置到新位置。

HA 代理版本: HA-Proxy version 1.7.5 2017/04/03

操作系统: CentOS Linux release 7.2.1511 (Core)

nus*_*ver 9

你可以用这个

$ haproxy -v
Nuster version 1.8.8.2.2
Copyright (C) 2017-2018, Jiang Wenyuan, <koubunen AT gmail DOT com >

HA-Proxy version 1.8.8.2 2018/05/29
Copyright 2000-2018 Willy Tarreau <willy@haproxy.org>
Run Code Online (Sandbox Code Playgroud)

配置文件

global
    debug
    lua-load handle_redirect.lua

frontend web1
    bind *:8080
    mode http

    default_backend app1

backend app1
    mode http

    http-response lua.handle_redirect if { status eq 301 }

    server s1 127.0.0.1:9000
Run Code Online (Sandbox Code Playgroud)

handle_redirect.lua

http = require("http")

core.register_action("handle_redirect", {"http-res"}, function(txn)

  local hdr = txn.http:res_get_headers()
  if hdr["location"] == nil then
    return nil
  end


  local r, msg = http.get{
    url = hdr["location"][0]
  }

  if r == nil then
    return msg
  end

  local res = "HTTP/1.1 " .. r.status_code .. " " .. r.reason .. "\r\n"
  for k, v in pairs(r.headers) do
    res = res .. k .. ": " .. v .. "\r\n"
  end
  res = res .. "\r\n"
  res = res .. r.content


  txn.res:set(res)
  txn.done(txn)

end)
Run Code Online (Sandbox Code Playgroud)

http.luahttps://github.com/haproxytech/haproxy-lua-http/blob/master/http.lua下载

haproxy -f haproxy.conf
Run Code Online (Sandbox Code Playgroud)