负载均衡有多种实现方法,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)算法自动删除不使用的缓存。