Nginx+DNS負載均衡實現


負載均衡有多種實現方法,nginx、apache、LVS、F5硬件、DNS等。

DNS的負載均衡就是一個域名指向多個ip地址,客戶訪問的時候進行輪詢解析

操作方法,在域名服務商解析的DNS也可以是第三方DNS提供商 上添加多條A記錄

qq.com DNS解析

 

參考:

http://blog.csdn.net/cywosp/article/details/38017027

http://www.cnblogs.com/cuihongyu3503319/archive/2012/07/09/2583129.html

dns解析的弊端:

1:無法獲取解析的主機狀態

2:dns一般三大運行商做了N多節點解析,修改dns后會有一定時間的延遲

 

Nginx的負載均衡

 Nginx的負載就是多個主機之間進行負載解析

分配方式:

nginx 的 upstream目前支持 4 種方式的分配 
1)、輪詢(默認) 
      每個請求按時間順序逐一分配到不同的后端服務器,如果后端服務器down掉,能自動剔除。 
2)、weight 
      指定輪詢幾率,weight和訪問比率成正比,用於后端服務器性能不均的情況。 
2)、ip_hash 
      每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個后端服務器,可以解決session的問題。  
3)、fair(第三方) 
      按后端服務器的響應時間來分配請求,響應時間短的優先分配。  
4)、url_hash(第三方)

配置:

在http節點里添加:

#定義負載均衡設備的 Ip及設備狀態 

upstream myServer {   

    server 127.0.0.1:9090 down; 
    server 127.0.0.1:8080 weight=2; 
    server 127.0.0.1:6060; 
    server 127.0.0.1:7070 backup; 
}

在需要使用負載的Server節點下添加

proxy_pass http://myServer;

upstream 每個設備的狀態:

down 表示單前的server暫時不參與負載 
weight  默認為1.weight越大,負載的權重就越大。 
max_fails :允許請求失敗的次數默認為1.當超過最大次數時,返回proxy_next_upstream 模塊定義的錯誤 
fail_timeout:max_fails 次失敗后,暫停的時間。 
backup: 其它所有的非backup機器down或者忙的時候,請求backup機器。所以這台機器壓力會最輕。

 

 

操作


這里我們設置的是dns解析和一台nginx的負載均衡(非兩台nginx互做解析),在A主機nginx.conf之前設置不變的情況下,新增多個端口對應之前域名

我們非兩台nginx互做解析是因為A配置強勁,且已經上線運行。不影響A的情況下,用B做一個當A不工作時的負載。

################  A主機原域名設置 #########################################

server {
        listen       80 ;
        server_name  yiiui.com;
        root   "E:/www/yiiui/backend/web";
        
        location / {
            index  index.html index.htm index.php;
            #autoindex  on;
            if (!-e $request_filename){
                rewrite ^/(.*) /index.php?r=$1 last;
            }
        }
        location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
}


server {
        listen       80;
        server_name  m.yiiui.com ;
        root   "E:/www/yiiui/myweb/web";
        location / {
            index  index.html index.htm index.php;
            #autoindex  on;
            if (!-e $request_filename){
                rewrite ^/(.*) /index.php?r=$1 last;
            }
        }
        
        location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9001;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
}

################  新增 nginx負載均衡端口域名  81  82  .... #########################################


server {
        listen       81 ;
        server_name  yiiui.com;
        root   "E:/www/yiiui/backend/web";
        
        location / {
            index  index.html index.htm index.php;
            #autoindex  on;
            if (!-e $request_filename){
                rewrite ^/(.*) /index.php?r=$1 last;
            }
        }
        location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
}


server {
        listen       82;
        server_name  m.yiiui.com ;
        root   "E:/www/yiiui/myweb/web";
        location / {
            index  index.html index.htm index.php;
            #autoindex  on;
            if (!-e $request_filename){
                rewrite ^/(.*) /index.php?r=$1 last;
            }
        }
        
        location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9001;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
}

 

 

B主機的nginx.conf中新增同A主機一樣的帶端口設置 (如果為內網局域網先設置hosts)

server {
        listen       81;
        server_name  yiiui.com;
        root   "C:/www/yiiui/backend/web";
        
        location / {
            index  index.html index.htm index.php;
            #autoindex  on;
            if (!-e $request_filename){
                rewrite ^/(.*) /index.php?r=$1 last;
            }
        }
        location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
}

server {
        listen       82;
        server_name  m.yiiui.com;
        root   "C:/www/yiiui/myweb/web";
        
        location / {
            index  index.html index.htm index.php;
            #autoindex  on;
            if (!-e $request_filename){
                rewrite ^/(.*) /index.php?r=$1 last;
            }
        }
        location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
}

設置B主機的負載域名:

upstream yiiui.com{
    server 127.0.0.1:81;  #這里是你自己要做負載均衡的服務器地址1    # 本地windows可以,外網linux 要用局域網的IP
    server 192.168.2.101:81; #這里是要參與負載均衡的地址2
}


upstream m.yiiui.com{
    server 127.0.0.1:82 weight=200;  #這里是你自己要做負載均衡的服務器地址1
    server 192.168.2.101:82 weight=2; #這里是要參與負載均衡的地址2
}







server {
        listen       80;
        server_name  yiiui.com;     #設置所有web服務器負載的共同域名 
        location / {
            proxy_pass http://yiiui.com;  #確定需要代理的URL,端口或socket。 
            proxy_set_header Host   $host;
        }
}

server {
        listen       80;
        server_name  m.yiiui.com;     #設置所有web服務器負載的共同域名 
        location / {
            proxy_pass http://m.yiiui.com;  #確定需要代理的URL,端口或socket。 
            proxy_set_header Host   $host;
        }
}

 

參考:

http://www.cnblogs.com/xiaogangqq123/archive/2011/03/04/1971002.html

http://825536458.blog.51cto.com/4417836/1784794

http://baidutech.blog.51cto.com/4114344/1033718/

設置nginx的負載均衡后的session同步登錄狀態問題

必須要將A、B主機的session指向一個地方才能使訪問的域名當前登錄的狀態一致。可以使用mysql、memcache進行會話存儲

windows的memcached 同時運行局域網ip訪問

Yii2 的設置:

# 設置session保存在mysql中
         'session' => [
            'class' => 'yii\web\DbSession',
            // Set the following if you want to use DB component other than
            // default 'db'.
            // 'db' => 'mydb',
            // To override default session table, set the following
            // 'sessionTable' => 'my_session',
        ],

參考: 

http://wiki.jikexueyuan.com/project/yii-2.0-guide/tutorial-performance-tuning.html

http://blog.sina.com.cn/s/blog_8a18c33d01013rp9.html

注:memcache存儲會話時,重啟、重啟操作系統會導致全部數據消失。內容容量達到指定值之后,就基於LRU(Least Recently Used)算法自動刪除不使用的緩存。

 


免責聲明!

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



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