实验环境
继HAProxy负载均衡一文,详情点击这---->点我查看详情
server1 172.25.64.1 haproxy 企业六
server2 172.25.64.2 httpd 企业六
server3 172.25.64.3 httpd 企业六
客户端 172.25.64.250
一、动静分离
server1(haproxy端)
[root@server1 mnt]# vim /etc/haproxy/haproxy.cfg
#################整个文件############
加了##的是重点改动的!
global
maxconn 10000
stats socket /var/run/haproxy.stat mode 600 level admin
log 127.0.0.1 local0
uid 200
gid 200
chroot /var/empty
daemon
defaults ##
mode http
# option prefer-last-server
retries 2
# option redispatch
timeout connect 5s
timeout server 5s
# The public 'www' address in the DMZ
frontend public
bind *:80 name clear
#bind 192.168.1.10:443 ssl crt /etc/haproxy/haproxy.pem
mode http
log global
option httplog
option dontlognull
monitor-uri /monitoruri
maxconn 8000
timeout client 30s
stats uri /admin/stats
# use_backend static if { hdr_beg(host) -i img }
use_backend dynamic if { path_end php } ##动态访问backend dynamic
default_backend static
# The static backend backend for 'Host: img', /img and /css.
backend static
balance roundrobin
# option httpchk HEAD /favicon.ico
server server2 172.25.64.2:80 check inter 1000
# the application servers go here
backend dynamic
balance roundrobin
timeout queue 30s
# option httpchk HEAD /login.php
# cookie DYNSRV insert indirect nocachei
server server3 172.25.64.3:80 check inter 1000 ##动态服务器
fullconn 4000 # the servers will be used at full load above this number of connections
# server dynsrv1 192.168.1.1:80 minconn 50 maxconn 500 cookie s1 check inter 1000
[root@server1 mnt]# /etc/init.d/haproxy restart
server3(web服务器)
[root@server3 html]# yum install php -y
[root@server3 html]# ls
index.php #动态
[root@server3 html]# cat index.php
<?php
phpinfo();
?>
[root@server3 html]# /etc/init.d/httpd restart
测试
静态:
动态:
二、读写分离
server1(haproxy端)
[root@server1 mnt]# vim /etc/haproxy/haproxy.cfg
##################添加########################
44 acl write method POST
45 acl write method PUT
46
47 acl read method GET
48 acl read method HEAD
49
50 use_backend dynamic if write
51
52 default_backend static
[root@server1 mnt]# /etc/init.d/haproxy restart
server2(web服务器)
[root@server2 html]# yum install php -y
[root@server2 html]# ls
index.php upload_file.php #编写这两个文件
[root@server2 html]# chmod 644 *
[root@server2 html]# mkdir upload
[root@server2 html]# chmod 777 upload/
[root@server2 html]# /etc/init.d/httpd restart
[root@server2 upload]# scp -rp /var/www/html/* root@172.25.64.3:/var/www/html/
#将/var/www/html/下的所有文件传到server3的/var/www/html/下
[root@server2 html]# cat index.php #index.php里的内容
##################################################################
<html>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" /> #注意这,一会儿为了视觉效果会改
</form>
</body>
</html>
################################################################
[root@server2 html]# cat upload_file.php #upload_file.php里的内容
###############################################################
<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 2000000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
###############################################################
server3(web服务器)
[root@server3 html]# yum install php -y
[root@server3 html]# ls
index.php upload upload_file.php
[root@server3 html]# /etc/init.d/httpd restart
为了观看效果,在server2上微改:
[root@server2 html]# rm -fr upload
[root@server2 html]# ls
index.php upload_file.php
[root@server2 html]# vim index.php
<html>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit_server2_only_read" /> #此处改动
</form>
</body>
</html>
[root@server2 html]# /etc/init.d/httpd restart
测试
server3中的upload下啥都没有!
server2中没有upload!
浏览器访问server1:
有没有看到server2_only_read,读的时候访问的server2
上传了一个图片:
在server3中写入:
实现读写分离!