Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure HAProxy

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to configure HAProxy, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Configuration of HAProxy

The HAProxy configuration is divided into five parts. Of course, these components are not required and can be selected as configuration as needed.

Global: parameters are process-level, usually related to the operating system (OS). These parameters are usually set only once. If the configuration is correct, there is no need to modify the configuration again.

Defaults: configure default parameters that can be used to configure to frontend,backend,listen components

Frontend: the front-end virtual node that receives the request. Frontend can directly specify the backend of the backend (which can be dynamically selected) according to the rules.

Backend: the configuration of the backend service cluster is a real server. A Backend corresponds to one or more physical servers.

Combination of listen:Frontend and Backend.

Here are some common configurations of HAProxy. This configuration is used to explain the configuration of some common functions of HAProxy. For more details, please see the documentation file in the doc directory under the installation directory, or go to http://cn.haproxy.org/ to download Chinese configuration instructions.

Configure a specific example, with instructions attached:

Global

# Global log configuration where the log level is [err warning info debug]

# local0 is a log device and must be one of the following 24 standard syslog devices:

# kern user mail daemon auth syslog lpr news

# uucp cron auth3 ftp ntp audit alert cron2

# local0 local1 local2 local3 local4 local5 local6 local7

# but local0 was previously defined in the / etc/syslog.conf file, so

# local0 is also used here

Log 127.0.0.1 local0 info # [err warning info debug]

# * connections

Maxconn 4096

# user

User admin

# Group

Group admin

# make the HAProxy process run in the background. This is the recommended operation mode.

Daemon

# create 4 processes to run in deamon mode. This parameter requires that the operation mode be set to "daemon"

Nbproc 4

# the user who starts the process by writing the pid of all processes to the file must have permission to access this file.

Pidfile / home/admin/haproxy/logs/haproxy.pid

Defaults

# default mode mode {tcp | http | health}, tcp is layer 4, http is layer 7, and health only returns OK

Mode http

# use http log format

Option httplog

# if the connection fails three times, the server is considered unavailable, or you can set it later.

Retries 3

If cookie writes to serverId and the client does not refresh cookie

# when the corresponding server of serverId hangs up, force the direction to another healthy server

Option redispatch

# automatically end the links that have been processed for a long time in the current queue when the server load is high

Option abortonclose

# default number of * connections

Maxconn 4096

# connection timeout

Contimeout 5000

# client timeout

Clitimeout 30000

# Server timeout

Srvtimeout 30000

# = heartbeat detection timeout

Timeout check 2000

# Note: some parameter values are time, such as timeout. The time value is usually measured in milliseconds (ms), but you can also use other units by adding a # suffix.

#-us: microseconds. 1 microsecond = 1 million second

#-ms: milliseconds. 1 millisecond = 1 second. This is the default.

#-s: seconds. 1s = 1000ms

#-m: minutes. 1m = 60s = 60000ms

#-h: hours. 1h = 60m = 3600s = 3600000ms

#-d: days. 1D = 24 hours = 1440m = 86400s = 86400000ms

# Statistics page configuration #

Listen admin_stats

# listening port

Bind 0.0.0.0:1080

# 7-tier mode of http

Mode http

# Log Settings

Log 127.0.0.1 local0 err # [err warning info debug]

# Statistics page automatic refresh time

Stats refresh 30s

# Statistics page url

Stats uri / admin?stats

# prompt text on the password box on the statistics page

Stats realm Gemini\ Haproxy

# user name and password settings on statistics page

Stats auth admin:admin

Stats auth admin1:admin1

# hide the version information of HAProxy on the statistics page

Stats hide-version

# definition of listen for website detection #

Listen site_status

Bind 0.0.0.0:1081

Mode http

Log 127.0.0.1 local0 err # [err warning info debug]

# website Health Test URL, which is used to check whether the website managed by HAProxy is available. It returns 200 normally and 500 abnormally.

Monitor-uri / site_status

# policies when defining down of a website

# return true when the number of medium-effective machines in the specified backend hung on the load balancer is less than 1

Acl site_dead nbsrv (denali_server) lt 1

Acl site_dead nbsrv (tm_server) lt 1

Acl site_dead nbsrv (mms_server) lt 1

# return 500 when the policy is met

Monitor fail if site_dead

# if the machine dies in 192.168.0.252 or 192.168.0.31

# if you think the website is dead, return 5000.If the mode is

# it is considered normal if http returns 2000.If mode is tcp, it is good that the port is unblocked

Monitor-net 192.168.0.252/31

# frontend configuration #

Frontend http_80_in

# listening port

Bind 0.0.0.0:80

# 7-tier mode of http

Mode http

# apply global log configuration

Log global

# enable log for http

Option httplog

# actively close the http tunnel after each request. HA-Proxy does not support keep-alive mode.

Option httpclose

# if the backend server needs to obtain the real IP of the client and needs to configure secondary parameters, it will be available from the Http Header

# obtain client IP

Option forwardfor

# configuration of logging content for HAProxy #

Capture request header Host len 40

Capture request header Content-Length len 10

Capture request header Referer len 200

Capture response header Server len 40

Capture response header Content-Length len 10

Capture response header Cache-Control len 8

# acl Policy definition #

# return true-I to ignore case if the requested domain name satisfies the regular expression

Acl denali_policy hdr_reg (host)-I ^ (www.gemini.taobao.net | my.gemini.taobao.net | auction1.gemini.taobao.net) $

# if the request domain name meets trade.gemini.taobao.net, return true-I is case ignored

Acl tm_policy hdr_dom (host)-I trade.gemini.taobao.net

# # if sip_apiname=, is included in the request url, this control policy returns true, otherwise it is false

Acl invalid_req url_sub-I sip_apiname=

# # if timetask exists as part of the address path in the request url, this control policy returns true, otherwise it returns false

Acl timetask_req url_dir-I timetask

# return true when Content-length equals 0 in the requested header

Acl missing_cl hdr_cnt (Content-length) eq 0

# # matching acl policies #

# # when Content-length equals 0 in header in the request, prevent the request from returning 403

Block if missing_cl

# # block means to block the request. A 403 error is returned. If the policy invalid_req or policy timetask_req is not met, the request will be blocked.

Block if! invalid_req | | timetask_req

# use denali_server 's backend when meeting denali_policy 's policy

Use_backend denali_server if denali_policy

# use tm_server 's backend when meeting tm_policy 's policy

Use_backend tm_server if tm_policy

# reqisetbe keyword definition, and select backend according to the defined keyword

Reqisetbe ^ Host:\ img dynamic

Reqisetbe ^ [^\] *\ / (img | css) / dynamic

Reqisetbe ^ [^\] * / admin/stats stats

# use the default mms_server backend when none of the above is satisfied

Default_backend mms_server

# HAProxy error Page setup

Errorfile 400 / home/admin/haproxy/errorfiles/400.http

Errorfile 403 / home/admin/haproxy/errorfiles/403.http

Errorfile 408 / home/admin/haproxy/errorfiles/408.http

Errorfile 500 / home/admin/haproxy/errorfiles/500.http

Errorfile 502 / home/admin/haproxy/errorfiles/502.http

Errorfile 503 / home/admin/haproxy/errorfiles/503.http

Errorfile 504 / home/admin/haproxy/errorfiles/504.http

# Settings for backend #

Backend mms_server

# 7-tier mode of http

Mode http

# load balancing method, roundrobin average method

Balance roundrobin

# allow insertion of serverid into cookie, which can be defined after serverid

Cookie SERVERID

# URL,HTTP/1.1 for heartbeat detection ¥r ¥nHost:XXXX, which specifies the version of heartbeat detection HTTP, and XXX is the request for heartbeat detection

# what is the domain name in the request of the server? if the corresponding function of detecting URL in the application depends on the domain name, you need to set it.

Option httpchk GET / member/login.jhtml HTTP/1.1\ r\ nHost:member1.gemini.taobao.net

# Server definition. Cookie 1 indicates that serverid is 1 inter check inter 1500 is to detect the heartbeat rate.

# rise 3 correctly considers the server available for 3 times, while fall 3 considers the server unavailable for 3 failures. Weight represents weight.

Server mms1 10.1.5.134:80 cookie 1 check inter 1500 rise 3 fall 3 weight 1

Server mms2 10.1.6.118:80 cookie 2 check inter 1500 rise 3 fall 3 weight 2

Backend denali_server

Mode http

# load balancing. Source hashes according to the client IP.

Balance source

# but when backup is set, the default backup will be given priority. When option allbackups is set,

# all backup servers have the same weight

Option allbackups

# heartbeat detection URL settings

Option httpchk GET / mytaobao/home/my_taobao.jhtml HTTP/1.1\ r\ nHost:my.gemini.taobao.net

# you can use your own special connection number configuration instead of the default connection number configuration according to the performance of the machine

# such as minconn 10 maxconn 20

Server denlai1 10.1.5.114:80 minconn 4 maxconn 12 check inter 1500 rise 3 fall 3

Server denlai2 10.1.6.104:80 minconn 10 maxconn 20 check inter 1500 rise 3 fall 3

# back up the machine configuration. Normally, the standby machine will not be used. When all the servers of the host are down, the backup machine will enable it.

Server dnali-back1 10.1.7.114:80 check backup inter 1500 rise 3 fall 3

Server dnali-back2 10.1.7.114:80 check backup inter 1500 rise 3 fall 3

Backend tm_server

Mode http

# load balancing. Leastconn selects the server with the least number of requests according to the current number of requests of the server.

Balance leastconn

Option httpchk GET / trade/itemlist/prepayCard.htm HTTP/1.1\ r\ nHost:trade.gemini.taobao.ne

Server tm1 10.1.5.115:80 check inter 1500 rise 3 fall 3

Server tm2 10.1.6.105:80 check inter 1500 rise 3 fall 3

# reqisetbe custom keyword matching backend section #

Backend dynamic

Mode http

Balance source

Option httpchk GET / welcome.html HTTP/1.1\ r\ nHost:www.taobao.net

Server denlai1 10.3.5.114:80 check inter 1500 rise 3 fall 3

Server denlai2 10.4.6.104:80 check inter 1500 rise 3 fall 3

Backend stats

Mode http

Balance source

Option httpchk GET / welcome.html HTTP/1.1\ r\ nHost:www.163.com

Server denlai1 10.5.5.114:80 check inter 1500 rise 3 fall 3

Server denlai2 10.6.6.104:80 check inter 1500 rise 3 fall 3

The above is all the content of this article "how to configure HAProxy". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report