HAproxy基础(2)
六.基础配置阶段1.安装haproxy
# yum install -y haproxy
# cd /etc/haproxy/
# cp haproxy.cfg{,.bak}
# ls
haproxy.cfg haproxy.cfg.bak 2.开启haproxy的系统日志
# vim/etc/rsyslog.conf
$ModLoad imudp
$UDPServerRun 514
local2.* /var/log/haproxy.log 重新启动rsyslog服务:
# service rsyslog restart
Shutting down system logger:
Starting system logger: 3.编辑配置文件,添加后端web服务器
# vim/etc/haproxy/haproxy.cfg
global
#to have these messages end up in /var/log/haproxy.log you will
#need to:
#
#1) configure syslog to accept network log events.This is done
# by adding the '-r' option tothe SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
#2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like thefollowing can be added to
# /etc/sysconfig/syslog
#
# local2.* /var/log/haproxy.log
#
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
#turn on 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
frontend main *:80
default_backend websrvs
backend websrvs
balance roundrobin
servernode7 172.16.31.30:80check
servernode8 172.16.31.31:80check 4.启动服务:
# service haproxy start
Starting haproxy: [ OK] 5.访问测试:
是基于轮询调度算法的。
http://s3.运维网.com/wyfs02/M00/58/79/wKioL1Sybm-j8kFrAAAqmd4yeR8582.jpg
七.常用配置解析:
1.cookie会话保持
backend websrvs
balance roundrobin
cookieSRV insert indirectnocache
servernode7 172.16.31.30:80cookie node7 check rise 1 fall 2
servernode8 172.16.31.31:80cookie node8 check 重启haproxy服务:
# service haproxy restart
Stopping haproxy:
Starting haproxy: 访问测试:
记录了cookie,实现了会话保持:
http://s3.运维网.com/wyfs02/M01/58/79/wKioL1SybrDBzgpSAALqT_mw9rY247.jpg
2.启用反向服务器状态信息页面
backend websrvs
balance roundrobin
servernode7 172.16.31.30:80cookie node7 check rise 1 fall 2
servernode8 172.16.31.31:80cookie node8 check
stats enable 重启haproxy服务,访问测试:
http://s3.运维网.com/wyfs02/M02/58/79/wKioL1SybtaCMB9SAAXV0BHCpqo780.jpg
状态页安全性配置:
backend websrvs
balance roundrobin
servernode7 172.16.31.30:80cookie node7 check rise 1 fall 2
servernode8 172.16.31.31:80cookie node8 check
stats enable
stats uri /haproxyadm?stats
stats hide-version
stats realm HAProxy\ Status
stats auth admin:admin
stats admin if TRUE 重启haproxy服务,访问测试:
http://s3.运维网.com/wyfs02/M02/58/7C/wKiom1Sybjfzht7EAAKVmz94Etk213.jpghttp://s3.运维网.com/wyfs02/M00/58/79/wKioL1SybxCx5XDtAAXRFTe7irM609.jpg
3.让后端web服务器记录真实的访问客户端IP地址
更改后端web服务器的日志格式:
# vim/etc/httpd/conf/httpd.conf
#LogFormat "%h %l %u %t\"%r\" %>s %b \"%{Referer}i\"\"%{User-Agent}i\"" combined
#将如上日志格式更改为下面的格式即可
LogFormat "%{X-Forwarded-For}i %l %u%t \"%r\" %>s %b \"%{Referer}i\"\"%{User-Agent}i\"" combined 重新启动web服务器后进行测试访问后查看日志:
# tail/var/log/httpd/access_log
#以前访问的记录地址都是haproxy服务器的地址
172.16.31.32 - - "GET / HTTP/1.1" 200 16 "-" "Mozilla/5.0(Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)Chrome/37.0.2062.124 Safari/537.36"
#更改记录日志格式后记录的是真实的客户端IP地址
172.16.31.254 - - "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0(Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)Chrome/37.0.2062.124 Safari/537.36"
172.16.31.254 - - "GET / HTTP/1.1" 200 16 "-" "Mozilla/5.0(Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)Chrome/37.0.2062.124 Safari/537.36" 4.通过ACL实现网站访问的动静分离
我通过ACL将动态资源的访问到节点7,而静态资源的访问定位到节点8
先在节点7和节点8安装php,实现php动态资源和httpd服务器的结合:
# yum install -y php 创建phpinfo测试页:
#cat /var/www/html/index.php
节点7和节点8都存在动态的php测试页:
http://s3.运维网.com/wyfs02/M01/58/7C/wKiom1SybuvRuVjZAAK2aZvkM6c982.jpg
http://s3.运维网.com/wyfs02/M02/58/79/wKioL1Syb7_iZyjUAAKwL5p4OLg063.jpg
我们配置haproxy实现动静分离:
# cat /etc/haproxy/haproxy.cfg
global
# to have these messages end up in/var/log/haproxy.log you will
#need to:
#
#1) configure syslog to accept network log events.This is done
# by adding the '-r' option tothe SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
#2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like thefollowing can be added to
# /etc/sysconfig/syslog
#
# local2.* /var/log/haproxy.log
#
log 127.0.0.1 local2
chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon
#turn on 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
listen stats
bind :1080
mode http
stats enable
stats uri /haproxy?stats
stats realm HAProxy\ Status
stats auth admin:admin
stats admin if TRUE
frontend http-in
bind *:80
mode http
log global
option httpclose
option logasap
option dontlognull
capture request header Host len 20
capture request header Referer len 60
acl url_static path_beg -i /static /images /javascript /stylesheets
acl url_static path_end -i.html .jpg .jpeg .gif .png .css .js
acl url_dynamic path_end -i .php .jsp
use_backend static_servers ifurl_static
use_backend dynamic_servers if url_dynamic
default_backend dynamic_servers
backend static_servers
balance roundrobin
servernode7 172.16.31.30:80check maxconn 1000
backend dynamic_servers
balance roundrobin
cookie srv insert nocache
server node8 172.16.31.31:80 check maxconn 1000 cookie node8 重新启动haproxy服务进行访问测试:
我们访问静态的html页面,代理服务器就定位到节点7上进行访问;
http://s3.运维网.com/wyfs02/M02/58/7C/wKiom1SybxiwJeNoAANJwtDBJ_M018.jpg
我们访问动态页面,代理服务器就将请求定位到了节点8上,并记录了session会话状态;
http://s3.运维网.com/wyfs02/M00/58/79/wKioL1Syb_HgERCnAAMn0-9xTxg158.jpg
至此,一些基础的haproxy实用配置就介绍到这里。
页:
[1]