HAProxy的高级配置选项-ACL篇之基于文件后缀实现动静分离

简介: 这篇文章介绍了HAProxy的高级配置选项,特别是如何使用ACL(访问控制列表)基于文件后缀实现动静分离的案例,通过配置示例展示了如何将动态内容和静态内容分别交由不同的后端服务器处理。

作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。

一.试验环境概述

1>.操作平台介绍

[root@node101.yinzhengjie.org.cn ~]# uname -r
3.10.0-957.el7.x86_64
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# uname -m
x86_64
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           7.6G        126M        7.4G        8.6M        129M        7.3G
Swap:          7.9G          0B        7.9G
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

172.30.1.101 node101.yinzhengjie.org.cn node101.yinzhengjie.com
172.30.1.102 node102.yinzhengjie.org.cn
172.30.1.103 node103.yinzhengjie.org.cn
172.30.1.104 node104.yinzhengjie.org.cn
172.30.1.105 node105.yinzhengjie.org.cn
172.30.1.106 node106.yinzhengjie.org.cn
172.30.1.107 node107.yinzhengjie.org.cn
172.30.1.108 node108.yinzhengjie.org.cn
[root@node101.yinzhengjie.org.cn ~]# 
[root@node101.yinzhengjie.org.cn ~]#

2>.试验架构说明

  node102.yinzhengjie.org.cn:
    haproxy服务器

  node103.yinzhengjie.org.cn:
    Apache httpd模拟静态数据,如存放的图片,html,css,javascript等。  
  node104.yinzhengjie.org.cn:
    "Nginx + php环境"模拟动态数据,php程序等。

二.部署Nginx服务器处理动态页面

1>.安装epel源

[root@node104.yinzhengjie.org.cn ~]# yum -y install epel-release
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.huaweicloud.com
 * extras: mirrors.huaweicloud.com
 * updates: mirror.bit.edu.cn
extras/7/x86_64/primary_db                                                    | 153 kB  00:00:00     
Resolving Dependencies
--> Running transaction check
---> Package epel-release.noarch 0:7-11 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=====================================================================================================
 Package                     Arch                  Version               Repository             Size
=====================================================================================================
Installing:
 epel-release                noarch                7-11                  extras                 15 k

Transaction Summary
=====================================================================================================
Install  1 Package

Total download size: 15 k
Installed size: 24 k
Downloading packages:
epel-release-7-11.noarch.rpm                                                  |  15 kB  00:00:05     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : epel-release-7-11.noarch                                                          1/1 
  Verifying  : epel-release-7-11.noarch                                                          1/1 

Installed:
  epel-release.noarch 0:7-11                                                                         

Complete!
[root@node104.yinzhengjie.org.cn ~]#

[root@node104.yinzhengjie.org.cn ~]# yum -y install epel-release

2>.安装nginx和php

[root@node104.yinzhengjie.org.cn ~]# yum -y install nginx php-fpm
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
epel/x86_64/metalink                                                          | 7.1 kB  00:00:00     
 * base: mirrors.huaweicloud.com
 * epel: hkg.mirror.rackspace.com
 * extras: mirrors.huaweicloud.com
 * updates: mirror.bit.edu.cn
epel                                                                          | 5.3 kB  00:00:00     
(1/3): epel/x86_64/updateinfo                                                 | 1.0 MB  00:00:02     
(2/3): epel/x86_64/group_gz                                                   |  90 kB  00:00:02     
(3/3): epel/x86_64/primary_db                                                 | 6.9 MB  00:00:11     
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.16.1-1.el7 will be installed
--> Processing Dependency: nginx-all-modules = 1:1.16.1-1.el7 for package: 1:nginx-1.16.1-1.el7.x86_6
4--> Processing Dependency: nginx-filesystem = 1:1.16.1-1.el7 for package: 1:nginx-1.16.1-1.el7.x86_64
--> Processing Dependency: nginx-filesystem for package: 1:nginx-1.16.1-1.el7.x86_64
--> Processing Dependency: redhat-indexhtml for package: 1:nginx-1.16.1-1.el7.x86_64
--> Processing Dependency: libprofiler.so.0()(64bit) for package: 1:nginx-1.16.1-1.el7.x86_64
---> Package php-fpm.x86_64 0:5.4.16-46.1.el7_7 will be installed
--> Processing Dependency: php-common(x86-64) = 5.4.16-46.1.el7_7 for package: php-fpm-5.4.16-46.1.el
7_7.x86_64--> Running transaction check
---> Package centos-indexhtml.noarch 0:7-9.el7.centos will be installed
---> Package gperftools-libs.x86_64 0:2.6.1-1.el7 will be installed
---> Package nginx-all-modules.noarch 1:1.16.1-1.el7 will be installed
--> Processing Dependency: nginx-mod-http-image-filter = 1:1.16.1-1.el7 for package: 1:nginx-all-modu
les-1.16.1-1.el7.noarch--> Processing Dependency: nginx-mod-http-perl = 1:1.16.1-1.el7 for package: 1:nginx-all-modules-1.16
.1-1.el7.noarch--> Processing Dependency: nginx-mod-http-xslt-filter = 1:1.16.1-1.el7 for package: 1:nginx-all-modul
es-1.16.1-1.el7.noarch--> Processing Dependency: nginx-mod-mail = 1:1.16.1-1.el7 for package: 1:nginx-all-modules-1.16.1-1.
el7.noarch--> Processing Dependency: nginx-mod-stream = 1:1.16.1-1.el7 for package: 1:nginx-all-modules-1.16.1-
1.el7.noarch---> Package nginx-filesystem.noarch 1:1.16.1-1.el7 will be installed
---> Package php-common.x86_64 0:5.4.16-46.1.el7_7 will be installed
--> Processing Dependency: libzip.so.2()(64bit) for package: php-common-5.4.16-46.1.el7_7.x86_64
--> Running transaction check
---> Package libzip.x86_64 0:0.10.1-8.el7 will be installed
---> Package nginx-mod-http-image-filter.x86_64 1:1.16.1-1.el7 will be installed
--> Processing Dependency: gd for package: 1:nginx-mod-http-image-filter-1.16.1-1.el7.x86_64
--> Processing Dependency: libgd.so.2()(64bit) for package: 1:nginx-mod-http-image-filter-1.16.1-1.el
7.x86_64---> Package nginx-mod-http-perl.x86_64 1:1.16.1-1.el7 will be installed
---> Package nginx-mod-http-xslt-filter.x86_64 1:1.16.1-1.el7 will be installed
---> Package nginx-mod-mail.x86_64 1:1.16.1-1.el7 will be installed
---> Package nginx-mod-stream.x86_64 1:1.16.1-1.el7 will be installed
--> Running transaction check
---> Package gd.x86_64 0:2.0.35-26.el7 will be installed
--> Processing Dependency: libXpm.so.4()(64bit) for package: gd-2.0.35-26.el7.x86_64
--> Running transaction check
---> Package libXpm.x86_64 0:3.5.12-1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=====================================================================================================
 Package                             Arch           Version                    Repository       Size
=====================================================================================================
Installing:
 nginx                               x86_64         1:1.16.1-1.el7             epel            562 k
 php-fpm                             x86_64         5.4.16-46.1.el7_7          updates         1.4 M
Installing for dependencies:
 centos-indexhtml                    noarch         7-9.el7.centos             base             92 k
 gd                                  x86_64         2.0.35-26.el7              base            146 k
 gperftools-libs                     x86_64         2.6.1-1.el7                base            272 k
 libXpm                              x86_64         3.5.12-1.el7               base             55 k
 libzip                              x86_64         0.10.1-8.el7               base             48 k
 nginx-all-modules                   noarch         1:1.16.1-1.el7             epel             19 k
 nginx-filesystem                    noarch         1:1.16.1-1.el7             epel             21 k
 nginx-mod-http-image-filter         x86_64         1:1.16.1-1.el7             epel             30 k
 nginx-mod-http-perl                 x86_64         1:1.16.1-1.el7             epel             39 k
 nginx-mod-http-xslt-filter          x86_64         1:1.16.1-1.el7             epel             29 k
 nginx-mod-mail                      x86_64         1:1.16.1-1.el7             epel             57 k
 nginx-mod-stream                    x86_64         1:1.16.1-1.el7             epel             84 k
 php-common                          x86_64         5.4.16-46.1.el7_7          updates         565 k

Transaction Summary
=====================================================================================================
Install  2 Packages (+13 Dependent packages)

Total download size: 3.4 M
Installed size: 12 M
Downloading packages:
(1/15): centos-indexhtml-7-9.el7.centos.noarch.rpm                            |  92 kB  00:00:00     
(2/15): gd-2.0.35-26.el7.x86_64.rpm                                           | 146 kB  00:00:00     
(3/15): gperftools-libs-2.6.1-1.el7.x86_64.rpm                                | 272 kB  00:00:00     
(4/15): libXpm-3.5.12-1.el7.x86_64.rpm                                        |  55 kB  00:00:00     
(5/15): libzip-0.10.1-8.el7.x86_64.rpm                                        |  48 kB  00:00:00     
warning: /var/cache/yum/x86_64/7/epel/packages/nginx-1.16.1-1.el7.x86_64.rpm: Header V3 RSA/SHA256 Si
gnature, key ID 352c64e5: NOKEYPublic key for nginx-1.16.1-1.el7.x86_64.rpm is not installed
(6/15): nginx-1.16.1-1.el7.x86_64.rpm                                         | 562 kB  00:00:00     
(7/15): nginx-all-modules-1.16.1-1.el7.noarch.rpm                             |  19 kB  00:00:00     
(8/15): nginx-filesystem-1.16.1-1.el7.noarch.rpm                              |  21 kB  00:00:00     
(9/15): nginx-mod-http-image-filter-1.16.1-1.el7.x86_64.rpm                   |  30 kB  00:00:00     
(10/15): nginx-mod-http-perl-1.16.1-1.el7.x86_64.rpm                          |  39 kB  00:00:00     
(11/15): nginx-mod-http-xslt-filter-1.16.1-1.el7.x86_64.rpm                   |  29 kB  00:00:00     
(12/15): nginx-mod-mail-1.16.1-1.el7.x86_64.rpm                               |  57 kB  00:00:00     
(13/15): php-common-5.4.16-46.1.el7_7.x86_64.rpm                              | 565 kB  00:00:00     
(14/15): nginx-mod-stream-1.16.1-1.el7.x86_64.rpm                             |  84 kB  00:00:00     
(15/15): php-fpm-5.4.16-46.1.el7_7.x86_64.rpm                                 | 1.4 MB  00:00:00     
-----------------------------------------------------------------------------------------------------
Total                                                                2.5 MB/s | 3.4 MB  00:00:01     
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Importing GPG key 0x352C64E5:
 Userid     : "Fedora EPEL (7) <epel@fedoraproject.org>"
 Fingerprint: 91e9 7d7c 4a5e 96f1 7f3e 888f 6a2f aea2 352c 64e5
 Package    : epel-release-7-11.noarch (@extras)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : centos-indexhtml-7-9.el7.centos.noarch                                           1/15 
  Installing : libzip-0.10.1-8.el7.x86_64                                                       2/15 
  Installing : php-common-5.4.16-46.1.el7_7.x86_64                                              3/15 
  Installing : gperftools-libs-2.6.1-1.el7.x86_64                                               4/15 
  Installing : 1:nginx-filesystem-1.16.1-1.el7.noarch                                           5/15 
  Installing : libXpm-3.5.12-1.el7.x86_64                                                       6/15 
  Installing : gd-2.0.35-26.el7.x86_64                                                          7/15 
  Installing : 1:nginx-mod-stream-1.16.1-1.el7.x86_64                                           8/15 
  Installing : 1:nginx-mod-http-perl-1.16.1-1.el7.x86_64                                        9/15 
  Installing : 1:nginx-mod-mail-1.16.1-1.el7.x86_64                                            10/15 
  Installing : 1:nginx-mod-http-xslt-filter-1.16.1-1.el7.x86_64                                11/15 
  Installing : 1:nginx-1.16.1-1.el7.x86_64                                                     12/15 
  Installing : 1:nginx-mod-http-image-filter-1.16.1-1.el7.x86_64                               13/15 
  Installing : 1:nginx-all-modules-1.16.1-1.el7.noarch                                         14/15 
  Installing : php-fpm-5.4.16-46.1.el7_7.x86_64                                                15/15 
  Verifying  : libXpm-3.5.12-1.el7.x86_64                                                       1/15 
  Verifying  : 1:nginx-mod-stream-1.16.1-1.el7.x86_64                                           2/15 
  Verifying  : gd-2.0.35-26.el7.x86_64                                                          3/15 
  Verifying  : 1:nginx-filesystem-1.16.1-1.el7.noarch                                           4/15 
  Verifying  : 1:nginx-mod-http-perl-1.16.1-1.el7.x86_64                                        5/15 
  Verifying  : php-fpm-5.4.16-46.1.el7_7.x86_64                                                 6/15 
  Verifying  : gperftools-libs-2.6.1-1.el7.x86_64                                               7/15 
  Verifying  : php-common-5.4.16-46.1.el7_7.x86_64                                              8/15 
  Verifying  : 1:nginx-1.16.1-1.el7.x86_64                                                      9/15 
  Verifying  : 1:nginx-mod-mail-1.16.1-1.el7.x86_64                                            10/15 
  Verifying  : 1:nginx-all-modules-1.16.1-1.el7.noarch                                         11/15 
  Verifying  : libzip-0.10.1-8.el7.x86_64                                                      12/15 
  Verifying  : 1:nginx-mod-http-xslt-filter-1.16.1-1.el7.x86_64                                13/15 
  Verifying  : centos-indexhtml-7-9.el7.centos.noarch                                          14/15 
  Verifying  : 1:nginx-mod-http-image-filter-1.16.1-1.el7.x86_64                               15/15 

Installed:
  nginx.x86_64 1:1.16.1-1.el7                   php-fpm.x86_64 0:5.4.16-46.1.el7_7                  

Dependency Installed:
  centos-indexhtml.noarch 0:7-9.el7.centos      gd.x86_64 0:2.0.35-26.el7                            
  gperftools-libs.x86_64 0:2.6.1-1.el7          libXpm.x86_64 0:3.5.12-1.el7                         
  libzip.x86_64 0:0.10.1-8.el7                  nginx-all-modules.noarch 1:1.16.1-1.el7              
  nginx-filesystem.noarch 1:1.16.1-1.el7        nginx-mod-http-image-filter.x86_64 1:1.16.1-1.el7    
  nginx-mod-http-perl.x86_64 1:1.16.1-1.el7     nginx-mod-http-xslt-filter.x86_64 1:1.16.1-1.el7     
  nginx-mod-mail.x86_64 1:1.16.1-1.el7          nginx-mod-stream.x86_64 1:1.16.1-1.el7               
  php-common.x86_64 0:5.4.16-46.1.el7_7        

Complete!
[root@node104.yinzhengjie.org.cn ~]#

[root@node104.yinzhengjie.org.cn ~]# yum -y install nginx php-fpm

3>.修改nginx的配置文件,添加一个匹配php文件的localtion

[root@node104.yinzhengjie.org.cn ~]# vim /etc/nginx/nginx.conf
[root@node104.yinzhengjie.org.cn ~]# 
[root@node104.yinzhengjie.org.cn ~]# egrep -v "^ *#|^$" /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}
http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    include /etc/nginx/conf.d/*.conf;
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;
        include /etc/nginx/default.d/*.conf;
        location / {
        }
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
        error_page 404 /404.html;
            location = /40x.html {
        }
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}
[root@node104.yinzhengjie.org.cn ~]# 
[root@node104.yinzhengjie.org.cn ~]# nginx -t              #检查nginx的配置文件是否正确
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@node104.yinzhengjie.org.cn ~]#

4>.启动nginx和php服务

[root@node104.yinzhengjie.org.cn ~]# ss -ntl
State       Recv-Q Send-Q     Local Address:Port                    Peer Address:Port              
LISTEN      0      128                    *:22                                 *:*                  
LISTEN      0      128                   :::22                                :::*                  
[root@node104.yinzhengjie.org.cn ~]# 
[root@node104.yinzhengjie.org.cn ~]# systemctl start nginx php-fpm
[root@node104.yinzhengjie.org.cn ~]# 
[root@node104.yinzhengjie.org.cn ~]# ss -ntl
State       Recv-Q Send-Q     Local Address:Port                    Peer Address:Port              
LISTEN      0      128            127.0.0.1:9000                               *:*                  
LISTEN      0      128                    *:80                                 *:*                  
LISTEN      0      128                    *:22                                 *:*                  
LISTEN      0      128                   :::80                                :::*                  
LISTEN      0      128                   :::22                                :::*                  
[root@node104.yinzhengjie.org.cn ~]# 
[root@node104.yinzhengjie.org.cn ~]#

5>.创建php的测试页面,并通过浏览器访问"http://node104.yinzhengjie.org.cn/index.php"

[root@node104.yinzhengjie.org.cn ~]# vim /usr/share/nginx/html/index.php
[root@node104.yinzhengjie.org.cn ~]# 
[root@node104.yinzhengjie.org.cn ~]# cat /usr/share/nginx/html/index.php
<?php
    phpinfo();
?>
[root@node104.yinzhengjie.org.cn ~]# 
[root@node104.yinzhengjie.org.cn ~]#

三.部署Apache httpd服务器处理静态页面

1>.安装apache httpd服务器

[root@node103.yinzhengjie.org.cn ~]# yum -y install httpd
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: mirror.bit.edu.cn
 * extras: mirrors.huaweicloud.com
 * updates: mirror.bit.edu.cn
base                                                                          | 3.6 kB  00:00:00     
extras                                                                        | 2.9 kB  00:00:00     
updates                                                                       | 2.9 kB  00:00:00     
(1/4): base/7/x86_64/group_gz                                                 | 165 kB  00:00:00     
(2/4): extras/7/x86_64/primary_db                                             | 153 kB  00:00:00     
(3/4): updates/7/x86_64/primary_db                                            | 5.9 MB  00:00:01     
(4/4): base/7/x86_64/primary_db                                               | 6.0 MB  00:00:01     
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-90.el7.centos will be installed
--> Processing Dependency: httpd-tools = 2.4.6-90.el7.centos for package: httpd-2.4.6-90.el7.centos.x
86_64--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-90.el7.centos.x86_64
--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-90.el7.centos.x86_64
--> Running transaction check
---> Package apr.x86_64 0:1.4.8-5.el7 will be installed
---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed
---> Package httpd-tools.x86_64 0:2.4.6-90.el7.centos will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=====================================================================================================
 Package                 Arch               Version                           Repository        Size
=====================================================================================================
Installing:
 httpd                   x86_64             2.4.6-90.el7.centos               base             2.7 M
Installing for dependencies:
 apr                     x86_64             1.4.8-5.el7                       base             103 k
 apr-util                x86_64             1.5.2-6.el7                       base              92 k
 httpd-tools             x86_64             2.4.6-90.el7.centos               base              91 k

Transaction Summary
=====================================================================================================
Install  1 Package (+3 Dependent packages)

Total download size: 3.0 M
Installed size: 9.9 M
Downloading packages:
(1/4): apr-1.4.8-5.el7.x86_64.rpm                                             | 103 kB  00:00:00     
(2/4): httpd-2.4.6-90.el7.centos.x86_64.rpm                                   | 2.7 MB  00:00:00     
(3/4): httpd-tools-2.4.6-90.el7.centos.x86_64.rpm                             |  91 kB  00:00:00     
(4/4): apr-util-1.5.2-6.el7.x86_64.rpm                                        |  92 kB  00:00:05     
-----------------------------------------------------------------------------------------------------
Total                                                                531 kB/s | 3.0 MB  00:00:05     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : apr-1.4.8-5.el7.x86_64                                                            1/4 
  Installing : apr-util-1.5.2-6.el7.x86_64                                                       2/4 
  Installing : httpd-tools-2.4.6-90.el7.centos.x86_64                                            3/4 
  Installing : httpd-2.4.6-90.el7.centos.x86_64                                                  4/4 
  Verifying  : apr-1.4.8-5.el7.x86_64                                                            1/4 
  Verifying  : httpd-tools-2.4.6-90.el7.centos.x86_64                                            2/4 
  Verifying  : apr-util-1.5.2-6.el7.x86_64                                                       3/4 
  Verifying  : httpd-2.4.6-90.el7.centos.x86_64                                                  4/4 

Installed:
  httpd.x86_64 0:2.4.6-90.el7.centos                                                                 

Dependency Installed:
  apr.x86_64 0:1.4.8-5.el7  apr-util.x86_64 0:1.5.2-6.el7  httpd-tools.x86_64 0:2.4.6-90.el7.centos 

Complete!
[root@node103.yinzhengjie.org.cn ~]#

[root@node103.yinzhengjie.org.cn ~]# yum -y install httpd

2>.创建测试数据

[root@node103.yinzhengjie.org.cn ~]# rz                    #随便上传一张测试图片即可

[root@node103.yinzhengjie.org.cn ~]# 
[root@node103.yinzhengjie.org.cn ~]# ll
total 120
-rw-r--r-- 1 root root 122026 Dec 16 19:58 02.迪丽热巴.jfif
[root@node103.yinzhengjie.org.cn ~]# 
[root@node103.yinzhengjie.org.cn ~]# file 02.迪丽热巴.jfif 
02.迪丽热巴.jfif: JPEG image data, JFIF standard 1.01
[root@node103.yinzhengjie.org.cn ~]# 
[root@node103.yinzhengjie.org.cn ~]# mv 02.迪丽热巴.jfif /var/www/html/01.jpeg
[root@node103.yinzhengjie.org.cn ~]# 
[root@node103.yinzhengjie.org.cn ~]# ll /var/www/html/
total 120
-rw-r--r-- 1 root root 122026 Dec 16 19:58 01.jpeg
[root@node103.yinzhengjie.org.cn ~]# 
[root@node103.yinzhengjie.org.cn ~]#

3>.启动httpd服务并使用浏览器访问上一步创建的图片(http://node103.yinzhengjie.org.cn/01.jpeg)

[root@node103.yinzhengjie.org.cn ~]# ss -ntl
State       Recv-Q Send-Q     Local Address:Port                    Peer Address:Port              
LISTEN      0      128                    *:22                                 *:*                  
LISTEN      0      128                   :::22                                :::*                  
[root@node103.yinzhengjie.org.cn ~]# 
[root@node103.yinzhengjie.org.cn ~]# systemctl start httpd
[root@node103.yinzhengjie.org.cn ~]# 
[root@node103.yinzhengjie.org.cn ~]# ss -ntl
State       Recv-Q Send-Q     Local Address:Port                    Peer Address:Port              
LISTEN      0      128                    *:80                                 *:*                  
LISTEN      0      128                    *:22                                 *:*                  
LISTEN      0      128                   :::22                                :::*                  
[root@node103.yinzhengjie.org.cn ~]# 
[root@node103.yinzhengjie.org.cn ~]#

四.配置haproxy基于acl+文件后缀实现动静分离

1>.编辑haproxy的配置文件

[root@node102.yinzhengjie.org.cn ~]# cat /etc/haproxy/haproxy.cfg
global
    maxconn 100000
    chroot /yinzhengjie/softwares/haproxy
    stats socket /yinzhengjie/softwares/haproxy/haproxy.sock mode 600 level admin
    user haproxy
    group haproxy
    daemon
    nbproc 2
    cpu-map 1 0
    cpu-map 2 1
    nbthread 2
    pidfile /yinzhengjie/softwares/haproxy/haproxy.pid
    log 127.0.0.1 local5 info

defaults
    option http-keep-alive
    option  forwardfor
    option redispatch
    option abortonclose
    maxconn 100000
    mode http
    timeout connect 300000ms
    timeout client  300000ms
    timeout server  300000ms
    errorloc 503 http://node107.yinzhengjie.org.cn/monitor/503.html
listen status_page
    bind 172.30.1.102:8888
    stats enable
    stats uri /haproxy-status
    stats auth    admin:yinzhengjie
    stats realm "Welcome to the haproxy load balancer status page of YinZhengjie"
    stats hide-version
    stats admin if TRUE
    stats refresh 5s

frontend WEB_PORT_80
    bind 172.30.1.102:80
    mode http    #定义ACL匹配所有以".php"结尾的文件的php程序
    acl php_server path_end -i .php
    #将php的请求交给nginx服务器去处理.
    use_backend nginx_php if php_server
    #定义ACL匹配所有以".jpg",".png",".jpeg",".gif"结尾的图片
    acl image_server path_end -i .jpg .png .jpeg .gif
    #将图片的请求交给httpd服务器去处理.
    use_backend apache_httpd if image_server
    default_backend backup_web

backend nginx_php
    server web04 172.30.1.104:80  check inter 3000 fall 3 rise 5

backend apache_httpd
    server web03 172.30.1.103:80  check inter 3000 fall 3 rise 5

backend backup_web
    server web03 172.30.1.108:80  check inter 3000 fall 3 rise 5 
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# systemctl restart haproxy            #别忘记重启haproxy服务使得配置文件生效哟~
[root@node102.yinzhengjie.org.cn ~]#

2>.查看haproxy的监听端口和进程信息

[root@node102.yinzhengjie.org.cn ~]# ss -ntl
State       Recv-Q Send-Q                           Local Address:Port                                          Peer Address:Port              
LISTEN      0      128                               172.30.1.102:80                                                       *:*                  
LISTEN      0      128                                          *:22                                                       *:*                  
LISTEN      0      128                               172.30.1.102:8888                                                     *:*                  
LISTEN      0      128                                         :::22                                                      :::*                  
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]# ps -ef | grep haproxy | grep -v grep
root     20428     1  0 19:45 ?        00:00:00 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /yinzhengjie/softwares/haproxy/haproxy.pid
haproxy  20431 20428  0 19:45 ?        00:00:00 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /yinzhengjie/softwares/haproxy/haproxy.pid
haproxy  20432 20428  0 19:45 ?        00:00:00 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /yinzhengjie/softwares/haproxy/haproxy.pid
[root@node102.yinzhengjie.org.cn ~]# 
[root@node102.yinzhengjie.org.cn ~]#

3>.查看haproxy的状态页

五.验证haproxy的动静分离

1>.浏览器访问haproxy的地址:"http://node102.yinzhengjie.org.cn/index.php",如下图所示

2>.浏览器访问haproxy的地址:"http://node102.yinzhengjie.org.cn/01.jpeg",如下图所示

目录
相关文章
|
2月前
|
应用服务中间件 nginx 数据安全/隐私保护
HAProxy的高级配置选项-配置haproxy的状态页
这篇文章详细介绍了如何配置HAProxy的状态页,包括隐藏版本信息、设置自动刷新时间、自定义访问URI、配置认证信息以及启用管理功能等,并通过实战案例展示了配置过程和效果。
91 4
HAProxy的高级配置选项-配置haproxy的状态页
|
应用服务中间件 nginx
Nginx 服务器中设置禁止访问文件或目录的方法
Nginx 服务器中设置禁止访问文件或目录的方法
|
2月前
|
数据安全/隐私保护
HAProxy的高级配置选项-ACL篇之基于域名匹配案例
这篇文章介绍了HAProxy的高级配置选项中的ACL(访问控制列表)功能,特别是如何基于域名匹配进行流量分发的案例,包括ACL的基本概念、语法、使用场景和配置实例。
89 8
HAProxy的高级配置选项-ACL篇之基于域名匹配案例
|
2月前
|
应用服务中间件 PHP Apache
HAProxy的高级配置选项-ACL篇之匹配访问路径案例
这篇文章介绍了HAProxy的高级配置选项,特别是如何使用ACL(访问控制列表)匹配访问路径以实现不同请求路径的流量分发到不同后端服务器的案例,通过实战配置展示了如何基于URL路径将请求定向到处理静态或动态内容的服务器。
78 5
HAProxy的高级配置选项-ACL篇之匹配访问路径案例
|
2月前
|
Apache 数据安全/隐私保护
HAProxy的高级配置选项-ACL篇之域名重定向案例
这篇文章介绍了HAProxy的ACL(访问控制列表)功能,特别是如何基于域名进行重定向的高级配置选项,并通过实战案例展示了如何配置ACL规则以实现基于特定域名的HTTP重定向。
68 6
HAProxy的高级配置选项-ACL篇之域名重定向案例
|
2月前
HAProxy的高级配置选项-haproxy预定义(内置)acl使用案例
这篇文章介绍了HAProxy的高级配置选项,特别是如何使用HAProxy预定义(内置)的ACL进行流量控制和路由分发。通过实战案例,展示了如何利用内置ACL如HTTP_1.1和TRUE结合自定义ACL来匹配请求并分配到不同的后端服务器,以实现复杂的流量管理策略。
53 11
HAProxy的高级配置选项-haproxy预定义(内置)acl使用案例
|
2月前
|
运维 Apache
HAProxy的高级配置选项-自定义错误页面
这篇文章介绍了如何在HAProxy中配置自定义错误页面,通过修改配置文件指定不同HTTP状态码对应的错误页面路径,并展示了在后端服务不可用时如何向用户展示友好的错误提示。
61 7
HAProxy的高级配置选项-自定义错误页面
|
2月前
|
Apache 数据安全/隐私保护
HAProxy的高级配置选项-ACL篇之基于浏览器匹配制案例
这篇文章介绍了HAProxy的ACL(访问控制列表)功能,特别是如何基于用户代理(User-Agent)即浏览器类型进行匹配和流量分发的高级配置选项,并通过实战案例展示了如何配置ACL规则以实现基于不同浏览器的访问控制。
54 5
HAProxy的高级配置选项-ACL篇之基于浏览器匹配制案例
|
2月前
|
Apache 数据安全/隐私保护
HAProxy的高级配置选项-ACL篇之基于源地址访问控制案例
这篇文章介绍了HAProxy的ACL(访问控制列表)功能,特别是如何基于源地址进行访问控制的高级配置选项,并通过实战案例展示了如何配置ACL规则以允许或阻止特定IP地址或IP范围的访问。
54 7
HAProxy的高级配置选项-ACL篇之基于源地址访问控制案例
|
2月前
HAProxy的高级配置选项-配置haproxy支持https协议及服务器动态上下线
文章介绍了如何配置HAProxy以支持HTTPS协议和实现服务器的动态上下线。
119 8
HAProxy的高级配置选项-配置haproxy支持https协议及服务器动态上下线