如果我理解正确,您希望通过客户端应用程序并基于该代理通过连接到其他后端服务器来设置自定义头。
我喜欢使用HAProxy,你也可以看看Nginx。
您可以从发行版的包管理器在linux上安装HAProxy,也可以使用可用的HAProxy docker容器。
sudo pacman -S haproxy
sudo systemctl start haproxy
haproxy.cfg
找到配置文件,然后复制我在下面发布的haproxy.cfg配置片段,而不是现有的默认配置。
/etc/haproxy/haproxy.cfg
为了在HAProxy中实现您想要的功能,您需要将所有客户端设置为与该主HAProxy服务器通信,然后该服务器将根据您可以在客户端设置的自定义头的值将连接转发到您拥有的不同后端服务器,例如
"x-mycustom-header: Server-one"
下面是一个简单的HAProxy配置文件(HAProxy.cfg)设置示例,该文件只有两个后端服务器,但您可以添加更多。
这里的逻辑是,所有客户端都会向侦听端口80的HAProxy服务器发出http请求,然后HAProxy会检查名为
x-mycustom-header
'客户端添加并基于该值,它将客户端转发到
backend_server_one
或
backend_server_two
出于测试目的,HAProxy和两个后端位于同一个盒子上,但监听的端口不同。HAProxy在端口80上,server1在127.0.0.1:3000上,server2在127.0.0.1:4000上。
cat haproxy.cfg
#---------------------------------------------------------------------
# Example configuration. See the full configuration manual online.
#
# http://www.haproxy.org/download/1.7/doc/configuration.txt
#
#---------------------------------------------------------------------
global
maxconn 20000
log 127.0.0.1 local0
user haproxy
chroot /usr/share/haproxy
pidfile /run/haproxy.pid
daemon
frontend main
bind :80
mode http
log global
option httplog
option dontlognull
option http_proxy
option forwardfor except 127.0.0.0/8
maxconn 8000
timeout client 30s
use_backend backend_server_one if { req.hdr(x-mycustom-header) server-one }
use_backend backend_server_two if { req.hdr(x-mycustom-header) server-two }
default_backend backend_server_one #when the header is something else default to the first backend
backend backend_server_one
mode http
balance roundrobin
timeout connect 5s
timeout server 5s
server static 127.0.0.1:3000 #change this ip to your 1st backend ip address
backend backend_server_two
mode http
balance roundrobin
timeout connect 5s
timeout server 30s
server static 127.0.0.1:4000 #change this ip to your 2nd backend ip address
nc -l 3000 # in the first screen
nc -l 4000 # in a second screen
sudo systemctl reload haproxy
为了确保HAProxy与您的新配置文件一起重新加载,您可以在端口80上发出http GET请求,并提供
标题。
您将能够在侦听端口3000的netcat实例的输出中看到请求。
"x-mycustom-header: Server-two"