如何使HA代理自行跟踪重定向? [英] How to Make HA Proxy to Follow Redirects by Itself?

查看:92
本文介绍了如何使HA代理自行跟踪重定向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java客户端,它通过HA代理与第三者服务进行对话.第三方服务最近已更改,因此现在它返回302(临时移动)而不是200(确定),这会导致我的Java客户端失败,并导致期望200的实际响应.由于多种原因,我想避免对Java客户端进行任何代码更改.

I have java client which talks to 3rd party service through HA Proxy. 3rd party service was recently changed, so now it is returning 302(Moved Temporarily) instead of 200(Ok) which causes failure on my java client cause it expects 200 with actual response. For a number of reasons I want to avoid any code changes to the java client.

所以,这是一个问题:是否有一种方法可以使HA代理自身遵循重定向并仅将结果(不是3xx http代码)返回给客户端?

So, here is the question: Is there a way to make HA Proxy to follow redirects by itself and only return result(not 3xx http code) to the client?

还有一件事要提到:我通过http访问HA代理,HA代理通过https访问第三者资源,并返回302,其中包含https上的位置.位置各不相同,因此不能将HA代理配置为新位置.

One more thing to mention: I access HA Proxy via http, HA proxy accesses 3rd party resource via https, and returns 302 with location on https. Location varies, so configure HA Proxy to the new location is not an option.

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

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

推荐答案

您可以使用此

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

http.lua"rel =" noreferrer> https://github.com /haproxytech/haproxy-lua-http/blob/master/http.lua

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

haproxy -f haproxy.conf

这篇关于如何使HA代理自行跟踪重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆