爛泥:haproxy學習之手機規則匹配

本文由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這個地址的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章