����Linux�Ż�
Linux����

CentOS 7��ʹ��HAProxyʵ��Nginx���ؾ���

����ʱ��:2016-09-16 15:31:53��Դ:topspeedsnail.com����:�������è
HAProxy��һ���ǿ�������õķ�������Ŀ�Դ���������ṩ�˸��ؾ��⡢�����������Ĺ��ܡ�HAProxy��Willy Tarreauʹ��C���Ա�д�ģ���֧��SSL��ѹ����keep-alive���Զ�����־��ʽ��header��д��
HAProxy���������ĸ��ؾ���ʹ�������������ռ��ϵͳ��Դ���١��ܶ���͵���վ����ʹ����������Github��StackOverflow��
�����Ұ�װ����HAProxy��Ϊ����Nginx�������ĸ��ؾ��⡣һ����Ҫʹ��3������������һ̨�����ϰ�װHAProxy������̨������װNginx����
 
HAProxy�Ļ�������
4���7��
HAProxy����ʹ������ģʽ���У�TCP 4��ģʽ��HTTP 7��ģʽ��TCPģʽ��HAProxy��ԭʼTCP���ݰ��ӿͻ���ת��Ӧ�÷�������HTTPģʽ������http����Ȼ��ת��web�����������ǽ�ʹ��HTTP 7��ģʽ��
���ؾ����㷨
HAProxyʹ�ø��ؾ����㷨�����������͸��ĸ���������ʹ�õ��㷨��
Roundrobin�������㷨
������򵥵ĸ��ؾ����㷨����ÿ�������ӣ�������һ����˷���������������������һ����˷���������ͷ��ʼ��
Lastconn
���������ӵĺ�˷��������������󡣵��������ϴ�ʱ�dz��á�
Source
���ݿͻ���IP�����ĸ���˷��������������IP1��server1��������ô���IP1������������server1����������IP��ַ�Ĺ�ϣֵ������˷�������
 
ϵͳҪ��
3��CentOS 7��������
�������ؾ����HAProxy��������192.168.0.101
Nginx1��������192.168.0.108
Nginx2��������192.168.0.109
 
��һ��
�༭HAProxy������(102.168.0.101)��/etc/hosts��
vim /etc/hosts
����Nginx1��Nginx2����������
192.168.0.108    nginx1.your_domain.com     nginx1
192.168.0.109    nginx2.your_domain.com     nginx2
�����˳���
ͬ�����༭����Nginx��������/etc/hosts�����ӣ�
192.168.0.101    loadbalancer
������Nginx�������϶�Ҫ���á�
 
�ڶ���
��HAProxy�������ϰ�װHAProxy��
# yum update
# yum install haproxy
haproxy�������ļ�λ��/etc/haproxy/��Ϊ�˷�ֹ�������ȱ���ԭʼ�����ļ���
# cd /etc/haproxy/
# mv haproxy.cfg haproxy.cfg.backup
�༭�����ļ���
# vim haproxy.cfg
д���������ݣ�
#---------------------------------------------------------------------
# ȫ������
#---------------------------------------------------------------------
global
log         127.0.0.1 local2     # ��־
chroot      /var/lib/haproxy
pidfile     /var/run/haproxy.pid
maxconn     4000                
user        haproxy             # Haproxy��haproxy�û�����������
group       haproxy
daemon
# ���� stats unix socket
stats socket /var/lib/haproxy/stats
#---------------------------------------------------------------------
# ��������
#---------------------------------------------------------------------
defaults
mode                    http
log                     global
option                  httplog
option                  dontlognull
option http-server-close
option forwardfor       except 127.0.0.0/8
option                  redispatch
retries                 3
timeout http-request    10s
timeout queue           1m
timeout connect         10s
timeout client          1m
timeout server          1m
timeout http-keep-alive 10s
timeout check           10s
maxconn                 3000
#---------------------------------------------------------------------
# HAProxy Monitoring ����
#---------------------------------------------------------------------
listen haproxy3-monitoring *:8080                # Haproxy Monitoring ��ʹ�ö˿ڣ�8080
mode http
option forwardfor
option httpclose
stats enable
stats show-legends
stats refresh 5s
stats uri /stats                            # HAProxy monitoring����ַ
stats realm Haproxy\ Statistics
stats auth testuser:test1234                # ��¼Monitoring���û�������
stats admin if TRUE
default_backend app-main
#---------------------------------------------------------------------
# FrontEnd ����
#---------------------------------------------------------------------
frontend main
bind *:80
option http-server-close
option forwardfor
default_backend app-main
#---------------------------------------------------------------------
# ʹ��roundrobin��Ϊ���ؾ����㷨
#---------------------------------------------------------------------
backend app-main
balance roundrobin                                    # ʹ�õĸ��ؾ����㷨
option httpchk HEAD / HTTP/1.1\r\nHost:\ localhost    # ���nginx�������Ƿ���ͨ- 200״̬��
server nginx1 192.168.0.108:80 check                  # Nginx1 
server nginx2 192.168.0.109:80 check                  # Nginx2
 
����rsyslog
������Ҫʹ��rsyslog��¼HAProxy����־���༭rsyslog.conf�����ļ�����UDP��514�˿ڣ�
# vim /etc/rsyslog.conf
ȥ�������е�ע�ͣ�
$ModLoad imudp
$UDPServerRun 514
�������ָ���ض�IP�����Ը��������У�
$UDPServerAddress 127.0.0.1
�����˳���
����rsyslog�����ļ���
# vim /etc/rsyslog.d/haproxy.conf
д���������ݣ�
local2.=info     /var/log/haproxy-access.log    # ������־
local2.notice    /var/log/haproxy-info.log      # haproxyִ����Ϣ
����rsyslog��
# systemctl restart rsyslog
����HAProxy��
# systemctl start haproxy
# systemctl enable haproxy
 
������
��װ����Nginx��
Nginx1��Nginx2���������÷�����ͬ��
��װepel-release��
# yum install epel-release
��װNginx��
# yum install nginx
����index.html�����ļ���Nginx1(192.168.0.108)��
# cd /usr/share/nginx/html/
# echo "<h1>nginx1.your_domain.com</h1>" > index.html
Nginx2(192.168.0.109)��
# cd /usr/share/nginx/html/
# echo "<h1>nginx2.your_domain.com</h1>" > index.html
����Nginx����
# systemctl enable nginx
# systemctl start nginx
 
����
����nginx1��2��ʹ����������ʣ�
192.168.0.108
192.168.0.109
����������վ��http://192.168.0.101�������������ָ�����IP��
��¼HAProxy web����ҳ�棺
http://192.168.0.101:8080/stats
��Ӧ�ÿ��Կ���nginx��http�����ת����Ϣ��
 
�������ø��µ�ַ��//m.ajphoenix.com/linux/24220.html