nginx的讀寫分離

來源:互聯網
上載者:User

標籤: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的讀寫分離

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.