Centos7 haproxy动静分离
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Centos7 haproxy动静分离相关的知识,希望对你有一定的参考价值。
Centos7 haproxy动静分离
实现的结果:
当客户端访问haproxy时,请求的是静态文件内容时,请求转交给static server,请求的是php内容时,请求转交给php server,请求的是jsp内容时,请求转交给tomcat server,以实现动静分离。
haproxy服务器:192.168.80.100
先部署三台web服务器:
一台httpd支持php 192.168.80.101
一台部署httpd支持静态资源 192.168.80.102
一台tomcat支持jsp 192.168.80.103192.168.80.100:
1、安装编译环境与软件yum install -y pcre-devel bzip2-devel gcc gcc-c++ make
2、源码编译安装haproxytar xzvf haproxy-1.5.15.tar.gz -C /opt
cd /opt/haproxy-1.5.15
make TARGET=linux26 PREFIX=/usr/local/haproxy //标识64为系统
make install PREFIX=/usr/local/haproxy
3、配置haproxy配置文件mkdir /etc/haproxy
useradd -s /sbin/nologin -M haproxy
cp /opt/haproxy-1.5.15/examples/haproxy.cfg /etc/haproxy/
vi /etc/haproxy/haproxy.cfg
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
log 127.0.0.1 local3
maxconn 204800
chroot /usr/local/haproxy
user haproxy
group haproxy
daemon
nbproc 1
pidfile /var/run/haproxy.pid
stats socket /usr/local/haproxy/stats
description haproxy server
#---------------------------------------------------------------------
# common defaults that all the ‘listen‘ and ‘backend‘ sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
log global
mode http
maxconn 10000
option httplog
option httpclose
option dontlognull
option forwardfor except 127.0.0.0/8
retries 3
option redispatch
option abortonclose
balance roundrobin
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
#---------------------------------------------------------------------
# use listen setting the haproxy status for site
#---------------------------------------------------------------------
listen admin_status #设置haproxy监控状态
bind *:8089
mode http
log 127.0.0.1 local3 err
stats refresh 5s
stats uri /status #监控状态页面访问url
stats realm www.skeryp.com
stats auth admin:admin
stats hide-version
stats admin if TRUE
#---------------------------------------------------------------------
# main listen which proxys to the backends
#---------------------------------------------------------------------
listen www
bind *:80
maxconn 5000
mode http
log global
option httplog
option httpclose
option forwardfor
log global
default_backend default #设置默认访问页面
#定义当请求的内容是静态内容时,将请求转交给static server的acl规则
acl url_static path_beg -i /static /images /img /javascript /stylesheets
acl url_static path_end -i .jpg .gif .png .css .js .html
acl host_static hdr_beg(host) -i img. video. download. ftp. imags. videos.
#定义当请求的内容是php内容时,将请求转交给php server的acl规则
acl url_php path_end -i .php
#定义当请求的内容是.jsp或.do内容时,将请求转交给tomcat server的acl规则
acl url_jsp path_end -i .jsp .do
#引用acl匹配规则
use_backend static_pool if url_static or host_static
use_backend php_pool if url_php
use_backend tomcat_pool if url_jsp
#定义后端backend server
backend static_pool
option httpchk GET /index.html
server static1 192.168.80.101:80 cookie id1 check inter 2000 rise 2 fall 3
backend php_pool
option httpchk GET /info.php
server php1 192.168.80.102:80 cookie id1 check inter 2000 rise 2 fall 3
backend tomcat_pool
option httpchk GET /index.jsp
server tomcat1 192.168.80.103:8086 cookie id2 check inter 2000 rise 2 fall 3
#<----------------------default site for listen and frontend------------------------------------>
backend default
mode http
option httpchk GET /index.html
server default 192.168.80.104:80 cookie id1 check inter 2000 rise 2 fall 3 maxconn 5000
192.168.80.101:
1、安装phpyum install -y php
2、安装php插件
yum install -y php-mysql php-gd libjpeg* php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-bcmath php-mhash
3、修改配置文件
vi /etc/httpd/conf/httpd.conf
ServerName www.aa.com
DirectoryIndex index.html index.php
vi /etc/php.ini
date.timezone = PRC 日期时区=×××
4、创建一个php网页
cd /var/www/html
vi index.php
<?php
phpinfo();
?>
systemctl start httpd
浏览器输入:192.168.80.101
192.168.80.102:
1、安装httpdyum install httpd -y
2、安装上传软件yum install lrz* -y
3、修改配置文件
vi /etc/httpd/conf/httpd.conf
ServerName www.aa.com
4、创建一个img目录,在给img目录里放进去一张图片,在创建一个index.html网页
cd /var/www/html
mkdir img
放进入一张图片,找一张图片直接拉到Xshell
cd ..
vi index.html
<h1>静态</h1>
systemctl start httpd
浏览器输入:192.168.80.102
192.168.80.102/img/图片名称.格式
192.168.80.103:
软件包自己去网上找
1、安装JAVAtar xf jdk-8u144-linux-x64.tar.gz
cp -rv jdk1.8.0_144/ /usr/local/java
vi /etc/profile //在文件末尾新增
export JAVA_HOME=/usr/local/java 安装路径
export JRE_HOME=/usr/local/java/jre
export PATH=$PATH:/usr/local/java/bin
export CLASSPATH=./:/usr/local/java/lib:/usr/local/java/jre/lib
source /etc/profile
2、安装tomcat
cp -r apache-tomcat-8.5.23 /usr/local/tomcat8
ln -s /usr/local/tomcat8/bin/startup.sh /usr/bin/tomcatup
ln -s /usr/local/tomcat8/bin/shutdown.sh /usr/bin/tomcatdown
tomcatup
netstat -anpt | grep 8080
浏览器输入:192.168.80.103:8080
测试:浏览器中输入
192.168.80.100:8089/status 统计页面
浏览器中输入:192.168.80.100/index.php
192.168.80.100/img/图片名称.格式
192.168.80.100/index.jsp
以上是关于Centos7 haproxy动静分离的主要内容,如果未能解决你的问题,请参考以下文章
实现 KeepAlive + Haproxy + Nginx 七层负载均衡 + 动静分离