Detailed haproxy configuration

Parse the ha configuration file:

  

copy code
global    #settings of global parameters 
log 127.0.0.1 local0 info
 # log syntax: log <address_1>[max_level_1] # global log configuration, use the log keyword, specify to use the local0 log device in the syslog service on 127.0.0.1, record log 
user haproxy with log level info
group haproxy
#Set the user and group to run haproxy, you can also use uid, gid keyword instead of 
daemon #Run
 as a daemon
nbproc 16
#Set the number of processes when haproxy starts. According to the explanation of the official document, I understand it as: the setting of this value should be consistent with the number of CPU cores of the server, that is, a common server with 2 8-core CPUs, that is, a total of 16 cores , you can set its value to: <=16, creating multiple processes can reduce the task queue of each process, but too many processes may also cause the process to crash. Here I set it to 16
maxconn 4096
#Define the maximum number of connections per haproxy process. Since each connection includes a client and a server, the maximum number of TCP sessions for a single process will be twice this value. 
# ulimit -n 65536 #Set 
the maximum number of open file descriptors, as suggested in the official documentation of 1.4, this value will be automatically calculated, so it is not recommended to set 
pidfile /var/run/ haproxy.pid #Define the pid 
defaults of haproxy
 # Definition of the default section mode http
 # mode syntax: mode {http|tcp|health}. http is a seven-layer mode, tcp is a four-layer mode, health is a health check, and returns OK 
log 127.0.0.1 local3 err
 #Use the local3 device of the syslog service on 127.0.0.1 to record error information

retries 3
#Define the number of failed reconnections to connect to the backend server. After the number of connection failures exceeds this value, the corresponding backend server will be marked as unavailable 
option httplog #Enable
 logging HTTP requests, the default haproxy logging does not record HTTP requests. Only log "time [Jan 5 13:23:46] log server [127.0.0.1] instance name has pid [haproxy[25218]] information [Proxy http_80_in stopped.]", the log format is very simple. 
option redispatch #When
 a cookie is used, haproxy will insert the serverID of the back-end server it requests into the cookie to ensure the SESSION persistence of the session; at this time, if the back-end server is down, but the client's cookie It will not be refreshed. If this parameter is set, the client's request will be forced to be directed to another backend server to ensure the normal service. 
option abortonclose #When
 the server load is very high, automatically end the connection that has been processed for a long time in the current queue 
option dontlognull #Enable
 this item , and the empty connection will not be recorded in the log. The so-called empty connection is when the upstream load balancer or monitoring system needs to connect regularly or obtain a fixed component or page in order to detect whether the service is alive and available, or detect whether the scanning port is listening or open. Empty connection; marked in the official documentation, if there is no other load balancer upstream of the service, it is recommended not to use this parameter, because malicious scans or other actions on the Internet will not be recorded 
option httpclose
 #I understand this parameter like this: using this parameter, haproxy will check the value of Connection in the http header after each request is processed. If the value is not close, haproxy will delete it. If the value is empty, it will be deleted. Will be added as: Connection: close. Causes each client and server to actively close the TCP connection after completing a transfer. Another parameter similar to this parameter is "option forceclose". The function of this parameter is to forcibly close the external service channel, because some servers will not automatically close the TCP connection when they receive Connection: close. If not closed, the connection will remain open until it times out.
contimeout 5000
#Set the maximum waiting time for a successful connection to a server, the default unit is milliseconds, the new version of haproxy uses timeout connect instead, this parameter is backward compatible
clitimeout 3000
#Set the maximum waiting time for a successful connection when the connection client sends data. The default unit is milliseconds. The new version of haproxy uses timeout client instead. This parameter is backward compatible
srvtimeout 3000
#Set the maximum waiting time for the server to respond to client data sending. The default unit is milliseconds. The new version of haproxy uses timeout server instead. This parameter is backward compatible with 

listen status #Define a section called status
bind 0.0.0.0:1080
#Define the listening socket 
mode http #Define
 as HTTP mode log global #Inherit
 the definition of log in global stats refresh 30s
 # stats is a socket of a statistics page of haproxy, this parameter sets the refresh interval of the statistics page to 30s 
stats uri /admin? stats #Set
 the uri of the statistics page to /admin?stats stats realm Private lands #Set
 the prompt content of the statistics page authentication stats auth admin:password #Set
 the user and password for the statistics page authentication, if you want to set multiple, Write another line to 
stats hide-version #Hide the haproxy version
 information on the statistics page 
frontend http_80_in #Define a front-end part named http_80_in





bind 0.0.0.0:80
# http_80_in defines the socket 
mode monitored by the front-end part http
 # Defined as HTTP mode 
log global
 # Inherit the definition of log in global 
option forwardfor
 # Enable X-Forwarded-For, insert the client IP in the request header and send it to the back-end server, Make the backend server get the real IP of the client
acl static_down nbsrv(static_server) lt 1
#Define an acl named static_down , which will be matched when the number of surviving machines in the backend static_sever is less than 1
acl php_web url_reg /*.php$
# acl php_web path_end .php 
#Define an acl named php_web. When the end of the requested url ends with .php, it will be matched. Choose one of the above two ways of writing.
acl static_web url_reg /*.(css|jpg|png|jpeg|js|gif)$
# acl static_web path_end .gif .png .jpg .css .js .jpeg #Define an acl named 
static_web , when the end of the requested url ends with .css, .jpg, .png, .jpeg, .js, .gif Yes , it will be matched, choose one of the above two ways of writing use_backend 
php_server if static_down #If
 the policy static_down is satisfied, the request will be handed over to the backend php_server use_backend 
php_server if php_web #If
 the policy php_web is satisfied, the request will be handed over backend php_server use_backend 
static_server if static_web #If
 the policy static_web is satisfied, hand over the request to backend static_server 
backend php_server #Define a backend part named php_server mode http
 #Set to http mode balance source
 #Set haproxy 's scheduling algorithm as the source address hash



cookie SERVERID #Allows SERVERID
 to be inserted into the cookie, the SERVERID of each server can be defined using the cookie keyword 
option below httpchk GET /test/index.php #Enable the health
 check of the backend server, through GET /test/index.php Judging the health of the backend server 
server php_server_1 10.12.25.68:80 cookie 1 check inter 2000 rise 3 fall 3 weight 2 
server php_server_2 10.12.25.72:80 cookie 2 check inter 2000 rise 3 fall 3 weight 1 
server php_server_bak 10.12.25.79:80 cookie 3 check inter 1500 rise 3 fall 3 backup
 # server syntax: server [:port] [param*] # Use the server keyword to set the backend server;
The internal name [php_server_1] set for the backend server that will be present in logs or alerts,
The IP address of the back-end server, supports port mapping [10.12.25.68:80], specifies that the server's SERVERID is 1 [cookie 1],
Accept health monitoring [check], monitoring interval, in milliseconds [inter 2000],
After monitoring the normal number of times, the backend server is considered to be available [rise 3],
How many times the monitoring fails, the backend server is considered to be unavailable [fall 3],
Distribution weight [weight 2],
The most backup back-end server, when all the normal servers are down, the backup server will be enabled [backup]

backend static_server
mode http
option httpchk GET /test/index.html
server static_server_1 10.12.25.83:80 cookie 3 check inter 2000 rise 3 fall 3
copy code

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326681778&siteId=291194637