HAProxy:获取主机名

3

我正在尝试在haproxy节点上获取请求者的主机/IP。我的haproxy配置如下:

frontend www-http
    bind *:9000
    http-request redirect location https://%fi:9143

frontend www-https
    bind *:9143 ssl crt /root/keys.pem
    reqadd X-Forwarded-Proto:\ https
    default_backend www-backend

backend www-backend
    balance roundrobin
    cookie SERVERID insert indirect nocache
    server server1 1.1.1.1:9080 cookie server1 weight 1 maxconn 1024 check

在这里,如果有任何http请求,则需要转发到https。现在请求可能是以IP地址或完全限定形式的主机名的形式出现的,如下所示:

http://10.10.10.10:9000 

需要将此转发至https://10.10.10.10:9143

同样,请求可能以完全限定的主机名形式出现,例如:

http://myhost.domain.com:9000

需要将此内容转发到https://myhost.domain.com:9143

基本上10.10.10.10和myhost.domain.com是同一个系统。

现在,使用上述haproxy配置,由于它是%fi(frontend_ip),因此我无法获得以下信息,因此它会重定向到https://10.10.10.10:9143

因此,我的问题是如何在haproxy节点到达haproxy时获取其ip/host。

我尝试了以下选项,但没有成功:

http-request redirect location https://%f:9143
http-request redirect location https://%[req.hdr(Host)]:9143

翻译来源:https://www.haproxy.com/doc/aloha/7.0/haproxy/log_format_rules.html

这是有关HAProxy日志格式规则的文档。
2个回答

1

请参考如何在HAProxy中设置动态变量?获取更多详细信息,但是以此为基础,以下内容适用于您:

frontend www-http
    bind *:9000

    # Redirect user from http port to https port
    http-request set-var(req.hostname) req.hdr(Host),field(1,:),lower
    http-request redirect code 301 location https://%[var(req.hostname)]:9143 if !{ ssl_fc }

frontend www-https
    bind *:9143 ssl crt /root/keys.pem
    reqadd X-Forwarded-Proto:\ https
    default_backend www-backend

backend www-backend
    balance roundrobin
    cookie SERVERID insert indirect nocache
    server server1 1.1.1.1:9080 cookie server1 weight 1 maxconn 1024 check

我的情况有些不同,因为我只想重定向一个统计UI的URL,这样我就不必更新我们内部文档中的每个统计URL。以下是适用于我的情况的解决方案(如果对其他人有帮助):

userlist stats-auth
    group admin users adminuser
    group readonly users readonlyuser

    # Passwords created via mkpasswd -m sha-512 PASSWORD_HERE
    user adminuser password NOT_REAL_PASSWORD
    user readonlyuser password NOT_REAL_PASSWORD

listen stats

    # Used just for the initial connection before we redirect the user to https
    bind *:4711

    # Combined file containing server, intermediate and root CA certs along
    # with the private key for the server cert.
    bind *:4712 ssl crt /etc/ssl/private/my-site-name_combined_cert_bundle_with_key.pem

    option dontlognull
    mode http
    option httplog

    # Redirect user from http port to https port
    http-request set-var(req.hostname) req.hdr(Host),field(1,:),lower
    http-request redirect code 301 location https://%[var(req.hostname)]:4712/ if !{ ssl_fc }

    acl AUTH            http_auth(stats-auth)
    acl AUTH_ADMIN      http_auth_group(stats-auth) admin

    stats enable

    # The only "site" for using these ports is the admin UI, so use '/' as
    # the base path instead of requiring something like '/haproxy_stats' or
    # '/stats' in order to display the UI.
    stats uri /

    # Force a login if not already authenticated
    stats http-request auth unless AUTH

    # Allow administrator functionality if user logged in using admin creds
    # (there are separate read-only username and password pairs)
    stats admin if AUTH_ADMIN

我省略了前端和后端配置,因为它们更长/详细。

0

你可以通过src变量获取源地址。 Haproxy在此处保存请求方的IP,可以在acl和其他地方使用。

用以下方式进行日志记录:%[src]

请查看以下链接:srcfetching-samples(under layer 4)


基本上我想知道haproxy节点的ip/host,因为请求者正在发出请求。例如:如果请求是http://1.1.1.1:8080,则需要转发到https://1.1.1.1:8143或http://hello.com:8080应该被转发到https://hello.com:8143。 - u_peerless

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接