How to configure nginx read-write separation

WBOY
Release: 2023-05-18 15:19:57
forward
1439 people have browsed it

nginx read-write separation

1. Experimental topology

How to configure nginx read-write separation

Requirements analysis, front-end done by nginx Load balancing reverse proxy, two httpd servers behind. The entire architecture is to provide bbs (forum) services. There is a need to achieve separation of reading and writing, which is the function of uploading attachments. The attachments we upload can only be uploaded to web1, and then rsync inotify is used to synchronize attachments on web1. Everyone knows rsync inotify. It can only be master-to-slave synchronization, not bidirectional synchronization. Therefore, web1 can perform write operations, while web2 can only perform read operations, which brings about the need for separation of reading and writing. Let's talk about how to achieve separation of reading and writing.

2.webdav function description

webdav (web-based distributed authoring and versioning) is a communication protocol based on the http 1.1 protocol. It extends http 1.1 and adds some new methods in addition to several http standard methods such as get, post, and head, so that applications can directly read and write to the web server, and supports writing file locking and unlocking. ), and can also support file version control. In this way, we can configure the read-write separation function. Let’s configure it in detail.

3. Modify the configuration file

[root@nginx nginx]# vim /etc/nginx/nginx.conf
server {
    listen    80;
    server_name localhost;
    #charset koi8-r;
    #access_log logs/host.access.log main;
    location / {
        proxy_pass http://192.168.18.202;
        if ($request_method = "put"){
            proxy_pass http://192.168.18.201;
        }
    }
}
Copy after login

4. Reload the configuration file

[root@nginx ~]# service nginx reload
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
重新载入 nginx:                      [确定]
Copy after login

5. Configure the webdav function of httpd

[root@web1 ~]# vim /etc/httpd/conf/httpd.conf
Copy after login

How to configure nginx read-write separation

Note, just enable it under .

6. Restart httpd

[root@web1 ~]# service httpd restart
停止 httpd:                        [确定]
正在启动 httpd:                      [确定]
Copy after login

7. Test it

[root@nginx ~]# curl http://192.168.18.201
<h1>web1.test.com</h1>
[root@nginx ~]# curl http://192.168.18.202
<h1>web2.test.com</h1>
Copy after login

Note, there is no problem accessing web1 and web2.

[root@nginx ~]# curl -t /etc/issue http://192.168.18.202
<!doctype html public "-//ietf//dtd html 2.0//en">
<html><head>
<title>405 method not allowed</title>
</head><body>
<h1>method not allowed</h1>
the requested method put is not allowed for the url /issue.
<hr>
<address>apache/2.2.15 (centos) server at 192.168.18.202 port 80</address>
</body></html>
Copy after login

Note, when we upload files to web2, because web2 only has human-readable functions, it does not have the webdav function to open an account, so the display is 405 method not allowed.

[root@nginx ~]# curl -t /etc/issue http://192.168.18.201
<!doctype html public "-//ietf//dtd html 2.0//en">
<html><head>
<title>403 forbidden</title>
</head><body>
<h1>forbidden</h1>
you don&#39;t have permission to access /issue
on this server.
<hr>
<address>apache/2.2.15 (centos) server at 192.168.18.201 port 80</address>
</body></html>
Copy after login

Note, we have enabled the webdav function in web1, but our directory is the root directory and apache users are not allowed to upload, so 403 forbidden is displayed. Next we authorize apache to allow uploading.

[root@web1 ~]# setfacl -m u:apache:rwx /var/www/html/
Copy after login

Let’s test it again,

[root@nginx ~]# curl -t /etc/issue http://192.168.18.201
<!doctype html public "-//ietf//dtd html 2.0//en">
<html><head>
<title>201 created</title>
</head><body>
<h1>created</h1>
resource /issue has been created.
<hr />
<address>apache/2.2.15 (centos) server at 192.168.18.201 port 80</address>
</body></html>
Copy after login

Note, you can see that we successfully uploaded the file, indicating that the nginx read-write separation function configuration is complete. Finally, let's take a look at the uploaded file.

[root@web1 ~]# cd /var/www/html/
[root@web1 html]# ll
Copy after login

Total dosage 12

drwxr-xr-x 2 root  root  4096 9月  4 13:16 forum
-rw-r--r-- 1 root  root   23 9月  3 23:37 index.html
-rw-r--r-- 1 apache apache  47 9月  4 14:06 issue
Copy after login

The above is the detailed content of How to configure nginx read-write separation. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!