<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 12. 作為負載均衡器 #### 1. 介紹 眾所周知,nginx是以高并發和內存占用少出名,它是一個http服務器,也是反向代理服務器,它更是負載均衡器。作為負載均衡器,在版本1.9之前,它只能作為http的負載均衡,也就是在網絡模型的第七層發揮作用,1.9之后,它可以對tcp進行負載均衡,比如redis,mysql等。 nginx的負載均衡是出了名的簡單,它跟反向代理的功能是緊密結合在一起的。比如下面是我網站上的一段配置: ``` upstream rails365 { # Path to Unicorn SOCK file, as defined previously server unix:///home/yinsigan/rails365/shared/tmp/sockets/unicorn.sock fail_timeout=0; } server { listen 80 default_server; # listen [::]:80 default_server ipv6only=on; server_name www.rails365.net; root /home/yinsigan/rails365/current/public; keepalive_timeout 70; location ~ ^/assets/ { gzip_static on; expires max; add_header Cache-Control public; # add_header ETag ""; # break; } try_files $uri/index.html $uri @rails365; location @rails365 { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; proxy_pass http://rails365; } # redirect server error pages to the static page /50x.html error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } ``` 我在服務器上部署的是ruby on rails項目,用unicorn來跑ruby的代碼,它是監聽在一個unix socket上的,也就是`unix:///home/yinsigan/rails365/shared/tmp/sockets/unicorn.sock`這個文件,`proxy_pass http://rails365;`是反向代理到上游服務器,也就是unicorn的部分,`upstream`這里指的就是上游服務器的部分。 #### 2. 使用 要實現負載均衡很簡單,我在部署多一個unicorn進程,監聽在另外的unix socket上,就等于多了一臺服務器,只需這樣做: ``` upstream rails365 { # Path to Unicorn SOCK file, as defined previously server unix:///home/yinsigan/rails365/shared/tmp/sockets/unicorn.sock; server unix:///home/yinsigan/rails365_cap/shared/tmp/sockets/unicorn.sock; } ``` 很簡單,只要用`server`指令添加多一行服務器就可以了。 現在有兩個上游服務器了,以前是一個,那么是如何以什么樣的方式訪問這兩個上游服務器的呢。 默認情況下,如果不指定方式,就是隨機輪循(round-robin)。兩個socket被不能地隨機訪問,這點可以通過監控日志看到的。 #### 3. 參數講解 接下來,我們來講一下nginx負載均衡在使用和配置上的一些參數。 上面有說過一個參數叫`round-robin`的,除了它之外,還有其他的幾個。 ##### 3.1 least\_conn 它是優先發送給那些接受請求少的,目的是為了讓請求分發得更平衡些。 ``` upstream rails365 { least_conn; server unix:///home/yinsigan/rails365/shared/tmp/sockets/unicorn.sock; server unix:///home/yinsigan/rails365_cap/shared/tmp/sockets/unicorn.sock; } ``` ##### 3.2 ip\_hash `ip_hash`可以記錄請求來源的ip,如果是同一個ip,下次訪問的時候還是會到相同的主機,這個可以略微解決那種帶cookie,session的請求的一致性問題。 ``` upstream rails365 { ip_hash; server unix:///home/yinsigan/rails365/shared/tmp/sockets/unicorn.sock; server unix:///home/yinsigan/rails365_cap/shared/tmp/sockets/unicorn.sock; } ``` ##### 3.3 hash 上面ip\_hash參數所設置的是根據相同的ip訪問相同的主機,這種是根據ip地址,還有一種粒度更小的控制,可以通過任何變量來控制。 比如下面的例子就是通過請求地址($request\_uri)來控制。 ``` upstream backend { hash $request_uri consistent; server unix:///home/yinsigan/rails365/shared/tmp/sockets/unicorn.sock; server unix:///home/yinsigan/rails365_cap/shared/tmp/sockets/unicorn.sock; } ``` ##### 3.4 down 假如有一臺主機是出了故障,或者下線了,要暫時移出,那可以把它標為down,表示請求是會略過這臺主機的。 ``` upstream rails365 { server unix:///home/yinsigan/rails365/shared/tmp/sockets/unicorn.sock; server unix:///home/yinsigan/rails365_cap/shared/tmp/sockets/unicorn.sock down; } ``` ##### 3.5 backup `backup`是指備份的機器,相對于備份的機器來說,其他的機器就相當于主要服務器,只要當主要服務器不可用的時候,才會用到備用服務器。 ``` upstream rails365 { server unix:///home/yinsigan/rails365/shared/tmp/sockets/unicorn.sock; server unix:///home/yinsigan/rails365_cap/shared/tmp/sockets/unicorn.sock backup; } ``` ##### 3.6 weight `weight`指的是權重,默認情況下,每臺主機的權重都是1,也就是說,接收請求的次數的比例是一樣的。但我們可以根據主機的配置或其他情況自行調節,比如,對于配置高的主機,可以把`weight`值調大。 ``` upstream myapp1 { server srv1.example.com weight=3; server srv2.example.com; server srv3.example.com; } ``` 假如有五個請求,srv1可能會得到三個,其他的兩臺服務器,可能分別得到1個。 ##### 3.7 max\_fails和fail\_timeout 默認情況下,max\_fails的值為1,表示的是請求失敗的次數,請求1次失敗就換到下臺主機。 另外還有一個參數是fail\_timeout,表示的是請求失敗的超時時間,在設定的時間內沒有成功,那作為失敗處理。 ``` upstream rails365 { server unix:///home/yinsigan/rails365/shared/tmp/sockets/unicorn.sock max_fails=2; server unix:///home/yinsigan/rails365_cap/shared/tmp/sockets/unicorn.sock backup; } ``` 那什么情況才叫請求失敗呢?有可能是服務器內部錯誤,超時,無效的頭部,或返回500以上的狀態碼的時候。 完結。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看