InfraLab
HAPROXY40 cmd

HAProxy チート集

HAProxy の global/defaults、frontend/backend、負荷分散、ヘルスチェック、TLS 終端、ACL 分岐、stats、Runtime API をまとめた運用チート集。

更新日
2026-05-29
参照範囲
公式ドキュメント / man page / 主要ベンダーCLI
対象実装
主要 Linux / BSD / ネットワーク機器 CLI の一般的な実装
免責
OS とバージョン差分は実環境で確認してください。

このチートシートの使いどころ

HAProxy チートシートは、global/defaults、frontend/backend、負荷分散、ヘルスチェック、TLS 終端、ACL 分岐、stats、Runtime API を整理した一覧です。対象は現行 HAProxy の一般的な運用設定です。L7/L4 の切り分け、backend 障害、証明書更新、リロード前検証に使います。

40 / 40

global maxconn

プロセス全体の最大接続数を設定する。

Example
global log /dev/log local0 maxconn 20000
global stats socket

Runtime API 用 UNIX ソケットを有効化する。

Example
global stats socket /var/run/haproxy.sock mode 660 level admin expose-fd listeners
defaults mode http

HTTP 用の既定値を定義する。

Example
defaults mode http option httplog timeout connect 5s timeout client 30s timeout server 30s
option dontlognull

空接続をログに出さない。

Example
defaults option dontlognull
option redispatch

接続失敗時に別サーバへ再割当する。

Example
defaults option redispatch retries 3
frontend http-in

HTTP 入口を定義する。

Example
frontend http-in bind *:80 default_backend web_back
frontend https-in

HTTPS 入口を定義する。

Example
frontend https-in bind *:443 ssl crt /etc/haproxy/cert.pem alpn h2,http/1.1 default_backend web_back
http-request redirect scheme https

HTTP を HTTPS へリダイレクトする。

Example
frontend http-in bind *:80 http-request redirect scheme https code 301 unless { ssl_fc }
http-request set-header X-Forwarded-Proto

上流へ元スキームを渡す。

Example
http-request set-header X-Forwarded-Proto https if { ssl_fc }
capture request header Host

Host ヘッダをログ用にキャプチャする。

Example
capture request header Host len 64
backend web_back

バックエンドサーバ群を定義する。

Example
backend web_back server web1 10.0.1.10:8080 check server web2 10.0.1.11:8080 check
server check fall rise

ヘルスチェックの失敗/復帰回数を指定する。

Example
server web1 10.0.1.10:8080 check inter 2s fall 3 rise 2
backup server

通常時は使わないバックアップサーバを置く。

Example
server web-dr 10.0.9.10:8080 check backup
cookie persistence

Cookie ベースのセッション固定を行う。

Example
backend app cookie SRV insert indirect nocache server app1 10.0.1.10:8080 check cookie a1
balance roundrobin

ラウンドロビンで均等に振り分ける。

Example
backend web_back balance roundrobin
balance leastconn

接続数が少ないサーバへ振り分ける。

Example
backend api_back balance leastconn
balance source

送信元 IP ハッシュで固定化する。

Example
backend session_back balance source
balance uri

URI ハッシュでキャッシュサーバへ振り分ける。

Example
backend cache_back balance uri
balance first

利用可能な最初のサーバを優先する。

Example
backend queue_back balance first
option httpchk GET /health

HTTP ヘルスチェックを設定する。

Example
backend web_back option httpchk GET /health
http-check expect status 200

ヘルスチェックの期待ステータスを指定する。

Example
http-check expect status 200
agent-check

agent ポートから重みや状態を受け取る。

Example
server app1 10.0.1.10:8080 check agent-check agent-port 9000
bind ssl crt

TLS 終端で証明書を指定する。

Example
bind *:443 ssl crt /etc/haproxy/cert.pem
bind alpn h2,http/1.1

ALPN で HTTP/2 と HTTP/1.1 を提示する。

Example
bind *:443 ssl crt /etc/haproxy/cert.pem alpn h2,http/1.1
ssl-default-bind-options

TLS の既定オプションを強化する。

Example
global ssl-default-bind-options ssl-min-ver TLSv1.2 no-tls-tickets
crt-list

複数証明書を crt-list で管理する。

Example
bind *:443 ssl crt-list /etc/haproxy/crt-list.txt alpn h2,http/1.1
acl is_api path_beg /api

パス接頭辞 ACL を定義する。

Example
acl is_api path_beg /api
use_backend api_back if is_api

ACL に一致したら別 backend へ送る。

Example
use_backend api_back if is_api
acl host_static hdr(host)

Host ヘッダで分岐する。

Example
acl host_static hdr(host) -i static.example.com use_backend static_back if host_static
acl bad_bot hdr_sub

User-Agent で拒否する。

Example
acl bad_bot hdr_sub(user-agent) -i badbot http-request deny if bad_bot
stick-table rate limit

送信元 IP ごとにリクエストレートを制限する。

Example
frontend https-in stick-table type ip size 100k expire 10m store http_req_rate(10s) http-request track-sc0 src http-request deny if { sc_http_req_rate(0) gt 100 }
stats enable

統計ページを有効化する。

Example
listen stats bind 127.0.0.1:8404 stats enable stats uri /stats
haproxy -c

設定ファイルを検証する。

Example
haproxy -c -f /etc/haproxy/haproxy.cfg
systemctl reload haproxy

接続を保ちながら HAProxy を reload する。

Example
sudo systemctl reload haproxy
show stat

Runtime API で統計を取得する。

Example
echo "show stat" | sudo socat - /var/run/haproxy.sock
show info

Runtime API でプロセス情報を確認する。

Example
echo "show info" | sudo socat - /var/run/haproxy.sock
disable server

特定サーバを drain/停止扱いにする。

Example
echo "disable server web_back/web1" | sudo socat - /var/run/haproxy.sock
enable server

無効化したサーバを戻す。

Example
echo "enable server web_back/web1" | sudo socat - /var/run/haproxy.sock
set server weight

Runtime API で重みを変更する。

Example
echo "set server web_back/web1 weight 20" | sudo socat - /var/run/haproxy.sock
show table

stick-table の中身を確認する。

Example
echo "show table https-in" | sudo socat - /var/run/haproxy.sock
対応ツール
Related