Centos7 haproxy dynamic and static separation

Results achieved:
When the client accesses haproxy, when the request is static file content, the request is transferred to static server, when the request is php content, the request is transferred to php server, when the request is jsp content, the request is transferred to tomcat server to realize dynamic static separation.
haproxy server: 192.168.80.100
First deploy three web servers:
One httpd supports PHP 192.168.80.101
One httpd deployment supports static resources 192.168.80.102
A tomcat supports JSP 192.168.80.103

192.168.80.100:
1. Install compilation environment and software
yum install -y pcre-devel bzip2-devel gcc gcc-c++ make

2. Source compilation and installation of haproxy
tar xzvf haproxy-1.5.15.tar.gz -C /opt

cd /opt/haproxy-1.5.15
make TARGET=linux26  PREFIX=/usr/local/haproxy     //Identify 64 as system
make install PREFIX=/usr/local/haproxy

3. Configure the haproxy configuration file
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     #Set haproxy monitoring status
        bind *:8089
        mode http
        log 127.0.0.1 local3 err
        stats refresh 5s
        stats uri /status     #Monitoring status page access 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   #Set default access page
        #Define the acl rule to pass the request to the static server when the requested content is static       
        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.
        #Define the acl rule to pass the request to the php server when the content of the request is php content    
        acl url_php path_end     -i .php
        #Define the acl rule to pass the request to tomcat server when the content of the request is. jsp or. do content    
        acl url_jsp path_end     -i .jsp .do

        #Reference acl matching rules
        use_backend static_pool if  url_static or host_static
        use_backend php_pool    if  url_php
        use_backend tomcat_pool if  url_jsp

   #Define 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. Install php
yum install -y php

2. Install php plug-in

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. Modify the configuration file

vi /etc/httpd/conf/httpd.conf
ServerName www.aa.com
DirectoryIndex index.html index.php
vi /etc/php.ini
date.timezone = PRC    Date time zone=×××

4. Create a php web page

cd  /var/www/html
vi index.php
<?php
phpinfo();
?>

systemctl start httpd

Browser input: 192.168.80.101

192.168.80.102:
1. Install httpd
yum install httpd -y

2. Install upload software
yum install lrz* -y

3. Modify the configuration file

vi /etc/httpd/conf/httpd.conf
ServerName www.aa.com

4. Create an img directory, put a picture in the IMG directory, and create an index.html web page

cd  /var/www/html
mkdir img
 Put in a picture, find a picture and pull it directly to Xshell
cd ..
vi  index.html
 <h1>Static</h1>

systemctl start httpd
Browser input: 192.168.80.102
192.168.80.102/img/picture name.format

192.168.80.103:
Go to the Internet to find the software package
1. Install JAVA
tar xf jdk-8u144-linux-x64.tar.gz

cp -rv jdk1.8.0_144/ /usr/local/java

vi /etc/profile //Add at the end of the file

export JAVA_HOME=/usr/local/java  Installation path
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. Install 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

Browser input: 192.168.80.103:8080

Tests: Browser input
192.168.80.100:8089/status statistics page

Input in browser: 192.168.80.100/index.php

192.168.80.100/img/picture name.format

192.168.80.100/index.jsp

Tags: Linux PHP JSP Java Tomcat

Posted on Tue, 03 Dec 2019 00:13:39 -0500 by shamuntoha