烂泥:haproxy学习之手机规则匹配

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

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

1、业务须要

如今根据业务的实际须要,有如下几种不一样的需求。以下:redis

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这个地址。服务器

2、haproxy配置

下面根据不一样的业务需求进行配置haproxy,以下。frontend

2.1 转发全部手机请求配置

要把全部的手机端请求转到www.ilanni.com这个地址,须要咱们首先把访问的终端匹配出来,haproxy能够经过hdr_sub(user-agent)这个参数把手机端匹配出来。iphone

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

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_p_w_picpath001

经过测试你会发现,在手机浏览器中输入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_p_w_picpath002

经过上图,咱们能够看到手机端访问http://http.ilanni.com/docs/这个链接的话,是能够直接访问的。

3、其余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_p_w_picpath003

经过上图,咱们能够看到手机端的访问确实跳转到了咱们指定的页面。

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

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

clip_p_w_picpath004

经过上图,咱们能够看到手机端访问http://http.ilanni.com/manager/status这个链接的话,是能够直接访问的。

clip_p_w_picpath001[1]

经过上图,咱们能够看到若是手机端访问的不是is_docs这个规则中定义的url话,则会所有跳转到http://www.ilanni.com这个地址的。

相关文章
相关标签/搜索