nginx七層負載均衡

        準備多臺虛擬機
環境準備:
          關閉防火牆和selinux   systemctl  stop  firewalld  &&  setenforce 0
          設置永久關閉       systemctl  disable  firewalld  
                                 vim  /etc/selinux
兩臺虛擬機做靜態頁面,兩臺做動態頁面,一臺做nginx反向代理,一臺做測試
:最好做好dns解析
做靜態頁面:
HTML  A  &  HTML   B
root@html1 ~]#   yum  -y  install nginx
創建測試頁面index.html,開啓服務

PHP   A  &  PHP  B
[root@php1 ~]#   yum  -y  install  httpd  php
創建測試頁面index.php,開啓服務

安裝Nginx
[root@php1 ~]# yum  -y  install nginx
修改配置文件
 定義主機集羣
[root@nginx ~]#  vim /etc/nginx/nginx.conf
http {
    upstream htmlservers {
       server 192.168.1.3:80 weight=1 max_fails=2 fail_timeout=2;
       server 192.168.1.4:80 weight=2 max_fails=2 fail_timeout=2;
        }
       
upstream phpservers {
       server 192.168.1.3:80 weight=1 max_fails=2 fail_timeout=2;
       server 192.168.1.4:80 weight=2 max_fails=2 fail_timeout=2;
        }
}
訪問頁面(使用之前定義的主機集羣)
方法一:
server {
      location / {
       root /usr/share/nginx/html;
       index index.html index.htm;
       if ($request_uri  ~*  \.html$) {
       proxy_pass http://htmlserver;
        }
       if ($request_uri  ~*  \.php$)  {
       proxy_pass http://phpserver;
        }
       }
     }
方法二:
server {
      location ~* \.html$ {
      proxy_pass http://htmlservers;
      }
     
      location ~* \.php$ {
      proxy_pass http://phpservers;
      }
}
:此處做測試的是動靜分離

在客戶端訪問 Nginx 測試
[root@localhost ~]# elinks –dump http:// nginx/index.php
[root@localhost ~]# elinks –dump http:// nginx/index.html

upstream支持的負載均衡算法
輪詢(默認)(rr)(round  robin)(輪流分流量)
           可以使用weight指定權重,權重越大,被調度的次數越多(權重用數字表示,可以任意數字)
           rr(普通輪詢)
           wrr(權重輪詢)
       例子:
                  upstream httpservers {
                           server 192.168.1.3:80 weight=1 max_fails=2 fail_timeout=2;
                           server 192.168.1.4:80 weight=2 max_fails=2 fail_timeout=2;
                           server 192.168.1.5:80 weight=2 max_fails=2 fail_timeout=2;
                           server 192.168.1.100:80 backup;
                   }  
ip_hash     根據請求的ip調度,可以解決session的問題,不能使用weight  (每有一個ip訪問,分配一臺服務器)
fail: 可以根據請求頁面的大小和加載時間長短進行調度,使用第三方的upstream_fair模塊
url_hash: 按請求的url的hash進行調度,從而使每個url定向到同一服務器,使用第三方的hash模塊

upstream支持的狀態參數
down:       暫定對該服務器進行調度(相當於註釋)
backup:     類似與LVS  Sorry Server,當所有的非backup的服務器故障才使用,平時不使用
max_fails:   請求失敗的次數,默認爲1
fail_timeout:   在經歷max_fail次失敗後,服務器暫停服務的時間
示例:
           upstream tianyun.com {
           #      ip_hash;
                   server 192.168.10.137 weight=1 max_fails=2 fail_timeout=2;
                   server 192.168.10.20   weight=2 max_fails=2 fail_timeout=2;
                   server 192.168.10.251 max_fails=2 fail_timeout=5 down;
                   server 192.168.10.253 backup;
               }
               
proxy_next_upstream:這個指令屬於 http_proxy 模塊的,指定後端返回什麼樣的異常響應時,使用另一個realserver(即將請求傳遞到下一個服務器)
示例:
           location  / {
               proxy_pass  
http://httpservers;
               proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
          }
 
Apache LogFormat 可選(日誌格式)
LogFormat "%{X-Real-IP}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent

根據站點分區進行調度
http {
   upstream news {
       server 192.168.1.11:80 weight=1 max_fails=2 fail_timeout=2;
       server 192.168.1.12:80 weight=2 max_fails=2 fail_timeout=2;
       server 192.168.1.13:80 weight=2 max_fails=2 fail_timeout=2;
      }
     
   upstream milis {
       server 192.168.1.21:80 weight=1 max_fails=2 fail_timeout=2;
       server 192.168.1.22:80 weight=2 max_fails=2 fail_timeout=2;
       server 192.168.1.23:80 weight=2 max_fails=2 fail_timeout=2;
      }
     
    upstream videos {
       server 192.168.1.31:80 weight=1 max_fails=2 fail_timeout=2;
       server 192.168.1.32:80 weight=2 max_fails=2 fail_timeout=2;
       server 192.168.1.33:80 weight=2 max_fails=2 fail_timeout=2;
      }
     
    upstream images {
       server 192.168.1.41:80 weight=1 max_fails=2 fail_timeout=2;
       server 192.168.1.42:80 weight=2 max_fails=2 fail_timeout=2;
       server 192.168.1.43:80 weight=2 max_fails=2 fail_timeout=2;
      }
     
     upstream others {
       server 192.168.1.51:80 weight=1 max_fails=2 fail_timeout=2;
       server 192.168.1.52:80 weight=2 max_fails=2 fail_timeout=2;
       server 192.168.1.53:80 weight=2 max_fails=2 fail_timeout=2;
      }
     
    server {
          location / {
      proxy_pass http://others;
      }
     
      location /news {
      proxy_pass http://news;
      }
     
      location /mili {
      proxy_pass http://milis;
      }
     
      location ~* \.(wmv|mp4|rmvb)$ {
      proxy_pass http://videos;
      }
     
      location ~* \.(png|gif|jpg)$ {
      proxy_pass http://images;
      }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章