如何根据传入请求选择haproxy后端(同一服务器使用不同的端口)?

时间:2019-01-22 19:19:39

标签: haproxy

我正在尝试根据传入请求路由流量,而我正在使用的后端服务器是相同的,但端口不同。但是,无论传入请求如何,所有流量都将流向同一个后端pserver。有人可以帮我吗?

frontend defhttps-in

   acl host_portal hdr(host) -i server1:80  
   acl host_mobility hdr(host) -i server1:81  
   acl host_mobility path_beg /HHServlet  
   acl host_mobility path_beg /ota  
   acl host_mobility path_beg /TransportServlet  
   use_backend pservers if host_portal  
   use_backend mobileservers if host_mobility  
   default_backend pservers    


backend pservers  
    balance roundrobin  
    cookie JSESSIONID prefix nocache  
    cookie SERVERID insert indirect nocache  
    option httpchk get /StatusServlet  
    http-check expect ! string red  
    server portal1 server1:80  cookie P1 check  

backend mobileservers  
    balance roundrobin  
    cookie JSESSIONID prefix nocache  
    cookie SERVERID insert indirect nocache  
    option httpchk get /StatusServlet  
    http-check expect ! string red  
    server mobility1 server1:81 cookie M1 check  

1 个答案:

答案 0 :(得分:0)

请参阅下面的我的配置文件,并相应地进行修改。

我建议您观看这些视频并阅读这些资源,以帮助更好地了解HAProxy。

视频:

HAProxy博客:

配置文件:

##############################

global
      log         127.0.0.1 syslog
      maxconn     1000
      user        haproxy
      group       haproxy
      daemon


    defaults
      log  global
      mode  http
      option  httplog
      option  dontlognull
      option  http-server-close
      option  forwardfor except 127.0.0.0/8
      option  redispatch
      option  contstats
      retries  3
      timeout  http-request 10s
      timeout  queue 1m
      timeout  connect 10s
      timeout  client 1m
      timeout  server 1m
      timeout  check 10s
      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


    ###########################################
    #
    # HAProxy Stats page
    #
    ###########################################

    listen stats
      bind *:9090
      mode  http
      maxconn  10
      stats  enable
      stats  hide-version
      stats  realm Haproxy\ Statistics
      stats  uri /
      stats  auth -----:-----



    ###########################################
    #
    # Front end for all
    #
    ###########################################

    frontend ALL
      bind   *:80
      mode   http

      # Define path for lets encrypt
      acl is_letsencrypt path_beg -i /.well-known/acme-challenge/
      use_backend letsencrypt if is_letsencrypt

      # Define hosts
      acl host_horizon hdr(host) -i horizon.eduarmor.com
      acl host_eduarmor hdr(host) -i www.eduarmor.com
      acl host_nextcloud hdr(host) -i nextcloud.eduarmor.com
      acl host_git hdr(host) -i git.eduarmor.com
      acl host_minecraft hdr(host) -i mine.eduarmor.com
      acl host_sugar hdr(host) -i sugar.eduarmor.com
      acl host_maas hdr(host) -i maas.eduarmor.com
      acl host_rocketchat hdr(host) -i rocketchat.eduarmor.com
      acl host_hive hdr(host) -i hive.eduarmor.com
      acl host_portainer hdr(host) -i portainer.eduarmor.com

      # Direct hosts to backend
      use_backend horizon if host_horizon
      use_backend eduarmor if host_eduarmor
      use_backend nextcloud if host_nextcloud
      use_backend git if host_git
      use_backend minecraft if host_minecraft
      use_backend sugar if host_sugar
      use_backend maas if host_maas
      use_backend rocketchat if host_rocketchat
      use_backend hive if host_hive
      use_backend portainer if host_portainer

    ###########################################
    #
    # Back end letsencrypt
    #
    ###########################################

    backend letsencrypt
      server letsencrypt 127.0.0.1:8888

    ###########################################
    #
    # Back end for Horizon
    #
    ###########################################

    backend horizon
      balance         roundrobin
      # option          httpchk GET /check
      option          httpchk GET /
      # http-check      expect rstring ^UP$
      default-server  inter 3s fall 3 rise 2
      server          server1 10.0.0.30:80 check
      #     server          server2 0.0.0.0:80 check
    ###########################################
    #
    # Back end for EduArmor
    #
    ###########################################
    backend eduarmor
      balance         roundrobin
      # option          httpchk GET /check
      option          httpchk GET /
      # http-check      expect rstring ^UP$
      default-server  inter 3s fall 3 rise 2
      server          server1 10.0.0.59:80 check
      #     server server2 0.0.0.0:80 check

    ##########################################
    #
    # Back end for Nextcloud
    #
    ##########################################
    backend nextcloud
      balance         roundrobin
      # option         httpchk GET /check
      option          httpchk GET /
      # http-check     expect rstring ^UP$
      default-server  inter 3s fall 3 rise 2
      server          server1 10.0.0.101:80 check


    ##########################################
    #
    # Back end, Gitlab
    #
    ##########################################
    backend git
     balance        roundrobin
     # option        httpchk GET /check
     option         httpchk GET /
     # http-check    expect rstring ^UP$
     default-server inter 3s fall 3 rise 2
     server         server1 10.0.0.101:800 check
     server         server2 10.0.0.102:800 check



    ##########################################
    #
    # Back end, Minecraft
    #
    ##########################################
    backend minecraft
     balance        roundrobin
     # option        httpchk GET /check
     option         httpchk GET /
     # http-check    expect rstring ^UP$
     default-server inter 3s fall 3 rise 2
     server         server1 10.0.0.101:25565 check