顯示具有 電腦-Linux-Server-Haproxy 標籤的文章。 顯示所有文章
顯示具有 電腦-Linux-Server-Haproxy 標籤的文章。 顯示所有文章

2018年11月28日 星期三

電腦-Linux-Haproxy 設定檔(http/https)

電腦-Linux-Haproxy 設定檔(http/https)

Haproxy安裝ssl憑證,後端server使用http。
當使用者使用http會自動轉為https,根據url指向定義的server。

global
    log         127.0.0.1 local0
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon
    tune.ssl.default-dh-param 2048
    ssl-default-bind-ciphers DES:RC4:AES256-SHA:AES128-SHA:DES-CBC3-SHA:!ADH-RC4-MD5:!ECDHE-RSA-RC4-SHA:!AECDH-RC4-SHA

defaults
    mode                            http
    log                                global
    option                           httplog
    option                           dontlognull
    retries                           3
    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 stats
    bind 0.0.0.0:8181
    stats enable
    stats hide-version
    stats uri /admin
    stats auth admin:admin
    stats admin if TRUE
    stats refresh 30s

#http轉https
frontend  http
bind *:80
redirect scheme https code 301 if !{ ssl_fc }

#https
frontend https_switch
    bind *:443 ssl crt /etc/pki/CA/CA.pem
    mode http
    option httpclose
    option forwardfor
    reqadd X-Forwarded-Proto:\ https

#ACL
    acl url_testsite ssl_fc_sni testsite.com
    use_backend url_testsite if url_testsite
    acl url_portal ssl_fc_sni -i portal.com
    use_backend url_portal if url_portal

#log-format
    capture request header Host len 64
    capture request header User-Agent len 128
    capture request header X-Forwarded-For len 100
    capture request header Referer len 200
    capture response header Server len 40
    capture response header Server-ID len 40
    log-format %ci:%cp\ %si:%sp\ %B\ %U\ %ST\ %r\ %b\ %f\ %bi\ %hrl\ %hsl\


backend url_testsite
    mode    http
    balance roundrobin
    option  httpclose
    option  forwardfor
    cookie  SRVERID insert nocache
    server  testserver1 192.168.11.11:80 check inter 3000 rise 3 fall 5
    server  testserver2 192.168.11.12:80 check inter 3000 rise 3 fall 5 backup
    #backup為備援參數,當testserver1異常才會使用

backend url_portal
    mode    http
    balance roundrobin
    option  httpclose
    option  forwardfor
    cookie  SRVERID insert nocache
    server  webserver1 192.168.11.21:80 check inter 3000 rise 3 fall 5
    server  webserver2 192.168.11.22:80 check inter 3000 rise 3 fall 5 backup


參考資料
haproxy生产环境的一个完整配置

電腦-Linux-Haproxy 日誌(Log)設定

電腦-Linux-Haproxy 日誌(Log)設定

使用rsyslog,啟用udp來接受資料
vim /etc/rsyslog.d/haproxy.conf

$ModLoad imudp
$UDPServerAddress 127.0.0.1
$UDPServerRun 514
local0.*     /var/log/haproxy.log
&~
重啟rsyslog服務
systemctl status rsyslog
檢查udp 514
netstat -tunpl

設定日誌
vim /etc/haproxy/haproxy.cfg
在global增加log設定,可自行定義等級
    log         127.0.0.1 local0
在defaults 
defaults
    mode                   http
    log                       global
    option                  httplog
    option                  dontlognull

自定義日誌在frontend 設定
frontend  http
bind *:80

capture request header Host len 64
capture request header User-Agent len 128
capture request header X-Forwarded-For len 100
capture request header Referer len 200
capture response header Server len 40
capture response header Server-ID len 40
log-format %ci:%cp\ %si:%sp\ %B\ %U\ %ST\ %r\ %b\ %f\ %bi\ %hrl\ %hsl\



frontend sql_server

bind *:1433
mode tcp
option tcplog
capture request header Host len 64
capture request header User-Agent len 128
capture request header X-Forwarded-For len 100
capture request header Referer len 200
capture response header Server len 40
capture response header Server-ID len 40
log-format "%ci:%cp [%t] %ft %b/%s %Tw/%Tc/%Tt %B %ts %ac/%fc/%bc/%sc/%rc %sq/%bq"



Haproxy的日志格式
* the default HTTP format is defined this way : *
log-format %ci:%cp\ [%t]\ %ft\ %b/%s\ %Tq/%Tw/%Tc/%Tr/%Tt\ %ST\ %B\ %CC\ %CS\ %tsc\ %ac/%fc/%bc/%sc/%rc\ %sq/%bq\ %hr\ %hs\ %{+Q}r

* the default CLF format is defined this way : *
log-format %{+Q}o\ %{-Q}ci\ -\ -\ [%T]\ %r\ %ST\ %B\ \"\"\ \"\"\ %cp\ %ms\ %ft\ %b\ %s\ \%Tq\ %Tw\ %Tc\ %Tr\ %Tt\ %tsc\ %ac\ %fc\ %bc\ %sc\ %rc\ %sq\ %bq\ %CC\ %CS\ \%hrl\ %hsl

* the default TCP format is defined this way : *
log-format %ci:%cp\ [%t]\ %ft\ %b/%s\ %Tw/%Tc/%Tt\ %B\ %ts\ %ac/%fc/%bc/%sc/%rc\ %sq/%bq

HTTP/TCP日志格式
log-format %ci:%cp\ %ft\ %b/%s\ %Tw/%Tc/%Tt\ %ST\ %B\ %fc/%sc%rc\ %sq/%bq

參考資料
Capturing HTTP headers 原廠說明
自定義日誌原廠說明
haproxy配置日志及自定义日志
haproxy日志设置
HAProxy日志配置详解
配置Haproxy增加日志记录功能

電腦-Linux-Haproxy安裝(CentOS)

電腦-Linux-Haproxy on CentOS

1.主機環境
CentOS 7 最小安裝
yum install make wget gcc openssl-devel pcre-devel zlib-devel

2.Haproxy安裝
下載程式
wget http://www.haproxy.org/download/1.8/src/haproxy-1.8.8.tar.gz

編譯、安裝
tar zxvpf haproxy-1.8.8.tar.gz
cd haproxy-1.8.8
make TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1
make install

驗證
/usr/local/sbin/haproxy -v

建立相關環境
useradd -r haproxy
mkdir -p /etc/haproxy
mkdir -p /run/haproxy
mkdir -p /var/lib/haproxy
touch /var/lib/haproxy/stats

建立執行檔連結
ln -s /usr/local/sbin/haproxy /usr/sbin/haproxy


設定啟動服務
cp haproxy-1.8.8/examples/haproxy.init /etc/init.d/haproxy
chmod 755 /etc/init.d/haproxy
systemctl daemon-reload
service haproxy start

啟動haproxy
systemctl start haproxy.service
停止haproxy
systemctl stop haproxy.service
重新啟動
systemctl restart haproxy.service
狀態
systemctl status haproxy.service

編輯設定檔
vim haproxy.cfg
驗證設定檔
haproxy -f /etc/haproxy/haproxy.cfg -c

參考資料: