【问题标题】:haproxy: get the host namehaproxy:获取主机名
【发布时间】:2017-09-25 20:22:48
【问题描述】:

我正在尝试获取 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 节点的 ip/host,因为它涉及到 haproxy。

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

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

【问题讨论】:

    标签: http https haproxy


    【解决方案1】:

    您可以通过src var 获取源地址。 Haproxy 在 this 下持有请求者 IP,可以在 acl 等地方使用。

    以下列方式使用它:%[src]

    查看这些链接:srcfetching-samples(under layer 4)

    【讨论】:

    • 基本上我想知道请求者发出的 haproxy 节点的 ip/主机。例如:如果请求是http://1.1.1.1:8080,则需要转发到https://1.1.1.1:8143或者http://hello.com:8080应该f/wed到https:// /hello.com:8143
    【解决方案2】:

    有关更多详细信息,请参阅 How do I set a dynamic variable in 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
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-22
      • 2011-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-08
      相关资源
      最近更新 更多