Haproxy+nginx Load Balancing

Source: Internet
Author: User
Tags http request haproxy nginx load balancing

Haproxy provides high availability, load balancing, and proxies based on TCP and HTTP applications, supporting virtual hosting, which is a free, fast, and reliable solution. Haproxy is especially useful for Web sites that are heavily loaded, and often require session-hold or seven-tier processing. The haproxy runs on the current hardware and can support tens of thousands of concurrent connections. and its operating mode makes it easy and safe to integrate into your current architecture, while protecting your Web server from being exposed to the web.

Haproxy implements an event-driven, single-process model that supports very large number of concurrent connections. A multi-process or multithreaded model is rarely capable of handling thousands of concurrent connections because of memory limitations, System scheduler restrictions, and ubiquitous lock limits. The event-driven model does not have these problems because it implements all of these tasks in a user space (User-space) that has better resource and time management. The disadvantage of this model is that, on multicore systems, these programs often have poor extensibility. That's why they have to be optimized so that each CPU time slice (Cycle) does more work. Second, installation Haproxy

Wget http://www.haproxy.org/download/1.5/src/haproxy-1.5.14.tar.gz (outside the wall).
Tar zxvf haproxy-1.5.14.tar.gz
UNAME-A # view Linux kernel version
Make Target=linux26 Prefix=/usr/local/haproxy
Make install Prefix=/usr/local/haproxy
Start Haproxy [root@localhost sbin]# haproxy-f/etc/haproxy/haproxy.cfg configuration file/etc/haproxy.cfg, haproxy configuration file content many very flexible, Here are just a few simple things to configure.

Global log 127.0.0.1 local0 log 127.0.0.1 local1 notice #log loghost local0 info maxconn 4096 C
    Hroot/var/haproxy uid GID daemon nbproc 1 #debug #quiet defaults log Global Mode HTTP option httplog option dontlognull retries 3 option Redispatch # Auto Redirect to health machine Maxconn 200 0 Timeout Check 2000ms # Check out timeout timeout connect 5000ms # Connection Timeout timeout client 50000ms # Clients connection time out timeout serve
       R 50000ms # service-side Connection Timeout Listen webinfo:1080 mode http balance roundrobin stats uri/ha_status  Option httpclose option forwardfor server web1 192.168.163.34:80 Check weight 1 minconn 1 Maxconn 3 Check inter 40000 server web2 192.168.163.35:80 Check weight 1 minconn 1 Maxconn 3 check inter 40000 #配置控制台 lis Ten stats:6553 mode http transparent stats uri/haproxy-stats stats Realm haproxy \ Statisti C Stats Auth AdmiN:admin 
third, testing

There are a total of 3 machines, the IP is 192.168.163.33,192.168.163.34,192.168.163.35 respectively.
33 This machine is fitted with haproxy,34 and 352 machines with Nginx.
Nginx Installation Reference previous Nginx installation -192.168.163.34:80 test

-192.168.163.35:80 Test

-Request came over to be polled by the WEB1 and WEB2 assigned to the Webinfo group, respectively

Web1 192.168.163.34:80
WEB2 192.168.163.35:80 -Environment ready, test 192.168.163.33, will rotation to 2 machines as follows:

Paproxy's console, refer to haproxy.cfg inside the configuration
Http://192.168.163.33:6553/haproxy-stats
-on the IP 33 machine, the pressure test is distributed to 2 nginx machines via Haproxy .

[Root@localhost ~]# ab-c 1-n 10000 http://192.168.163.33:1080/results
-on the IP 33 machine, test the Nginx machine with IP 34

[Root@localhost ~]# ab-c 1-n 10000 http://192.168.163.34:80/index.html results
-through the above is the test visible through the Haproxy shunt after the efficiency is reduced. It's better than a single nginx. Reference

Now Haproxy algorithm is also very much, no more than the professional F5/lvs algorithm less, commonly used algorithms have the following 8 kinds:
Balance Roundrobin, expressed simple polling, suggested concern;
Balance Static-rr, said according to the weight, suggest concern;
Balance Leastconn, indicating that the least connected to the first processing, the proposed attention;
Balance source, said that according to the request of the IP, and nginx Ip_hash algorithm similar to the proposed attention;
A balance URI that represents the URI according to the request;
Balance Url_param, which represents the URL parameter according to the request;
Balance HDR (name), which indicates that each HTTP request is locked according to the HTTP request header;
Balance Rdp-cookie (name), which indicates that each TCP request is locked and hashed according to the cookie (name). Reference Articles

http://blog.haohtml.com/archives/7959
Http://www.open-open.com/lib/view/open1388290883344.html
Https://tyr.so/haproxy.html
http://www.haproxy.org

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.