Apache, Nginx and other reverse proxy (forwarding) function, usually only for the HTTP protocol, the other Protocol is not good (note: Nginx is said to be commercial version, support TCP protocol).
Haproxy can compensate for this shortcoming, haproxy support http/tcp a variety of protocols, can be used as RPC (THRIFT/GRPC/AVRO) framework front-end load balancer forwarding middleware, the following describes the basic use:
The following environments are Mac OSX.
First, installation
Brew Install Haproxy
The default installation is version 1.6.1, note: If you do not have brew installed, please visit the http://brew.sh/installation first.
The path after installation is:
/usr/local/cellar/haproxy/1.6.0
Or, you can directly http://www.haproxy.org/#down download
When the installation is complete, enter
haproxy-version If you can see output similar to the following:
Ha-proxy version 1.6.0 2015/10/13
Copyright 2000-2015 Willy Tarreau <[email protected]>
Indicates successful installation
Second, HTTP forwarding configuration
Find a directory (for example: ~/work/cfg/), create a haproxy.cfg file (the file name is arbitrary), the reference content is as follows:
Global daemon maxconn 256defaults mode http timeout connect 5000ms timeout client 50000ms Timeout server 50000mslisten http-in bind *:9000 server Server1 127.0.0.1:8081 Maxconn 32
The main is the last three lines, indicating that the native 9000 port HTTP access, forwarded to the 127.0.0.1:8081 port, that is, Access: http://127.0.0.1:9000 equivalent to access http://127.0.0.1:8081
Third, start
Haproxy-f ~/work/cfg/haproxy.cfg-d
If normal, the following will be output:
Available Polling Systems:
kqueue:pref=300, test result OK
poll:pref=200, test result OK
select:pref=150, test result OK
Total:3 (3 usable), would use Kqueue.
Using Kqueue () as the polling mechanism.
At this point, the access http://localhost:9000/should have the result, and the terminal will have the relevant information output
Note: If you are prompted to bind a port if it fails, first check that the port is occupied
Command lsof-i tcp:port (port replaced by port number, such as 9000) can see what the port is occupied by the program, and display PID, convenient kill process
If the port is not occupied, try to switch to a higher port, I am in the Mac to try, just start using 80 or 81 port, always up, use the above command to check the goods occupy, also not occupied, replaced by a high port, before normal start, do not know is not individual phenomenon.
Iv. HTTP Load Balancing Example
Global daemon maxconn 256defaults mode http stats enable stats uri/haproxy-stats stats Refresh 10s monitor-uri/haproxy-test balance roundrobin option httpclose option Forwardfor Timeout Connect 5000ms timeout client 50000ms Timeout server 50000mslisten my-web-cluster1 bind *:9000< C16/>server Server1 127.0.0.1:80 server Server2 192.168.1.14:80
The above configuration indicates that when http://localhost:9000/is accessed, it is forwarded to one of the 127.0.0.1:80 or 192.168.1.14:80
In addition, visit Http://localhost:9000/haproxy-stats can also see a statistics page, http://localhost:9000/haproxy-test used to test haproxy work is normal
Reference article:
Http://cbonte.github.io/haproxy-dconv/configuration-1.6.html
Haproxy Novice on the road