Configuration details:
HAPrpxy configuration file: / etc/haproxy/haproxy.cfg
It consists of two parts: global and proxies.
- global: global configuration segment
Process and security configuration-related parameters Performance tuning related parameters Debug parameter
- proxies: Proxy configuration segment with multiple subsegments
defaults provides default configuration for frontend, backend, listen listen has both front-end and back-end configurations # Not commonly used below frontend front end, equivalent to server {} in nginx Backend backend, equivalent to upstream {} in nginx
global configuration parameters:
Official Document: https://cbonte.github.io/haproxy-dconv/2.0/intro.html
global #Lock the Running Directory chroot /usr/local/haproxy #Define global syslog servers; up to two can be defined log 127.0.0.1 local3 info #Specify the pid file path pidfile /var/run/haproxy.pid #Running as a daemon deamon #Maximum number of concurrent connections per haproxy process maxconn 4000 #User Identity Running haproxy user haproxy group haproxy #perhaps uid 99 gid 99 /********************Options******************************************/ #Number of haproxy processes open, consistent with CPU nbproc 4 #CPU binding cpu-map 1 0 cpu-map 2 1 cpu-map 3 2 cpu-map 4 3 #Specifies the number of threads open for each haproxy process, defaulting to one thread per process nbthread 1 #Socket file 1, permission of mode file, level level level, use when processing multi-process, bind socket file stats socket /var/lib/haproxy/haproxy.sock1 mode 600 level admin process 1 #socket file 2 stats socket /var/lib/haproxy/haproxy.sock2 mode 600 level admin process 2 #Maximum number of connections per ssl for each haproxy process,Be used for haproxy In scenarios where certificates are configured maxsslconn #Maximum number of connections per process created per second maxconnrate 200 #Back-end server status check is randomly advanced or delayed percentage time, recommendation2-5(20%-50%)Between spread-checks 3
defaults configuration:
Provide default configuration for frontend, backend, listen
defaults #Default type of work mode http #Define logs, global represents global definitions log global #Open session with client option http-keep-alive #Pass-through client real IP to back-end web server option forwardfor #When the server Id corresponds to the server hangs up, force the server to be directed to other healthy servers option redispatch #Automatically terminate links that have been processed by the current queue for a long time when the server load is high option abortonclose #Maximum connection waiting time for client requests to back-end server (before TCP) timeout connect 120s #Timeout time-out of client requests to back-end server (after TCP) timeout server 600s #Maximum inactivity time with client timeout client 600s #session sessions are timed out and forwarded to the same back-end server in scope timeout http-keep-alive 120s #Detection timeout for back-end servers timeout check 5s
listen:
Replace the configuration of frontend and backend with listen:
listen WEB_PORT_80 bind 192.168.7.102:80 mode http balance <algorithm> option forwardfor server web1 192.168.7.101:80 check inter 3000 fall 3 rise 5 server web2 192.168.7.101:80 check inter 3000 fall 3 rise 5
frontend configuration
Front end, equivalent to server {} in nginx
#Specify HAProxy's listening address to listen on multiple IP or ports at the same time bind [<address>]:<port_range> [, ...] [param*] frontend WEB_PORT bind :80,:8080 bind 192.168.7.102:10080,:8801-8810,192.168.7.101:9001-9010 mode http/tcp #Specify load protocol type use_backend backend_name #The name of the back-end server group invoked
backend configuration
Define a set of back-end servers that will be called from content.
#Specify load protocol type mode http/tcp #configuration option option #Define the back-end real server server <name> <ip:port> #The default is 1, and the maximum value is 256, 0, which means not participating in load balancing. weight #Mark the back-end server as a backup state backup #Mark the back-end server as unavailable disabled #Temporarily redirect requests to other URL s, only for http mode redirect prefix http://www.magedu.net/ #Maximum number of concurrent connections for back-end server s maxconn <maxconn> #Backup queue length after server's connection number reaches the upper limit backlog <backlog> #Check the health status of the specified real, which is not turned on by default check #Designable health monitoring IP addr IP #Designated health monitoring port port num #Health check interval, default 2000 ms inter num #Back-end server failure checks, default 3 fall num #Back-end server restores checks from offline by default of 2 rise num
frontend+backend configuration example:
frontend WEB_PORT_80 bind 192.168.7.248:80 mode http use_backend web_prot_http_nodes backend web_prot_http_nodes mode http option forwardfor server 192.168.7.101 192.168.7.101:8080 check inter 3000 fall 3 rise 5 server 192.168.7.102 192.168.7.102:8080 check inter 3000 fall 3 rise 5