【问题标题】:HAProxy and LXD Containers - Route based on naked domain, specific sub-domain and all other sub-domainsHAProxy 和 LXD 容器 - 基于裸域、特定子域和所有其他子域的路由
【发布时间】:2018-04-19 07:27:17
【问题描述】:

我已尝试寻找与我遇到的问题类似的解决方案,但找不到任何东西。

我已经设置了一些 LXD 容器,其中一个是 HAProxy,它从主机的公共 IP 地址接收端口 80 流量。 HAProxy 然后根据域将流量发送到正确的容器。

我遇到的问题是,即使我已经为 subdomain.example.com 设置了 domain_specific_subdomain ACL 规则,但另一个 domain_root ACL 规则优先。

容器:

  • lxd-container-web1:用于根域或任何其他子域,例如(www., dev.) 'example.com'
  • lxd-container-web2:仅在此特定子域“subdomain.example.com”时使用

问题:我怎样才能拥有这样的裸域 example.com 和所有其他子域(例如 www.、dev. 等)去这个后端 backend_web1 除了subdomain.example.com应该转到:backend_web2 后端。

这是我的配置文件:

global
        log /dev/log    local0
        log /dev/log    local1 notice
        chroot /var/lib/haproxy
        stats socket /run/haproxy/admin.sock mode 660 level admin
        stats timeout 30s
        user haproxy
        group haproxy
        daemon

        # Default SSL material locations
        ca-base /etc/ssl/certs
        crt-base /etc/ssl/private

        # Default ciphers to use on SSL-enabled listening sockets.
        # For more information, see ciphers(1SSL). This list is from:
        #  https://hynek.me/articles/hardening-your-web-servers-ssl-ciphers/
        ssl-default-bind-ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS
        ssl-default-bind-options no-sslv3

defaults
        log     global
        mode    http
        option  httplog
        option  dontlognull
        option   forwardfor
        option   http-server-close
        timeout connect 5000
        timeout client  50000
        timeout server  50000
        errorfile 400 /etc/haproxy/errors/400.http
        errorfile 403 /etc/haproxy/errors/403.http
        errorfile 408 /etc/haproxy/errors/408.http
        errorfile 500 /etc/haproxy/errors/500.http
        errorfile 502 /etc/haproxy/errors/502.http
        errorfile 503 /etc/haproxy/errors/503.http
        errorfile 504 /etc/haproxy/errors/504.http


# Frontend
frontend www_frontend
        bind *:80     # Bind to port 80 (www) on the container


        # Covers naked domain and all subdomains
        acl domain_root hdr(host) -i example.com

        # Specific sub-domain
        acl domain_specific_subdomain hdr(host) -i subdomain.example.com


        # Redirect the connection to the proper server cluster, depending on the match.
        use_backend backend_web1 if domain_root
        use_backend backend_web2 if domain_specific_subdomain


# Domain dependent containers
backend backend_web1
        balance leastconn

        # We set the X-Client-IP HTTP header. This is useful if we want the web server to know the real client IP.
        http-request set-header X-Client-IP %[src]

        # This backend, named here "backend_web1", directs to container "lxd-container-web1.lxd" (hostname).
        server web1 lxd-container-web1:80 check

backend backend_web2
        balance leastconn
        http-request set-header X-Client-IP %[src]
        server web2 lxd-container-web2.lxd:80 check

【问题讨论】:

    标签: linux load containers haproxy lxd


    【解决方案1】:

    来自 haproxy 文档:

    可以有任意多的“use_backend”规则。所有这些规则都按照它们的声明顺序进行评估,第一个匹配的将分配后端。

    因此,如果您切换 use_backend 行顺序,它应该可以工作。

        # Redirect the connection to the proper server cluster, depending on the match.
        use_backend backend_web2 if domain_specific_subdomain
        use_backend backend_web1 if domain_root
    

    或者更好,也许default_backend在这里会更好:

        # Use backend_web2 if ACL matches
        use_backend backend_web2 if domain_specific_subdomain
    
        # Otherwise, default to backend_web1
        default_backend backend_web1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-26
      • 1970-01-01
      • 2013-11-19
      • 1970-01-01
      • 2020-11-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多