標籤:nginx的讀寫分離、$request_method模組
一般網站都是用rsync+inotify實現檔案同步,而rsync+inotify並不能雙向同步,所以這個時候我們就要使用到讀寫分離。拓撲:nginx:192.168.137.50:80
後端web:apache1:192.168.137.51:80
apache2:192.168.137.52:80
這裡我們使用nginx作為反向 Proxy,使用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測試:
[[email protected] conf]# curl http://192.168.137.50
<h1>2.syk.com</h1>
[[email protected] conf]# curl http://192.168.137.50
<h1>2.syk.com</h1>
[[email protected] conf]# curl http://192.168.137.50
<h1>2.syk.com</h1>
[[email protected] 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即可
測試:
[[email protected] 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/
再次測試:
[[email protected] conf]# curl -T /etc/passwd http://192.168.137.50
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>201 Created</title>
</head><body>
<h1>Created</h1>
<p>Resource /passwd has been created.</p>
<hr />
<address>Apache/2.2.15 (CentOS) Server at 192.168.137.52 Port 80</address>
</body></html>
並且我們可以看到,檔案被上傳到了192.168.137.52端。
本文出自 “Linux” 部落格,請務必保留此出處http://syklinux.blog.51cto.com/9631548/1837046
nginx的讀寫分離