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 realize reverse proxy and load balancing in haproxy

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article will explain in detail how to achieve reverse proxy and load balancing in haproxy. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Nginx can realize the caching function, but haproxy can not. This paper only describes its reverse proxy function and load balancing function.

Yum install haproxy main configuration file haproxy.cfg enables log function: edit / etc/rsyslog.conf file $ModLoad imudp$UDPServerRun 514 # Open udp514 port local2.* / var/log/haproxy.log edit / etc/haproxy/haproxy.cfg file: log 127.0.0.1 local2 configuration load balancer backend host: global log 127.0 .0.1 local2 chroot / var/lib/haproxy pidfile / var/run/haproxy.pid maxconn 4000 defines the total maximum number of client-oriented connections (client-facing side) user haproxy group haproxy daemon # turn on stats unix socket stats socket / var/lib/haproxy/stats#-- -# main frontend which proxys to the backends# -frontend main *: 80 # first way # bind *: 80 # second way # bind *: 8080 # can only be used for frontend Listen # maxconn can also be defined here or after listen, the maximum number of concurrent connections for a single instance is defined If the definition in the global section is the total default_backend websrvs#----# round robin balancing between the various backends#- of all instances -backend websrvs balance roundrobin server web1 192.168.20.7 the name web1 defined by web1 80 check # will be added to the request header and sent to the backend Server web2 192.168.20.8 server web2 80 check is useful when there is a virtual host at the back end.

Balance: specify the scheduling algorithm; several scheduling algorithms:

Dynamic: weights can be adjusted dynamically

Static: adjusting weights will not take effect in real time

Roundrobin: polling, dynamic algorithm, up to 4128 connections per backend host

Static-rr: polling, static algorithm, unlimited number of supports per backend host

Leastconn: scheduling according to the load of backend hosts; only for long-connected sessions; dynamic

Hash-type:

Map-based: modeling method; static

Consistent: consistent hash; dynamic

The following four scheduling algorithms are based on the above two hash-type

Source:

Uri: for the left half of uri (? (the part before the tag) or the whole uri do hash, and bind to the backend server except the total weight of the backend server.

Url_param: schedule according to the value of the specified parameter in url; the value is calculated by hash and divided by the total weight

Hdr (): schedule according to the header specified in the request message (such as use_agent, referer, hostname); the value of the specified header is calculated by hash divided by the total weight

Example:

Backend websrvs balance hdr (User-Agent) hash-type consistent server web1 192.168.20.7 check server web2 80 check server web2 192.168.20.8 check

Test:

Mode: what protocol is used in health status detection?

The working mode of HAProxy; by default, there are three types of tcp;: tcp, http, and health

Only the client, the front end and the back end communicate with http can use http mode.

You can also specify log in the front section:

Frontend main *: 80 log global log 127.0.0.2 local3

Define the latter segment using use_backend and acl

Use_backend dynamic if url_dyn

Use_backend static if url_css url_img extension_img

Parameters that can be added after the server segment:

Backup: set as a standby server, but no other server in the load balancing scenario can be used to enable this server;check: start a health check on this server, which can be set more finely with the help of other additional parameters, such as: inter: set the time interval for health check in milliseconds, default is 2000; you can also use fastinter and downinter to optimize this time delay based on server state Rise: sets the number of times an offline server changes from offline state to normal state in health check; fall: confirms the number of times server needs to be checked when it transitions from normal state to unavailable state; cookie: sets a cookie value for the specified server, the value specified here will be checked when inbound is requested, and the first selected server for this value will be selected in subsequent requests to achieve the function of persistent connection Maxconn: specifies the maximum number of concurrent connections accepted by this server; if the number of connections sent to this server is higher than the value specified here, it will be placed in the request queue to wait for other connections to be released; maxqueue: sets the maximum length of the request queue Observe: determines the health status of the server by observing the communication status of the server. The default is disabled. The supported types are "layer4" and "layer7". "layer7" can only be used in http proxy scenarios. Redir: enable the redirect function to respond to both GET and HEAD requests sent to this server with a 302 status code. It should be noted that / cannot be used after prefix, and relative addresses cannot be used to avoid loops. For example: server srv1 172.16.100.6 redir 80 redir http://imageserver.magedu.com checkweight: weight. Default is 1. The maximum value is 256, which means you do not participate in load balancing. Option httpchkoption httpchkoption httpchk for example: backend https_relay mode tcp option httpchk OPTIONS * HTTP/1.1\ r\ nHost:\ www.lee123.com server apache1 192.168.1.1 nHost 443 check port 80 use case: server first 172.16.100.7 option httpchkoption httpchkoption httpchk 1080 cookie first check inter 1000server second 172.16.100.8 HTTP/1.1 1080 cookie second check inter 1000

Implement session sticky based on browser cookie: you can use option to define the health check method:

Key points: (1) each server has its own unique cookie identity; (2) it is defined in backend as cookiebackend websrvs balance roundrobin cookie SERVERID insert nocache indirect server web1 192.168.20.7 check cookie websrv1 server web2 80 check cookie websrv1 server web2 192.168.20.8 check cookie websrv1 server web2 80 check cookie websrv2 after the user's request scheduling is completed.

wKioL1Y4qbmzOZf2AAIwdL1Eqd0065.jpg

Test: did you notice the websrv1 keyword in the cookie header?

Open the statistics page:

Listen statistics bind *: 9090 stats enable stats hide-version # stats scope. Stats uri / haproxyadmin?stats stats realm "HAPorxy\ Statistics" stats auth admin:mageedu stats admin if TRUE

Record additional information in the log:

Capture request header

Capture response header

When mode is http, record rich log information:

Option httplog---- is enabled by default

Error page redirection:

Errorfile: respond with haproxy host local file

Errorloc, errorloc302: use the specified url to respond. The response status code is 302. It is not applicable to request methods other than GET.

Errorloc303: returns 303 status codes

Add request or response message header:

Reqadd

Rspadd

Frontend main bind *: 80 bind *: 8080 rspadd Via:\ node1.lee.com default_backend websrvs

Via appears:

Example of static and dynamic separation: frontend main bind *: 80 bind *: 8080 acl url_static path_beg-I / static / images / javascript / stylesheets acl url_static path_end-I .jpg .gif .png .css .js use_backend static if url_static default_backend appsrvs#- -# static backend for serving up images Stylesheets and such#---- backend static balance roundrobin server static1 192.168.20.7 check server static2 192.168.20.8 check backend appsrvs balance roundrobin Option forwardfor except 127.0.0.1 header X-Client option httpchk cookie SERVERID insert indirect nocache server web1 192.168.20.7 check cookie web1 server web2 80 check cookie web1 server web2 192.168.20.8 check cookie web2 on how to implement reverse proxy and load balancing in haproxy I hope the above content can be of some help to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

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