This article is sponsored by Ilanniweb, starting in Slime Line the world
To get more articles, you can follow my ilanniweb.
Today, let's introduce some of the rule configurations for haproxy matching phones.
first, business needs
Now, according to the actual needs of the business, there are several different needs. As follows:
1.1
forwarding all phone requests
All access to the http.ilanni.com domain name through the mobile phone, all forwarded to http://www.ilanni.com this address, and the PC side is not subject to this limitation.
1.2
forwarding based on URL
If the phone-side request http.ilanni.com The URL of the domain name, starting with either the docs or the Manager keyword, forward the request to the backend server, which is not limited by the PC side.
This means that the mobile phone to access the specific URL address, you can access the normal. If you are directly accessing the http.ilanni.com domain name, forward the request directly to the http://www.ilanni.com address.
Second, haproxy configuration
The following are configured according to different business requirements Haproxy, as follows.
2.1
forwarding all phone request configuration
To get all the phone-side requests to www.ilanni.com this address, we need to first put the access to the terminal, Haproxy can be hdr_sub (user-agent) This parameter to match the mobile phone side.
After the mobile phone is matched, we can define the corresponding rules and forward the request of the mobile phone to the www.ilanni.com address.
Haproxy specific configuration files are as follows:
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 1024x768 weight 3 check Inter rise 2 Fall 3
In the above configuration file, the following two lines need to be noted:
ACL UA Hdr_sub (user-agent)-I android iphone
REDIRECT Prefix http://www.ilanni.com if UA
In these two lines, the first line is the first UA rule, and the rule is to determine if it is the phone side.
Note: On this phone side, we only match the Android phone and iphone.
The second line is the jump rule, if the match is the phone side, then jump directly to http://www.ilanni.com this address.
If it is the PC side, the default jump to httpserver this back-end server group.
The above configuration is a server to provide only one domain name access requests, if there are two domain names, it is necessary to configure the following:
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 1024x768 weight 3 check Inter rise 2 Fall 3
Backend Haproxyserver
Balance Source
Server Web1 127.0.0.1:7070 maxconn 1024x768 weight 3 check Inter rise 2 Fall 3
2.2
test forwards all phone requests
Now let's test the jump function as follows:
By testing you will find that entering http.ilanni.com in your mobile browser will automatically jump to the address of http://www.ilanni.com.
2.3
forwarding configuration based on URL
According to the URL of the request of the mobile phone to forward, the first is to match the phone side, and then define the URL path rules. Finally, the combination of mobile phone and URL path rules, to jump.
Haproxy specific configuration files, as follows:
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 1024x768 weight 3 check Inter rise 2 Fall 3
In the above configuration file, the following lines are required for interpretation.
ACL Is_docs Url_beg/docs/manager
Defines a is_docs rule. If the URL starts with/docs or/manager, it is all part of the rule.
ACL UA Hdr_sub (user-agent)-I android iphone
REDIRECT Prefix http://www.ilanni.com if UA!is_docs
These two lines are first to match the phone side, and then if it is mobile phone access, and access is not the Is_docs rules, then jump directly to http://www.ilanni.com this address.
Use_backend Httpserver If UA Is_docs
This command is to jump directly to the Httpserver back-end server group if it is a mobile-side access and is accessing the Is_docs rule.
If it is the PC side, the default jump to httpserver this back-end server group.
2.4
testing is forwarded based on URL
Once configured based on URL forwarding, we will now test. As follows:
Through, we can see the mobile phone side access http://http.ilanni.com/docs/This connection, is can be directly accessed.
Third, other haproxy configuration
In the front we explained the relevant configuration of the mobile phone, in the actual production environment, sometimes we will encounter some bizarre requirements.
Requires all mobile phone-side access to the http.ilanni.com to go to the specified page.
Haproxy main configuration files are as follows:
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 1024x768 weight 3 check Inter rise 2 Fall 3
The above configuration is all mobile phone-side access, all jump to http://www.ilanni.com/?p=10624 this page. The test is as follows:
Through, we can see that the phone-side access does jump to the page we specify.
Similar to such requirements, generally in the upgrade of the company's related business, the company's public IP can be normal, but external access, jump to the specified maintenance page.
This we can match according to the source IP address, here does not carry on the detailed explanation.
Through, we can see the mobile phone side access Http://http.ilanni.com/manager/status This connection, is can be directly accessed.
We can see that if the phone is not accessing the URL that is defined in the Is_docs rule, it will all jump to the address of http://www.ilanni.com.
Slime: Haproxy learning phone rule matching