zoukankan      html  css  js  c++  java
  • 烂泥:haproxy学习之手机规则匹配

    本文由ilanniweb提供友情赞助,首发于烂泥行天下

    想要获得更多的文章,可以关注我的微信ilanniweb。

    今天我们来介绍下有关haproxy匹配手机的一些规则配置。

    一、业务需要

    现在根据业务的实际需要,有以下几种不同的需求。如下:

    1.1 转发所有手机请求

    所有通过手机端访问http.ilanni.com域名的话,全部转发到http://www.ilanni.com这个地址,而PC端不受此限制。

    1.2 根据url进行转发

    如果手机端请求http.ilanni.com这个域名的url中,以docs或者manager这两个关键词开头的话,把该请求转发到后端的服务器,而PC端不受此限制。

    也就是说手机端访问具体的url地址的话,可以正常访问。如果是直接访问http.ilanni.com域名的话,直接把该请求转发到http://www.ilanni.com这个地址。

    二、haproxy配置

    下面根据不同的业务需求进行配置haproxy,如下。

    2.1 转发所有手机请求配置

    要把所有的手机端请求转到www.ilanni.com这个地址,需要我们首先把访问的终端匹配出来,haproxy可以通过hdr_sub(user-agent)这个参数把手机端匹配出来。

    手机端匹配出来后,我们就可以定义相应的规则,把手机端的请求转发到www.ilanni.com这个地址了。

    haproxy具体配置文件如下:

    global

    log 127.0.0.1 local0

    log 127.0.0.1 local1 notice

    maxconn 4096

    uid 188

    gid 188

    daemon

    tune.ssl.default-dh-param 2048

    defaults

    log global

    mode http

    option httplog

    option dontlognull

    option http-server-close

    option forwardfor except 127.0.0.1

    option redispatch

    retries 3

    option redispatch

    maxconn 2000

    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

    listen admin_stats

    bind 0.0.0.0:1080

    mode http

    option httplog

    maxconn 10

    stats refresh 30s

    stats uri /stats

    stats auth admin:admin

    stats hide-version

    frontend weblb

    bind *:80

    acl is_http hdr_beg(host) http.ilanni.com

    acl ua hdr_sub(user-agent) -i android iphone

    redirect prefix http://www.ilanni.com if ua

    use_backend httpserver if is_http

    backend httpserver

    balance source

    server web1 127.0.0.1:8080 maxconn 1024 weight 3 check inter 2000 rise 2 fall 3

    在以上配置文件中,有以下两行需要注意:

    acl ua hdr_sub(user-agent) -i android iphone

    redirect prefix http://www.ilanni.com if ua

    这两行,第一行是第一个ua规则,该规则是判断是否是手机端。

    注意:在此手机端,我们只匹配了安卓手机和iphone。

    第二行是跳转规则,如果匹配是手机端的话,那么直接跳转到http://www.ilanni.com这个地址。

    如果是PC端的话,默认跳转到httpserver这个后端服务器组。

    以上配置是一台服务器对外只提供一个域名访问的请求,如果有两个域名的话,就要进行如下配置:

    global

    log 127.0.0.1 local0

    log 127.0.0.1 local1 notice

    maxconn 4096

    uid 188

    gid 188

    daemon

    tune.ssl.default-dh-param 2048

    defaults

    log global

    mode http

    option httplog

    option dontlognull

    option http-server-close

    option forwardfor except 127.0.0.1

    option redispatch

    retries 3

    option redispatch

    maxconn 2000

    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

    listen admin_stats

    bind 0.0.0.0:1080

    mode http

    option httplog

    maxconn 10

    stats refresh 30s

    stats uri /stats

    stats auth admin:admin

    stats hide-version

    frontend weblb

    bind *:80

    acl is_http hdr_beg(host) http.ilanni.com

    acl is_haproxy hdr_beg(host) haproxy.ilanni.com

    acl ua hdr_sub(user-agent) -i android iphone

    redirect prefix http://www.ilanni.com if ua !is_haproxy

    use_backend haproxyserver if ua is_haproxy

    use_backend haproxyserver if is_haproxy

    use_backend httpserver if is_http

    backend httpserver

    balance source

    server web1 127.0.0.1:8080 maxconn 1024 weight 3 check inter 2000 rise 2 fall 3

    backend haproxyserver

    balance source

    server web1 127.0.0.1:7070 maxconn 1024 weight 3 check inter 2000 rise 2 fall 3

    2.2 测试转发所有手机请求

    现在我们来测试该跳转功能,如下:

    clip_image001

    通过测试你会发现,在手机浏览器中输入http.ilanni.com会自动跳转到http://www.ilanni.com这个地址。

    2.3 根据url进行转发配置

    根据手机端请求的url进行转发的话,首先也是需要匹配出手机端,然后定义url路径规则。最后结合手机端和url路径规则,进行跳转。

    haproxy具体配置文件,如下:

    global

    log 127.0.0.1 local0

    log 127.0.0.1 local1 notice

    maxconn 4096

    uid 188

    gid 188

    daemon

    tune.ssl.default-dh-param 2048

    defaults

    log global

    mode http

    option httplog

    option dontlognull

    option http-server-close

    option forwardfor except 127.0.0.1

    option redispatch

    retries 3

    option redispatch

    maxconn 2000

    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

    listen admin_stats

    bind 0.0.0.0:1080

    mode http

    option httplog

    maxconn 10

    stats refresh 30s

    stats uri /stats

    stats auth admin:admin

    stats hide-version

    frontend weblb

    bind *:80

    acl is_http hdr_beg(host) http.ilanni.com

    acl is_docs url_beg /docs /manager

    acl ua hdr_sub(user-agent) -i android iphone

    redirect prefix http://www.ilanni.com if ua !is_docs

    use_backend httpserver if ua is_docs

    use_backend httpserver if is_http

    backend httpserver

    balance source

    server web1 127.0.0.1:8080 maxconn 1024 weight 3 check inter 2000 rise 2 fall 3

    在上述配置文件中,需要以下几行解释下。

    acl is_docs url_beg /docs /manager

    定义一个is_docs规则。如果url以/docs或者/manager开头的,则全部属于该规则。

    acl ua hdr_sub(user-agent) -i android iphone

    redirect prefix http://www.ilanni.com if ua !is_docs

    这两行首先是匹配出手机端,然后如果是手机端访问,并且访问的不是is_docs规则的话,则直接跳转到http://www.ilanni.com这个地址。

    use_backend httpserver if ua is_docs

    这条命令是,如果是手机端访问,并且访问的是is_docs规则的话,则直接跳转到httpserver这个后端服务器组。

    如果是PC端的话,默认跳转到httpserver这个后端服务器组。

    2.4 测试根据url进行转发

    根据url转发配置完毕后,我们现在来测试。如下:

    clip_image002

    通过上图,我们可以看到手机端访问http://http.ilanni.com/docs/这个连接的话,是可以直接访问的。

    三、其他haproxy配置

    在前面我们讲解了有关手机的相关配置,在实际的生产环境中,有时候我们会碰到一些奇奇怪怪的要求。

    要求所有手机端访问的http.ilanni.com,转到指定的页面。

    haproxy主要配置文件如下:

    frontend weblb

    bind *:80

    acl is_http hdr_beg(host) http.ilanni.com

    acl ua hdr_sub(user-agent) -i android iphone

    redirect prefix http://www.ilanni.com/?p=10624 if ua

    use_backend httpserver if is_http

    backend httpserver

    balance source

    server web1 127.0.0.1:8080 maxconn 1024 weight 3 check inter 2000 rise 2 fall 3

    以上配置是所有手机端访问的,都跳转到http://www.ilanni.com/?p=10624这个页面。测试如下:

    clip_image003

    通过上图,我们可以看到手机端的访问确实跳转到了我们指定的页面。

    类似这样的要求,一般会在升级公司相关业务时提出,对公司的公网IP可以正常,但是外部访问时,跳转到指定的维护页面。

    这个我们可以根据源IP地址进行匹配,在此就不进行详细的讲解了。

    clip_image004

    通过上图,我们可以看到手机端访问http://http.ilanni.com/manager/status这个连接的话,是可以直接访问的。

    clip_image001[1]

    通过上图,我们可以看到如果手机端访问的不是is_docs这个规则中定义的url话,则会全部跳转到http://www.ilanni.com这个地址的。

  • 相关阅读:
    面试问题之C++语言:C++中指针和引用的区别
    手撕代码:最长回文子串
    手撕代码:求字符串最长回文子序列
    手撕代码:用宏来实现获取数组的大小
    手撕代码之线程:thread类简单使用
    面试问题之计算机网络:OSI七层网络模型及相关协议
    C++各种输入
    C printf格式化输出
    记一次mac 安装MySQL-python 的惨痛经历
    记一次tomcat程序运行慢的处理过程
  • 原文地址:https://www.cnblogs.com/ilanni/p/4945830.html
Copyright © 2011-2022 走看看