一般網站都是用rsync+inotify實作檔案同步,而rsync+inotify并不能雙向同步,是以這個時候我們就要使用到讀寫分離。拓撲:nginx:192.168.137.50:80
後端web:apache1:192.168.137.51:80
apache2:192.168.137.52:80
這裡我們使用nginx作為反向代理,使用if語句和$request_method子產品實作讀寫分離,
配置如下:
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://192.168.137.51/;
if ( $request_method = "PUT"){
proxy_pass http://192.168.137.52;
}
index index.html index.htm;
}
當$request_method為PUT,我們将請求轉發給隻做寫的web端。
重新開機nginx,使用curl測試:
[root@syk conf]# curl http://192.168.137.50
<h1>2.syk.com</h1>
讀取隻被轉發到了192.168.137.51端;
開啟192.168.137.52端apache的上傳子產品:
vim /etc/httpd/conf/httpd.conf
在<Directory "/var/www/html">下添加:
dav on即可
測試:
[root@syk conf]# curl -T /etc/passwd http://192.168.137.50
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /passwd
on this server.</p>
<hr>
<address>Apache/2.2.15 (CentOS) Server at 192.168.137.52 Port 80</address>
</body></html>
出現403,是因為apache目錄屬組為root,是以這裡做修改:
setfacl -m u:apache:rwx /var/www/html/
再次測試:
<title>201 Created</title>
<h1>Created</h1>
<p>Resource /passwd has been created.</p>
<hr />
并且我們可以看到,檔案被上傳到了192.168.137.52端。
本文轉自 sykmiao 51CTO部落格,原文連結:http://blog.51cto.com/syklinux/1837046,如需轉載請自行聯系原作者