如何让 HA Proxy 自己跟随重定向?

How to Make HA Proxy to Follow Redirects by Itself?

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

所以,问题来了:有没有办法让 HA Proxy 自己跟随重定向,并且只向客户端发送 return 结果(不是 3xx http 代码)?

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

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

OS: CentOS Linux release 7.2.1511 (Core)

看来您正在尝试解决错误的问题。

听起来站点正在返回 302 因为 您没有使用 HTTPS。因此,将代理配置为使用 HTTPS。

server foo foo.example.com:443 ssl verify none

您不必使用 HTTPS 访问代理,但代理需要使用 HTTPS 发出出站请求。

此外,HAProxy 不会执行您要求的操作。可以在返回时修改响应,但您不能强制代理遵循重定向。

你可以用这个

$ 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>

haproxy.conf

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

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)

https://github.com/haproxytech/haproxy-lua-http/blob/master/http.lua

下载 http.lua
haproxy -f haproxy.conf