Nginx for read and write separation

Source: Internet
Author: User

Nginx for read and write separation

Test environment

Os:rhel6

A server: Configuring Nginx for load balancing and directional proxy functions

B Server: Simple Web server, no other configuration, read server

C Server: Simple Web server, no other configuration, write server

Deployment: A server to achieve front-end load balancing and directional proxy functions, the backend has two Web servers, b server only read operations, C Server only write operations, and B and C server through the rsync+inotify to achieve data synchronization so simple to achieve the Nginx read and write separation function.

Before configuring the configuration, let's look at what is WebDAV??

WebDAV (web-based Distributed Authoring and Versioning) based on Http1.1 protocol. It extends HTTP 1.1, adding new methods beyond the Get, POST, head, and other HTTP standard methods, enabling applications to directly read and write to the Web server and support write file locking (Locking) and Unlock (Unlock). You can also support version control for files.

Web Distributed Authoring and Versioning (WebDAV) extends the http/1.1 protocol, allowing clients to publish, lock, and manage resources on the web.

1. Configure load balancing and directional proxy functionality on a server

#vim/etc/nginx/nginx.conf

Proxy_cache_path/nginx/cache/first levels=1:2 keys_zone=first:20m max_size=1g;
# # #读服务器组 B
Upstream Read {
#ip_hash;
Server 192.168.20.121 weight=2 max_fails=2 fail_timeout=2;
Server 192.168.20.123 weight=2 max_fails=2 fail_timeout=2;
}
# # #写服务器组 A
Upstream Write {
#ip_hash;
Server 192.168.20.122 weight=2 max_fails=2 fail_timeout=2;
Server 192.168.20.124 weight=2 max_fails=2 fail_timeout=2;
Server 127.0.0.1:8080 backup;
}

server {
Location/{
root HTML;
Index index.html index.htm;
Proxy_cache first;
Proxy_cache_valid 10m;
Proxy_pass Http://read;
if ($request _method = "POST") {# # # #在这里判断用户是否执行的是写操作
Proxy_pass Http://write;
}
Proxy_set_header Host $host;
Proxy_set_header X-real-ip $remote _addr;
}
}

Restarting the service on a 2.A server

#service nginx Restart

3. Enable the httpd WebDAV feature on the B,C server

#vim/etc/httpd/conf/httpd.conf

Dav on # # # #在 <directory "/var/www/html/" > tags added

4. Restart the httpd service on the B,C server

#service httpd Retstart

5. Test the Web service on the B,C server with the Curl command on the a server for normal access

#curl http://192.168.20.121

6. Use the T parameter of the Curl command to implement the Put method in the HTTP protocol upload file (a server)

#curl-T/etc/issue http://192.168.20.121 # # #往读服务器上传文件会报405的错误 because WebDAV is not enabled

#curl-T/etc/issue http://192.168.20.122 # # #往写服务器上传文件会报403的错误, not enough access rights

7. Below we give Apache user authorization

#setfacl-M u:apache:rwx/var/www/html/

8. Finally re-test to see if you can upload files to the writing server

#curl-T/etc/issue http://192.168.20.122

9. Verify that the upload is successful

#cd/var/www/html


This article is from "Luo Chen's blog" blog, please be sure to keep this source http://luochen2015.blog.51cto.com/9772274/1699201

Nginx for read and write separation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.