在前面實驗(https://blog.csdn.net/qq_35887546/article/details/104731132)的基礎上實現動靜分離
實驗流程如下:
1.修改配置文件
在server1:
[root@server1 ~]# vim /etc/haproxy/haproxy.cfg
61 # main frontend which proxys to the backends
62 #---------------------------------------------------------------------
63 frontend westos *:80 #編輯前端和後端策略
64 acl url_static path_beg -i /images #表示url地址以/images開頭時即認爲時靜態地址
65 acl url_static path_end -i .jpg .gif .png #表示url地址以.jpg .gif .png 結尾時即認爲時靜態地址
66
67 use_backend static if url_static
68 default_backend app #其他默認爲動態地址
69
70 #---------------------------------------------------------------------
71 # static backend for serving up images, stylesheets and such
72 #---------------------------------------------------------------------
73 backend static #編輯靜態策略
74 # balance roundrobin
75 server static 172.25.63.3:80 check #指定server3爲靜態頁面服務器
76
77 ##---------------------------------------------------------------------
78 # round robin balancing between the various backends
79 #---------------------------------------------------------------------
80 backend app #編輯動態策略
81 balance roundrobin
82 server web1 172.25.63.2:80 check
83 server web2 172.25.63.1:8000 check
84 # server app3 127.0.0.1:5003 check
85 # server app4 127.0.0.1:5004 check
86
87 listen admin *:8080
88 stats enable
89 stats uri /status #監控頁面地址
90 stats auth admin:westos #管理帳號和密碼
91 stats refresh 5s #刷新頻率
92
93 #listen westos *:80 #監聽的實例名稱,地址和端口
94 # balance roundrobin #負載均衡算法
95 # server web1 172.25.63.2:80 check
96 # server web2 172.25.63.3:80 check
2.配置動態頁面服務器server1
將server1配置爲另外一臺apache服務器:
[root@server1 ~]# yum install httpd -y
[root@server1 ~]# cd /var/www/html/
[root@server1 html]# vim index.html
server1
[root@server1 html]# vim /etc/httpd/conf/httpd.conf #由於server1同樣也是haproxy代理服務器,爲了防止端口衝突,將server1的htppd端口改爲8000
42 Listen 8000
[root@server1 html]# systemctl start httpd #注意,selinux必須關閉,要不然啓動會報錯
3.配置靜態頁面服務器server3
在sercer3apache發佈目錄放入測試文件:
[root@server3 html]# pwd
/var/www/html
[root@server3 html]# mkdir images
[root@server3 html]# cd images/
[root@server3 images]# ls
123.jpg
4.測試
首先在server1重啓haproxy服務:
[root@server1 html]# systemctl restart haproxy.service
測試動態頁面
在客戶端:
[root@foundation63 ~]# curl 172.25.63.1
server2
[root@foundation63 ~]# curl 172.25.63.1
server1
[root@foundation63 ~]# curl 172.25.63.1
server2
[root@foundation63 ~]# curl 172.25.63.1
server1
說明動態頁面測試成功
測試靜態頁面
在客戶端瀏覽器訪問http://172.25.63.1/images/
:
以上實驗結果說明使用haproxy成功實現動靜分離。