使用Haproxy實現讀寫分離

在前面實驗(https://blog.csdn.net/qq_35887546/article/details/104734977)的基礎上實現讀寫分離

1.配置haproxy服務器

在server1:

[root@server1 html]# 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
 65     acl url_static       path_end       -i .jpg .gif .png
 66     acl read method GET
 67     acl read method HEAD                #兩個read write 只用一個就行
 68     acl write method PUT
 69     acl write method POST
 70     #acl westos.org hdr_beg(host)       -i westos.org
 71     #acl 172.25.63.1 hdr_beg(host)      -i 172.25.63.1 
 72     #errorloc 403 http://172.25.63.1:8000
 73     use_backend app          if write                   #如果是寫的話就是動態頁面
 74     default_backend             static                  #默認爲靜態頁面
 75 #     redirect code 301 location http://www.westos.org if westos.org              #以westos.org訪問就自動重定向到www.we    stos.org
 76 #    redirect code 301 location http://www.westos.org if 172.25.63.1             #以172.25.63.1訪問就自動重定向到www.w    estos.org

2.配置後端服務器

在server2和server3做同樣的操作:

[root@server2 ~]# yum install php -y
[root@server2 ~]# cd /var/www/html/
[root@server2 html]# ls
index.html  index.php  upload_file.php	upload			#放入測試腳本以及文件目錄

[root@server2 html]# vim upload_file.php 
  5 && ($_FILES["file"]["size"] < 2000000))

[root@server2 html]# chmod 777 upload_file.php 
[root@server2 html]# systemctl restart httpd

3.測試

server1重啓haproxy後:

[root@server1 html]# systemctl restart haproxy

在客戶端瀏覽器輸入 172.25.63.1/index.php 進行寫測試:在這裏插入圖片描述

在這裏插入圖片描述
在客戶端瀏覽器輸入 172.25.63.1/images 進行讀測試:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章