HAProxy-后端服务器的基本身份验证

ovn*_*tar 3 haproxy

我使用以下配置从本地127.0.0.1:2000代理访问Internet。

global
    log 127.0.0.1   local0
    log 127.0.0.1   local1 notice
    #log loghost    local0 info
    maxconn 4096
    #chroot /usr/share/haproxy
    user haproxy
    group haproxy
    daemon
    #debug
    #quiet

defaults
    log global
    mode    http
    option  httplog
    option  dontlognull
    retries 3
    option redispatch
    maxconn 2000
    contimeout  5000
    clitimeout  50000
    srvtimeout  50000

listen appname 0.0.0.0:2000
    mode http
    stats enable
    acl white_list src 127.0.0.1 
    tcp-request content accept if white_list
    tcp-request content reject
    stats uri /haproxy?stats
    stats realm Strictly\ Private
    stats auth special_admin:special_username
    balance roundrobin
    option httpclose
    option forwardfor
    server lamp1 23.123.1.110:3128 check
Run Code Online (Sandbox Code Playgroud)

不幸的是,我需要通过http基本身份验证“ special_admin:special_username”对我的外部代理23.123.1.110进行身份验证。我的问题是,有没有办法使用基本身份验证,例如:

server lamp1 http://special_admin:special_username@23.123.1.110:3128 check
Run Code Online (Sandbox Code Playgroud)

谢谢

ges*_*lix 7

在您的示例中,您只需要添加Authorization带有授权方法的必要标头,并将其username:password编码为base64,如下所示:

reqadd Authorization:\ Basic\ c3BlY2lhbF9hZG1pbjpzcGVjaWFsX3VzZXJuYW1l
Run Code Online (Sandbox Code Playgroud)

我创建了这样的base64编码字符串:

echo -n "special_admin:special_username" | base64
Run Code Online (Sandbox Code Playgroud)

有关HTTP基本授权的更多详细信息,请参见https://en.wikipedia.org/wiki/Basic_access_authentication#Client_side

  • 自 HAProxy 2.1 起不再支持 Reqadd!请改用 `http-request add-header Authorization "Basic BASE64STRING"`。 (2认同)

dev*_*min 5

下面列出的步骤对我有用。

# haproxy conf
global
  log 127.0.0.1   local1
  maxconn 4096

defaults
  mode http
  maxconn 2048

userlist AuthUsers
        user admin password $6$SydPP/et7BGN$C5VIhcn6OxuIaLPhCDCmzJyqDYQF8skik3J6sApkXPa6YPSVGutcgQPpdX/VEycGNi3sw7NxLSflEb53gzJtA1

frontend nginx-frontend
  bind *:5000
  mode http
  timeout connect 5s
  timeout client 5s
  timeout server 5s
  default_backend nginx-backend

  
  # For Path based basic authentication  use this commented example
  #acl PATH_cart  path_beg -i /testing 
  #acl authusers_acl http_auth(AuthUsers)
  #http-request auth realm nginx-backend  if PATH_cart  !authusers_acl

  acl authusers_acl http_auth(AuthUsers)      
  http-request auth realm nginx-backend if !authusers_acl

backend nginx-backend
  server nginx nginx:80  check inter 5s rise 2 fall 3
Run Code Online (Sandbox Code Playgroud)

安装以下软件包以生成哈希密码

sudo apt-get install whois

mkpasswd -m sha-512 '你的密码'

mkpasswd -m sha-512 admin@456

预期产出

$6$gnGNapo/XeXYg39A$T/7TDfMrZXUDPbv5UPYemrdxdh5xEwqBrzSbpJYs9rfxLbQtgQzxyzkSGWIVOEGze8KrsA0urh3/dG.1xOx3M0

复制生成的密码并粘贴到 haproxy.cfg 文件中

#部署容器来测试配置

sudo docker run -d --name nginx nginx
sudo docker run -d -p 5000:5000 --name haproxy --link nginx:nginx -v /home/users/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg haproxy
Run Code Online (Sandbox Code Playgroud)

在浏览器中查看,会提示用户名和密码。