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 in Linux to realize Web load balancing

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces how to configure HAProxy in Linux to achieve Web load balancing, which is very detailed and has a certain reference value. Interested friends must read it!

HAProxy is an agent software that provides high availability, load balancing and applications based on TCP (layer 4) and HTTP (layer 7). It supports virtual hosts. It is a free, fast and reliable solution.

I. Test environment

HAProxy:

Hostname: RH7-HAProxy

IP address: 192.168.10.20

Operating system: Red Hat Enterprise Linux Server release 7.2 (Maipo) minimized installation

Firewall and SELinux: off

Installed service: HAProxy-1.5.14

WEB01:

Hostname: RH7-Web-01 IP address: 192.168.10.21 operating system: Red Hat Enterprise Linux Server release 7.2 (Maipo) minimizing installation of firewalls and SELinux: shutting down installed services: Apache-httpd 2.4

WEB02:

Hostname: RH7-Web-02 IP address: 192.168.10.22 operating system: Red Hat Enterprise Linux Server release 7.2 (Maipo) minimizing installation of firewalls and SELinux: shutting down installed services: Apache-httpd 2.4

II. WEB page access test

1. Use a browser to access the IP addresses of the two web, and you can successfully access different pages, which is convenient for us to intuitively observe the effect of load balancer later. As the installation and configuration of Apache is not the focus of this article, please refer to my previous blog post "RedHat 7 installation and configuration Apache 2.4"

2. Also visit the two Web on RH7-HAProxy and confirm that they can also be accessed normally.

3. Install and configure HAProxy [root@RH7-HAProxy~] yum-y install haproxy [root@RH7-HAProxy~] systemctl start haproxy [root@RH7-HAProxy~] systemctl enable haproxy [root@RH7-HAProxy~] mv / etc/haproxy/haproxy.cfg / etc/haproxy/haproxy.cfg.bak [root@RH7-HAProxy~] vi / etc/haproxy/haproxy.cfg

Add the following, and then restart the haproxy service to access the IP address of HAProxy using the browser, and refresh it many times. The successful polling access web01 and web02 login haproxy backend can see the relevant statistics of accessing web01 and web02 to modify the haproxy configuration file. Change the weight of web01 to 3MagneWeb02, and the weight is still 1, so the ratio of access to two web should be 3:1. After the improvement, restart the haproxy service to access the IP address of the HAProxy through the browser again, refresh the access test many times, log in to the HAProxy backend again to view the statistics, and you can see that the ratio of access to web01 and web02 is 3:1.

Configure HAProxy to use Https to encrypt access

Use https from client to HAProxy and http from HAProxy to Web. You only need to configure a certificate on HAProxy. Here we will configure a self-signed certificate to test and check the configured certificate jiangjianlong.pem to modify the configuration file of HAProxy. After adding the following parameters, restart the haproxy service to access the IP address of HAProxy using https protocol, indicating that there is a problem with the certificate. This is because the self-signed certificate is not trusted by the browser. However, it means that the certificate configured above is already valid. Click to continue to browse this website.

Successfully access the Web01 page and refresh the Web02, indicating that under the encrypted access of https, the load balancer has no problem configuring a domain name pointing to the IP address of HAProxy through hosts resolution, and can also access and view the certificate information normally, configure http redirection in accordance with the certificate information we have created, automatically redirect http to https, modify the haproxy configuration file, add the following parameters, and restart the service to test using the browser. The http is automatically redirected to https for access successfully, but it is not truncated because the screenshot cannot show this dynamic effect.

Configure HAProxy to use independent log files

1. The log of HAProxy is written to the system log by default, and we can make it independent to facilitate troubleshooting and management. Edit / etc/rsyslog.conf, press the following figure to modify 2, restart the rsyslog service, visit web through the browser to generate the log, then check / var/log/haproxy.log, and find that the log has been recorded in / var/log/haproxy.log as we configured above.

These are all the contents of the article "how to configure HAProxy in Linux to achieve Web load balancing". Thank you for reading! Hope to share the content to help you, more related 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

Development

Wechat

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

12
Report