溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

nginx讀寫分離怎么配置

發(fā)布時間:2022-04-29 15:24:40 來源:億速云 閱讀:243 作者:iii 欄目:大數(shù)據(jù)

本文小編為大家詳細介紹“nginx讀寫分離怎么配置”,內(nèi)容詳細,步驟清晰,細節(jié)處理妥當(dāng),希望這篇“nginx讀寫分離怎么配置”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識吧。

nginx之讀寫分離

1.實驗拓撲

nginx讀寫分離怎么配置

需求分析,前端一臺nginx做負載均衡反向代理,后面兩臺httpd服務(wù)器。整個架構(gòu)是提供bbs(論壇)服務(wù),有一需求得實現(xiàn)讀寫分離,就是上傳附件的功能,我們上傳的附件只能上傳到web1,然后在web1上利用rsync+inotify實現(xiàn)附件同步,大家都知道rsync+inotify只能是主向從同步,不能雙向同步。所以web1可進行寫操作,而web2只能進行讀操作,這就帶來讀寫分離的需求,下面我們就來說一下,讀寫分離怎么實現(xiàn)。

2.webdav功能說明

webdav (web-based distributed authoring and versioning) 一種基于 http 1.1協(xié)議的通信協(xié)議。它擴展了http 1.1,在get、post、head等幾個http標(biāo)準(zhǔn)方法以外添加了一些新的方法,使應(yīng)用程序可直接對web server直接讀寫,并支持寫文件鎖定(locking)及解鎖(unlock),還可以支持文件的版本控制。這樣我們就能配置讀寫分離功能了,下面我們來具體配置一下。

3.修改配置文件

[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;
        }
    }
}

4.重新加載一下配置文件

[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:                      [確定]

5.配置httpd的webdav功能

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

nginx讀寫分離怎么配置

注,在<directory "/var/www/html">下啟用就行。

6.重新啟動一下httpd

[root@web1 ~]# service httpd restart
停止 httpd:                        [確定]
正在啟動 httpd:                      [確定]

7.測試一下

[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>

注,web1與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>

注,我們上傳文件到,web2上時,因為web2只人讀功能,所以沒有開戶webdav功能,所以顯示是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'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>

注,我們在web1開啟了webdav功能,但我們目錄是root目錄是不允許apache用戶上傳的,所以顯示的是403 forbidden。下面我們給apache授權(quán),允許上傳。

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

下面我們再來測試一下,

[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>

注,大家可以看到我們成功的上傳了文件,說明nginx讀寫分離功能配置完成。最后,我們來查看一下上傳的文件。

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

總用量 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

讀到這里,這篇“nginx讀寫分離怎么配置”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領(lǐng)會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI