負載均衡有多種實現方法,nginx、apache、LVS、F5硬件、DNS等。php
DNS的負載均衡就是一個域名指向多個ip地址,客戶訪問的時候進行輪詢解析html
操做方法,在域名服務商解析的DNS也能夠是第三方DNS提供商 上添加多條A記錄mysql
qq.com DNS解析linux
參考:nginx
http://blog.csdn.net/cywosp/article/details/38017027web
http://www.cnblogs.com/cuihongyu3503319/archive/2012/07/09/2583129.html算法
dns解析的弊端:sql
1:沒法獲取解析的主機狀態apache
2:dns通常三大運行商作了N多節點解析,修改dns後會有必定時間的延遲windows
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節點下添加 proxy_pass http://myServer; upstream 每一個設備的狀態: down 表示單前的server暫時不參與負載 |
操做
這裏咱們設置的是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)算法自動刪除不使用的緩存。