通过 HAProxy 转发 SSL 流量和认证证书
Forward SSL traffic and authentication certificates through HAProxy
我的客户有一个 nginx,我可以 POST 成功地使用:
curl -v --cacert ca.crt --cert client.crt --key client.key -POST https://nginx:8443/api/ -H 'Content-Type: application/json' -H 'cache-control: no-cache' -d@test.json
现在我在 nginx 前面安装了一个 haproxy,我正在尝试用同样的方法做一个 POST,不成功:
curl -v --cacert ca.crt --cert client.crt --key client.key -POST http://haproxy:8443/api/ -H 'Content-Type: application/json' -H 'cache-control: no-cache' -d@test.json
错误:
<center>The plain HTTP request was sent to HTTPS port</center>
<hr><center>nginx</center>
这是我的 haproxy 配置:
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
stats socket /var/lib/haproxy/stats
defaults
mode tcp
log global
option tcplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
frontend main *:8443
acl url_static path_beg -i /static /images /javascript /stylesheets
acl url_static path_end -i .jpg .gif .png .css .js
use_backend static if url_static
default_backend app
backend static
balance roundrobin
server static 127.0.0.1:8443
backend app
mode tcp
balance roundrobin
server nginx nginx01:8443
我想通过 HAProxy 转发 SSL 流量,并将用于身份验证的证书传递给 nginx。
我知道拥有两个 LB 没有任何意义,但我无法修改 nginx 和后面的 api 服务器,但客户端将是内部的。
如您所见,此时我可以访问 nginx,但 haproxy 不会将证书和密钥从请求传递到 nginx 后端。
我错过了什么吗?这是我能达到的目标吗?
ps:如果我在后端设置 'ssl verify none',我会得到 'No required SSL certificate was sent'。
如果我在后端设置 'send-proxy',我会从 nginx 收到“400 Bad Request”。
您需要将 ssl 配置添加到 haproxy 并设置一些 headers 将转发到 nginx。
# your other config from above
backend app
mode tcp
balance roundrobin
server nginx nginx01:8443 ssl ca-file <The ca from nginx backend>
实施的解决方案是 SS/TLS pass-through 来自 https://www.haproxy.com/documentation/haproxy/deployment-guides/tls-infrastructure/
将前端和后端都设置为 tcp 模式我能够通过证书和 nginx 验证并进行身份验证。
我的客户有一个 nginx,我可以 POST 成功地使用:
curl -v --cacert ca.crt --cert client.crt --key client.key -POST https://nginx:8443/api/ -H 'Content-Type: application/json' -H 'cache-control: no-cache' -d@test.json
现在我在 nginx 前面安装了一个 haproxy,我正在尝试用同样的方法做一个 POST,不成功:
curl -v --cacert ca.crt --cert client.crt --key client.key -POST http://haproxy:8443/api/ -H 'Content-Type: application/json' -H 'cache-control: no-cache' -d@test.json
错误:
<center>The plain HTTP request was sent to HTTPS port</center>
<hr><center>nginx</center>
这是我的 haproxy 配置:
global
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
stats socket /var/lib/haproxy/stats
defaults
mode tcp
log global
option tcplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000
frontend main *:8443
acl url_static path_beg -i /static /images /javascript /stylesheets
acl url_static path_end -i .jpg .gif .png .css .js
use_backend static if url_static
default_backend app
backend static
balance roundrobin
server static 127.0.0.1:8443
backend app
mode tcp
balance roundrobin
server nginx nginx01:8443
我想通过 HAProxy 转发 SSL 流量,并将用于身份验证的证书传递给 nginx。 我知道拥有两个 LB 没有任何意义,但我无法修改 nginx 和后面的 api 服务器,但客户端将是内部的。 如您所见,此时我可以访问 nginx,但 haproxy 不会将证书和密钥从请求传递到 nginx 后端。 我错过了什么吗?这是我能达到的目标吗?
ps:如果我在后端设置 'ssl verify none',我会得到 'No required SSL certificate was sent'。 如果我在后端设置 'send-proxy',我会从 nginx 收到“400 Bad Request”。
您需要将 ssl 配置添加到 haproxy 并设置一些 headers 将转发到 nginx。
# your other config from above
backend app
mode tcp
balance roundrobin
server nginx nginx01:8443 ssl ca-file <The ca from nginx backend>
实施的解决方案是 SS/TLS pass-through 来自 https://www.haproxy.com/documentation/haproxy/deployment-guides/tls-infrastructure/ 将前端和后端都设置为 tcp 模式我能够通过证书和 nginx 验证并进行身份验证。