天天看點

HAProxy 使用進階

HAProxy 使用進階

================================================================================

概述:

  本章将繼續介紹HAProxy中代理配置段的配置的相關參數,内容如下:

  • server 在backend中定義一個主機;
  • 基于http協定作7層健康狀态檢測機制;
  • cookie,啟用基于cookie的會話黏性;
  • default_backend 為前端設定預設的後端主機;
  • log:日志相關的參數;
  • 錯誤頁面自定義:
  • 修改請求或響應封包首部相關;
  • 逾時時長:

HAProxy配置參數---代理配置段:

 4.server:用于在backend中定義一個主機;

★格式:
  • server <name> <address>[:[port]] [param*]
⊙<name> 
  • is the internal name assigned to this server. This name will appear in logs and alerts.
⊙<address> 
  • is the IPv4 or IPv6 address of the server

⊙<param*>:參數

◆weight <weight>:

  • 目前server的權重;

◆check:

 對目前server進行健康狀态檢測;

  • inter <delay>:      時間間隔;
  • rise <count>:       判定為“健康”狀态需要檢測的次數,預設2;
  • fall <count>:        判定為“不健康”狀态需要檢測的次數,預設3;
  • addr <ipv4|ipv6>:健康狀态檢測時使用的位址;
  • port <port>:        健康狀态檢測時使用的端口;
注意:
  • 預設為傳輸層檢測,即探測端口是否能響應;需要執行應用層檢測,則需要httpchk, smtpchk, mysql-check, pgsql-check, ssl-hello-chk; 
◆cookie <value>:
  • 為目前server指定其cookie值,此值會在收到請求封包時進行檢測,其功能在于實作基于cookie會話保持;
◆disabled:
  • 将主機标記為不可用;
◆maxconn <maxconn>:
  • 目前server的最大并發連接配接數;
◆maxqueue <maxqueue>:
  • 目前server的等待隊列的最大長度;超出此長度的所有配置給伺服器的連接配接将會被丢棄;
◆redir <prefix>:
  • 将發往目前server的所有請求GET和HEAD類的請求均重定向至指定的URL;

示範:

 1.為後端主機指明權重,最大并發連接配接數,cookie等參數,編輯配置檔案/etc/haproxy/haprosy.crg,如下:

HAProxy 使用進階

 使用curl請求haproxy可以發現以權重輪詢的方式排程後端主機,如下:

# 沒有定義後端主機權重之前以輪詢的方式響應
[root@centos7 ~]# for i in {0..9};do curl http://10.1.252.153/index.html;done
<h1>Backend Server 1</h1>
<h1>Backend Server 2</h1>
<h1>Backend Server 1</h1>
<h1>Backend Server 2</h1>
<h1>Backend Server 1</h1>
<h1>Backend Server 2</h1>
<h1>Backend Server 1</h1>
<h1>Backend Server 2</h1>
<h1>Backend Server 1</h1>
<h1>Backend Server 2</h1>

# 定義主機權重之後,可以看到權重大的響應次數多
[root@centos7 ~]# for i in {0..9};do curl http://10.1.252.153/index.html;done
<h1>Backend Server 1</h1>
<h1>Backend Server 1</h1>
<h1>Backend Server 2</h1>
<h1>Backend Server 1</h1>
<h1>Backend Server 1</h1>
<h1>Backend Server 2</h1>
<h1>Backend Server 1</h1>
<h1>Backend Server 1</h1>
<h1>Backend Server 2</h1>
<h1>Backend Server 1</h1>      

 5.option httpchk

★文法:
  • option httpchk
  • option httpchk <uri>
  • option httpchk <method> <uri>
  • option httpchk <method> <uri> <version>
⊙作用:
  • 基于http協定作7層(應用層)健康狀态檢測機制;
⊙請求封包的起始行:
  • <method> <uri> <version>
Examples :
# Relay HTTPS traffic to Apache instance and check service availability
# using HTTP request "OPTIONS * HTTP/1.1" on port 80.backend https_relay
    mode tcp
    option httpchk OPTIONS * HTTP/1.1\r\nHost:\ www
    server apache1 192.168.1.1:443 check port 80      
★http-check expect [!] <match> <pattern>:
  • 定義做健康狀态檢測後傳回的響應碼
⊙格式:
  • <match>:status(響應嗎)或string; 
  • <pattern>:比對期望的響應結果的模式;
# only accept status 200 as valid
  http-check expect status 200      

 6.cookie:

★cookie:
  • 啟用基于cookie的會話黏性,要結合server指定的cookie參數一起實作;
HAProxy 使用進階
cookie JSESSIONID prefix
cookie SRV insert indirect nocache  //常用方法
cookie SRV insert postonly indirect      

  1.基于浏覽器的使用者會話綁通路,對于haproxy伺服器來說,第一次排程到某一主機,之後也要排程到這一主機,編輯配置檔案,如下:

HAProxy 使用進階

 2.在浏覽器中通路可以發現在響應封包中的 Set-Cookie: WEBSRV=web1; path=/,插入cookie,之後再次使用此浏覽器請求,就會攜帶插入的cookie資訊,haproxy根據cookie 發往同一個後端主機,如下:

HAProxy 使用進階

 再次重新整理之後,可以看到請求封包中攜帶這的cookie資訊

HAProxy 使用進階

------------------------------------------------------------------------------------------------

 7.default_backend <backend>:

★default_backend <backend>:
  • 為前端設定預設的後端主機

⊙範圍:frontend和default

Example :

use_backend     dynamic  if  url_dyn   //如果請求動态内容,就使用dynamic主機
use_backend     static   if  url_css url_img extension_img  //如果請求靜态内容,就使用static主機
default_backend dynamic  //剩餘的使用預設的dynamic主機      

 8.log:日志相關的參數

★log:
  • 為frontend或backend定義日志記錄機制;
  • log global
  • log <address> [len <length>] <facility> [<level> [<minlevel>]]
  • no log 
log global
log 127.0.0.1:514 local0 notice         # only send important events
log 127.0.0.1:514 local0 notice notice  # same but limit output level
log ${LOCAL_SYSLOG}:514 local0 notice   # send to local server      
★capture request header <name> len <length>
  • 記錄請求封包中的指定的首部的值于日志中;len用于指定要記錄的資訊的長度;

⊙範圍:frontend和listen

Example:

capture request header Host len 15
capture request header X-Forwarded-For len 15
capture request header Referer len 15  # 從哪裡跳轉而來      
★capture response header <name> len <length>
  • 記錄響應封包中的指定的首部的值于日志中;len用于指定要記錄的資訊的長度;
capture response header Content-length len 9
capture response header Location len 15      

 9.錯誤頁面自定義:

★errorfile <code> <file>
  • 傳回由haproxy自定義的錯誤頁面
⊙參數:
  • <code> :is the HTTP status code. Currently, HAProxy is capable of generating codes 200, 400, 403, 408, 500, 502, 503, and 504.
  • <file> :designates a file containing the full HTTP response. 
errorfile 400 /etc/haproxy/errorfiles/400badreq.http
errorfile 408 /dev/null  # workaround Chrome pre-connect bug
errorfile 403 /etc/haproxy/errorfiles/403forbid.http
errorfile 503 /etc/haproxy/errorfiles/503sorry.http      

★errorloc <code> <url>

    errorloc302 <code> <url>

  • 由haproxy服務傳回一個HTTP重定向的URL;

示範1:

  1.自定義haproxy的錯誤傳回頁面,編輯配置檔案在frontend中定義,如下:

HAProxy 使用進階

 2.建立指定的目錄和檔案内容,然後重載haproxy服務如下:

[root@centos7 haproxy]# mkdir  /etc/haproxy/errorfiles
[root@centos7 haproxy]# echo "<h1>Something Wrong</h1>" > /etc/haproxy/errorfiles/503sorry.http
[root@centos7 haproxy]# cat /etc/haproxy/errorfiles/503sorry.http
<h1>Something Wrong</h1>

[root@centos7 haproxy]# systemctl reload haproxy.service      

 3.停掉後端兩台web伺服器,在浏覽器中通路,發現錯誤頁面為我們自定義的内容,如下:

HAProxy 使用進階

-----------------------------------------------------------------------------------------

示範2:

 1.定義由haproxy伺服器傳回一個HTTP重定向的URL,編輯配置檔案如下:

HAProxy 使用進階

 2.在本機httpd服務的根目錄中/var/www/html中建立指定的url,并啟動本機httpd服務

 【注意:為了不和haproxy監聽的端口起沖突,這裡我修改httpd監聽的端口為8088】如下:

[root@centos7 ~]# echo "<h1>A URL from other server</h1>" >  /var/www/html/errorpagetest.html
[root@centos7 ~]# cat /var/www/html/errorpagetest.html
<h1>A URL from other server</h1>

[root@centos7 ~]# systemctl start httpd
[root@centos7 ~]# ss -tnl
State       Recv-Q Send-Q   Local Address:Port                  Peer Address:Port              
LISTEN      0      25                   *:514                              *:*                  
LISTEN      0      128                  *:80                               *:*                  
LISTEN      0      128                  *:22                               *:*                  
LISTEN      0      25                  :::514                             :::*                  
LISTEN      0      128                 :::22                              :::*                  
LISTEN      0      128                ::1:631                             :::*                  
LISTEN      0      128                 :::8088                            :::*                  
LISTEN      0      100                ::1:25                              :::*      

 3.重載haprosy服務,停掉後端兩台web伺服器,在浏覽器中通路,發現錯誤頁面為我們自定義重定向的URL的内容,如下:

HAProxy 使用進階

繼續閱讀