nginx實現負載均衡(實現http和https雙存負載均衡)


三台服務器,其中一台主機為nginx服務器A,另外兩台是應用服務器B和C

只需在主機nginx服務器 安裝nginx ,另外兩台無須安裝

A服務器ip為 192.168.1.3
B服務器ip為 192.168.1.1
C服務器ip為 192.168.1.2

nginx配置文件如下:

worker_processes  64;
events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    
    upstream  siteHttps { 
      ip_hash;
      server   192.168.1.1:8080 ;
      server   192.168.1.2:8080 ;
    }
    
    server {
        listen       8080;
        server_name  192.168.1.3;
        location / {
            root   html;
            index  index.html index.htm;    
        }
        
        location ^~ /oms/ {
        proxy_pass  http://siteHttps/oms/; 
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        
         }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
            
        }
  
    }
    
    #設定虛擬主機配置
    server {
    
        #偵聽443端口,這個是ssl訪問端口
        listen       443 ssl;  
        
        #定義使用 訪問域名
        server_name  192.168.1.3;
        
        #定義服務器的默認網站根目錄位置
        root /usr/share/nginx/html;
        
        ssl_certificate      /opt/my_key_store.crt;
        ssl_certificate_key    /opt/my_store.key;
        ssl_protocols        TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers          ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
        ssl_prefer_server_ciphers  on;

        
        location ^~ /oms/ {
            proxy_redirect http:// https://;
            proxy_set_header Host $host;
            client_max_body_size    100m;
            proxy_set_header Host $host:$server_port;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass  http://siteHttps/oms/; 
         }
    
       #默認請求
        location / {
            root   html;
            #定義首頁索引文件的名稱
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        
          #靜態文件,nginx自己處理
          location ~ ^/(images|javascript|js|css|flash|media|static)/ {
              #過期30天,靜態文件不怎么更新,過期可以設大一點,
              #如果頻繁更新,則可以設置得小一點。
              expires 30d;
          }

    }
    
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;    
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
            
        }
       }

}

 

其中ssl的證書,自己生成

 

 

--------------------------相關知識---------------------------

 

nginx負載均衡的策略

1.輪詢(默認方式) 
對於一級后端服務器群,形成一個環隊列的形式,對於每個到達的請求按時間順序順次分配給這些后端服務器。在前端調度器與后端服務器之間采用“心跳”方式進行狀態檢查,如果發現后端服務器宕機,則將其刪除。 

    這種方式為默認配置,優點是簡潔,但缺點是無法進行最優化調度,有可能有的請求需要耗時較久,這樣會帶來一定的不平衡。 
    它的例子:在http區域里添加: 
upstream lb { 
        server 10.10.57.122:80; 
        server 10.10.57.123:80; 

在你的某個server里增加: 
location / { 
              proxy_pass http://lb; 
        } 

2. 加權輪詢 
    這是一種對上述方式的改進,引入權值的概念,能夠解決后端服務器性能不均的情況。 

    例如這樣一個配置: 
    upstream lb { 
         server 10.10.57.122:80 weight=5; 
         server 10.10.57.123:80 weight=10; 
    } 

ps:以上輪詢負載均衡策略,我個人認為對於動態網站應用,這幾乎就是形同擺設,沒有人會采用。但一種情況例外:服務器端的session采用共享機制,如存儲在數據庫或者memcached內存里等。 
3. ip_hash(基於ip的hash分配策略) 
     這是一種非輪詢式方式,對於每個到達的請求,直接通過其請求IP進行哈希的映射,通過映射結果獲得那一台后端服務器要處理這個請求,這種方式有一個明顯的好處是能夠保證session的唯一性。 
    它的配置例子: 
upstream lb { 
        ip_hash; 
        server 10.10.57.122:80; 
        server 10.10.57.123:80; 

在你的某個server里增加: 
location / { 
              proxy_pass http://lb; 
        } 

    
This directive causes requests to be distributed between upstreams based on the IP-address of the client. 
The key for the hash is the class-C network address of the client. This method guarantees that the client request will always be transferred to the same server. But if this server is considered inoperative, then the request of this client will be transferred to another server. This gives a high probability clients will always connect to the same server. 

It is not possible to combine ip_hash and weight methods for connection distribution. If one of the servers must be removed for some time, you must mark that server as *down*. 

由這段英文解說知道,客戶端只要來自同一網段的ip的request都會轉發到相同的后端服務器上。這里的所謂的class-C network這里作者並沒有很詳細地解釋,我只能說,寫這句話的人不懂網絡。我個人的理解是:以ip地址的點分十進制格式的前3個字節進行hash。 
其實這不是真正意義上的ip address hash,而只是network address hash。真正的ip address hash方式有不?其實可以通過下面介紹的url_hash來實現。關鍵指令:hash $remote_addr; 
不過這里有個前提,$remote_addr必須是client的real ip address。 
為什么這里能夠實現真正意義上的ip address hash?很簡單,就是這里整個ip address被當作一個字符串來對待,故只要ip地址(key)不同,hash必然也是不同的。 

4. url_hash(基於URL的哈希方式) 
    這種方式與IP的哈希方式類似,是對客戶機請求的URL進行哈希操作,這樣的方式有一個明顯的好處是,能夠便於內容緩存的實現,對於經常性的資源訪問,采用這樣的方式會獲得非常好的質量。它目前不是nginx自帶的功能,需要安裝補丁方可使用。本指令的詳細說明和安裝見:(文章后面有附帶詳細安裝實例) 
http://wiki.nginx.org/HttpUpstreamRequestHashModule 

    它的配置方式為: 
   
upstream lb { 
         server 10.10.57.122:80; 
         server 10.10.57.123:80; 
         hash $request_uri; 
    } 

如果將這里的$request_uri換成$remote_addr便可實現上面我所說的真正基於ip地址的策略。 
5. 基於服務響應式 
   這種方式是根據服務器端的動態響應,對每一個請求進行分配。 這種方式能夠自動根據當前的后端實際負載來優化。 
   它的配置方式: 
upstream lb { 
         server 10.10.57.122:80; 
         server 10.10.57.123:80; 
         fair; 
    } 

這個沒怎么測試,只是配起來用了下,機器差不多的話感覺就是輪詢。 
=============================== 
cd nginx-0.7.67; 
patch -p0 < ../nginx_upstream_hash-0.3.1/nginx.patch 
configure時: 
./configure --prefix=/app/nginx -user=nobody -group=nobody \ 
--add-module=../nginx_upstream_hash-0.3.1/ \ 
--add-module=../gnosek-nginx-upstream-fair-2131c73/

 

相關知識摘自:

https://blog.csdn.net/wgluser/article/details/8023443?utm_medium=distribute.pc_relevant_bbs_down.none-task--2~all~first_rank_v2~rank_v28-12.nonecase&depth_1-utm_source=distribute.pc_relevant_bbs_down.none-task--2~all~first_rank_v2~rank_v28-12.nonecase

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM