标签: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作为反向代理,使用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>
[root@syk conf]# curl http://192.168.137.50
<h1>2.syk.com</h1>
[root@syk conf]# curl http://192.168.137.50
<h1>2.syk.com</h1>
[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/
再次测试:
[root@syk 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的读写分离、$request_method模块
原文地址:http://syklinux.blog.51cto.com/9631548/1837046