Haproxy官方文檔翻譯(第二章)配置Haproxy 附英文原文

2.配置 HAProxygit

2.1 配置文件格式正則表達式

Haproxy的配置過程包含了3部分的參數資源:
- 命令行中的參數,此種參數老是享有優先權被使用
- 配置文件中global節點中的參數,此種參數是進程範圍參數
- 代理節點參數,此種參數是從defaults,listen,frontend,backend節點中讀取的shell

這個手冊裏,以關鍵字起始並做爲關聯引用的行,組成了配置文件的語法結構。後面跟上可選的一個或者幾個用空格隔開的參數。express

2.2 引用(Quoting)和轉義(escaping)編程

Haproxy的配置介紹了一個像大多數編程語言同樣的引用和轉義的系統。配置文件支持3種類型:用一個反斜槓(\),弱引用用雙引號,
強引用用單引號。api

若是字符串中必需要加入空格,必需要在它們前面加上反斜槓('\')來轉義它們或者用引號引發來。反斜槓也必須用雙反斜槓或者強引用來轉義它們。服務器

咱們在一個特殊字符前面加上反斜槓('\')來表達一個轉義:
\ 用來表示空格,以區分一個分隔符
\# 用來表示一個#號,以區分註釋符號
\\ 表示一個反斜槓
\' 表示一個單引號,以區分強引用
\" 表示一個雙引號,以區分弱引用less

弱引用使用雙引號來實現("")。用來阻止解釋語義(使特殊字符變爲普通字符,好比讓如下特殊字符所表示的特殊意義失效):
空格字符表示參數的分隔符
‘ 單引號表示強引用的分隔符
# #表示註釋的開始frontend

弱引用容許對變量進行語法解釋,若是你想在一個雙引號的變量中用一個非解釋(non-interpreted)美圓符號,你應該用一個反斜槓去
轉義它('\$'),注意,在弱引用外這樣作是不起效果的。編程語言

弱引用不會阻止轉義解釋和特殊字符的。

強引用用單引號來實現('')。在單引號內,不會解釋任何東西,這對用來引用正則表達式效果是極佳的。

被引用起來的和被轉義的字符串在內存中被替換成它們所表示的內容,它容許你處理與之有關的一系列的事情。

示例:
# 這些是表達式:
log-format %{+Q}o\ %t\ %s\ %{-Q}r
log-format "%{+Q}o %t %s %{-Q}r"
log-format '%{+Q}o %t %s %{-Q}r'
log-format "%{+Q}o %t"' %s %{-Q}r'
log-format "%{+Q}o %t"' %s'\ %{-Q}r

# 這些是表達式:
reqrep "^([^\ :]*)\ /static/(.*)" \1\ /\2
reqrep "^([^ :]*)\ /static/(.*)" '\1 /\2'
reqrep "^([^ :]*)\ /static/(.*)" "\1 /\2"
reqrep "^([^ :]*)\ /static/(.*)" "\1\ /\2"

2.3 環境變量

Haproxy的配置支持環境變量。這些變量只能在雙引號中被解釋。在解析配置文件的時候,變量也被解析。變量必須以$開頭,也能夠像在Bourne shell裏同樣在後面跟上花括號(「{}」)。
變量名能夠包含字母,數字或者下劃線「_」,但不能以數字開頭.

示例:
bind "fd@${FD_APP1}"

log "${LOCAL_SYSLOG}:514" local0 notice # send to local server

user "$HAPROXY_USER"

在進程啓動階段,定義裏一個特殊的變量$HAPROXY_LOCALPEER,它包含了local peer的名字。(詳情請在管理指南章節查看"- L")。

2.4 時間格式

有些變量包含了如今的時間值,好比timeouts.這些值通常都用毫秒(除非其餘的一些精確的狀態)表示,若是要用其餘單位,須要用數字加上單位。
認知到這一點很重要,由於不是每一個關鍵詞都會被重複。支持的單位有:

- us : 微秒(microseconds). 1 微秒 = 1/1000000 秒
- ms : 毫秒(milliseconds). 1 毫秒 = 1/1000 秒. 默認以毫秒爲單位.
- s : 秒(seconds). 1s = 1000ms
- m : 分鐘(minutes). 1m = 60s = 60000ms
- h : 小時(hours). 1h = 60m = 3600s = 3600000ms
- d : 天(days). 1d = 24h = 1440m = 86400s = 86400000ms

2.5示例

#簡單配置一個HTTP代理,用來監聽80端口上的全部接口和端口轉發請求,與之對應的是一個單個的後臺「servers」,
#這個後臺配置了一個監聽127.0.0.1的服務器「server1」。
global
daemon
maxconn 256

defaults
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms

frontend http-in
bind *:80
default_backend servers

backend servers
server server1 127.0.0.1:8000 maxconn 32


# 下面的示例效果同樣,是用一個單獨的listen代碼塊,尤爲是在HTTP模式下這樣作更簡介更有效。

global
daemon
maxconn 256

defaults
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms

listen http-in
bind *:80
server server1 127.0.0.1:8000 maxconn 32


假如haproxy已經在$PATH環境變量中,在shell終端中這樣來測試這些配置:

$ sudo haproxy -f configuration.conf -c


-------------------------------------------如下是英文原文--------------------------------------------------

2. Configuring HAProxy

2.1. Configuration file format
HAProxy's configuration process involves 3 major sources of parameters :

- the arguments from the command-line, which always take precedence
- the "global" section, which sets process-wide parameters
- the proxies sections which can take form of "defaults", "listen",
"frontend" and "backend".

The configuration file syntax consists in lines beginning with a keyword
referenced in this manual, optionally followed by one or several parameters
delimited by spaces.
2.2. Quoting and escaping
HAProxy's configuration introduces a quoting and escaping system similar to
many programming languages. The configuration file supports 3 types: escaping
with a backslash, weak quoting with double quotes, and strong quoting with
single quotes.

If spaces have to be entered in strings, then they must be escaped by preceding
them by a backslash ('\') or by quoting them. Backslashes also have to be
escaped by doubling or strong quoting them.

Escaping is achieved by preceding a special character by a backslash ('\'):

\ to mark a space and differentiate it from a delimiter
\# to mark a hash and differentiate it from a comment
\\ to use a backslash
\' to use a single quote and differentiate it from strong quoting
\" to use a double quote and differentiate it from weak quoting

Weak quoting is achieved by using double quotes (""). Weak quoting prevents
the interpretation of:

space as a parameter separator
' single quote as a strong quoting delimiter
# hash as a comment start

Weak quoting permits the interpretation of variables, if you want to use a non
-interpreted dollar within a double quoted string, you should escape it with a
backslash ("\$"), it does not work outside weak quoting.

Interpretation of escaping and special characters are not prevented by weak
quoting.

Strong quoting is achieved by using single quotes (''). Inside single quotes,
nothing is interpreted, it's the efficient way to quote regexes.

Quoted and escaped strings are replaced in memory by their interpreted
equivalent, it allows you to perform concatenation.
Example:
# those are equivalents:
log-format %{+Q}o\ %t\ %s\ %{-Q}r
log-format "%{+Q}o %t %s %{-Q}r"
log-format '%{+Q}o %t %s %{-Q}r'
log-format "%{+Q}o %t"' %s %{-Q}r'
log-format "%{+Q}o %t"' %s'\ %{-Q}r

# those are equivalents:
reqrep "^([^\ :]*)\ /static/(.*)" \1\ /\2
reqrep "^([^ :]*)\ /static/(.*)" '\1 /\2'
reqrep "^([^ :]*)\ /static/(.*)" "\1 /\2"
reqrep "^([^ :]*)\ /static/(.*)" "\1\ /\2"
2.3. Environment variables
HAProxy's configuration supports environment variables. Those variables are
interpreted only within double quotes. Variables are expanded during the
configuration parsing. Variable names must be preceded by a dollar ("$") and
optionally enclosed with braces ("{}") similarly to what is done in Bourne
shell. Variable names can contain alphanumerical characters or the character
underscore ("_") but should not start with a digit.
Example:
bind "fd@${FD_APP1}"

log "${LOCAL_SYSLOG}:514" local0 notice # send to local server

user "$HAPROXY_USER"
A special variable $HAPROXY_LOCALPEER is defined at the startup of the process
which contains the name of the local peer. (See "-L" in the management guide.)
2.4. Time format
Some parameters involve values representing time, such as timeouts. These
values are generally expressed in milliseconds (unless explicitly stated
otherwise) but may be expressed in any other unit by suffixing the unit to the
numeric value. It is important to consider this because it will not be repeated
for every keyword. Supported units are :

- us : microseconds. 1 microsecond = 1/1000000 second
- ms : milliseconds. 1 millisecond = 1/1000 second. This is the default.
- s : seconds. 1s = 1000ms
- m : minutes. 1m = 60s = 60000ms
- h : hours. 1h = 60m = 3600s = 3600000ms
- d : days. 1d = 24h = 1440m = 86400s = 86400000ms
2.5. Examples
# Simple configuration for an HTTP proxy listening on port 80 on all
# interfaces and forwarding requests to a single backend "servers" with a
# single server "server1" listening on 127.0.0.1:8000
global
daemon
maxconn 256

defaults
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms

frontend http-in
bind *:80
default_backend servers

backend servers
server server1 127.0.0.1:8000 maxconn 32


# The same configuration defined with a single listen block. Shorter but
# less expressive, especially in HTTP mode.
global
daemon
maxconn 256

defaults
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms

listen http-in
bind *:80
server server1 127.0.0.1:8000 maxconn 32


Assuming haproxy is in $PATH, test these configurations in a shell with:

$ sudo haproxy -f configuration.conf -c

相關文章
相關標籤/搜索