How to use Haproxy to achieve static and dynamic separation of Web sites

Source: Internet
Author: User
Tags haproxy

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 on the client side (User-space) with 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.
--Excerpt from Baidu Encyclopedia

Implementation process diagram:

The first step: install httpd, PHP and Haproxy, here we directly with Yum to install, here we use three virtual machines to do the test, the front-end of a haproxy to do scheduling, the back end of two httpd servers to provide Web services, When doing static and dynamic separation, the front-end haproxy to determine which servers are dynamically and statically dispatched to each other;

Yum install httpd php haproxy

The second step: after the installation of the three virtual configuration of the relevant network equipment and provide relevant test page, etc., in the eth1 this net card to choose the same communication channel, NODE0 this virtual machine haproxy host, Node1 and Node2 are different virtual hosts that provide Web servers for both back ends;

[Email protected] ~]#ifconfigEth1192.168.27.10/ -Up # Configure IP address for eth1 [[email protected]~]#ifconfigeth0 Link encap:ethernet HWaddrxx: 0C: in: B2:AD:BA inet Addr: 172.16. 27.88 Bcast:172.16.255.255Mask:255.255.0.0Inet6 addr:fe80::20c:29ff:feb2:adba/ -scope:link up broadcast RUNNING multicast MTU: theMetric:1RX Packets:11122303Errors0Dropped0Overruns:0Frame0TX Packets:1193136Errors0Dropped0Overruns:0Carrier0Collisions:0Txqueuelen: +RX Bytes:697850934(665.5MiB) TX Bytes:74549101(71.0MiB)      eth1 Link encap:ethernet HWaddrxx: 0C: in: B2:AD:C4 inet Addr: 192.168. 27.10 Bcast:192.168.27.255Mask:255.255.255.0Inet6 addr:fe80::20c:29ff:feb2:adc4/ -scope:link up broadcast RUNNING multicast MTU: theMetric:1RX Packets:0Errors0Dropped0Overruns:0Frame0TX Packets:6Errors0Dropped0Overruns:0Carrier0Collisions:0Txqueuelen: +RX Bytes:0(0.0b) TX Bytes:468(468.0b) [[Email protected]~]#

[email protected] ~]# ifconfig eth0 192.168 . 27.18 / - up on Node1 and Node2 set up the relevant configuration, select the virtual machine on the VMnet2 and Haproxy eth1 on the same communication channel, then configure the IP address and the default gateway interface, and then provide a different page for testing;

 192.168 . 27.18   # Gateway points to the eth1 address of the Haproxy host [[email protected]  ~]# ping  27.10   # ping Haproxy node to see if Ping is [[email protected]  ~]# vim/var/www/html/ index.html  [[email protected]  ~]# service httpd Start # Configure the Web service to boot up, then access the test, test the connection changed to bridge mode   
[Email protected] ~]#ifconfigEth0192.168.27.19/ -Up[[email protected]~]# Route add Defaulte GW192.168.27.18# Gateway points to the eth1 address of the haproxy host [[email protected]~]#Ping 192.168.27.10# Ping the Haproxy node to see if it pings [[email protected]~]# vim/var/www/html/index.phpphp phpinfo ();?>[[Email protected]~]# service httpd Start # After configuring the Web service to start up, and then access the test, the test connection changed to bridging mode

Use the default configuration above ############## to ###################### #frontend web*: the# *indicates that the Haproxy listens on all addresses and listens on a port of 80 # To define access control, which represents a URL with a. css. js. html. PHP end of the separately dispatched to which server to access the ACL url_static path_end -I. CSS. js. html ACL url_dynamic path_end-i. PHP # Usr_backend represents the use of the backend service, if it means that the url_static is dispatched to this server if the condition is met use_backend staticifurl_static default_backend dynamicbackend Static # defines the server on which to call the back-end page on the servers Node1192.168.27.18: theCheck Inter theRise2Fall2Maxconn theBackend Dynamic # defines a server node2 that is called on the back-end Dynamics page192.168.27.19: theCheck Inter theRise2Fall2Maxconn theListen Statspage # defines interface for the monitoring management interface bind*:8888# define access page Port stats enable # Enabled admin Interface stats Hide-Version # Hidden versions stats URI/admin?Stats # access Path stats auth Xiao:linux # Access requires authentication login Stats AdminifTRUE # If the login succeeds, you can manage the online server

The third step: Configure the relevant configuration information of Haproxy;

Fourth Step : Login to our configured management interface:

Fifth Step: Configure the relevant configuration so that you can test it,

Finally, we will do the two pages of the request for a stress test to see:

Summarize:

The Haproxy is a lightweight, load-balanced server, especially for those 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.

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.