负载均衡有多种实现方法,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不工作时的负载。

  1. ################ A主机原域名设置 #########################################
  2.  
  3. server {
  4. listen ;
  5. server_name yiiui.com;
  6. root "E:/www/yiiui/backend/web";
  7.  
  8. location / {
  9. index index.html index.htm index.php;
  10. #autoindex on;
  11. if (!-e $request_filename){
  12. rewrite ^/(.*) /index.php?r=$ last;
  13. }
  14. }
  15. location ~ \.php(.*)$ {
  16. fastcgi_pass 127.0.0.1:;
  17. fastcgi_index index.php;
  18. fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
  19. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  20. fastcgi_param PATH_INFO $fastcgi_path_info;
  21. fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
  22. include fastcgi_params;
  23. }
  24. }
  25.  
  26. server {
  27. listen ;
  28. server_name m.yiiui.com ;
  29. root "E:/www/yiiui/myweb/web";
  30. location / {
  31. index index.html index.htm index.php;
  32. #autoindex on;
  33. if (!-e $request_filename){
  34. rewrite ^/(.*) /index.php?r=$ last;
  35. }
  36. }
  37.  
  38. location ~ \.php(.*)$ {
  39. fastcgi_pass 127.0.0.1:;
  40. fastcgi_index index.php;
  41. fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
  42. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  43. fastcgi_param PATH_INFO $fastcgi_path_info;
  44. fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
  45. include fastcgi_params;
  46. }
  47. }
  48.  
  49. ################ 新增 nginx负载均衡端口域名 81 82 .... #########################################
  50.  
  51. server {
  52. listen ;
  53. server_name yiiui.com;
  54. root "E:/www/yiiui/backend/web";
  55.  
  56. location / {
  57. index index.html index.htm index.php;
  58. #autoindex on;
  59. if (!-e $request_filename){
  60. rewrite ^/(.*) /index.php?r=$ last;
  61. }
  62. }
  63. location ~ \.php(.*)$ {
  64. fastcgi_pass 127.0.0.1:;
  65. fastcgi_index index.php;
  66. fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
  67. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  68. fastcgi_param PATH_INFO $fastcgi_path_info;
  69. fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
  70. include fastcgi_params;
  71. }
  72. }
  73.  
  74. server {
  75. listen ;
  76. server_name m.yiiui.com ;
  77. root "E:/www/yiiui/myweb/web";
  78. location / {
  79. index index.html index.htm index.php;
  80. #autoindex on;
  81. if (!-e $request_filename){
  82. rewrite ^/(.*) /index.php?r=$ last;
  83. }
  84. }
  85.  
  86. location ~ \.php(.*)$ {
  87. fastcgi_pass 127.0.0.1:;
  88. fastcgi_index index.php;
  89. fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
  90. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  91. fastcgi_param PATH_INFO $fastcgi_path_info;
  92. fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
  93. include fastcgi_params;
  94. }
  95. }

B主机的nginx.conf中新增同A主机一样的带端口设置 (如果为内网局域网先设置hosts)

  1. server {
  2. listen ;
  3. server_name yiiui.com;
  4. root "C:/www/yiiui/backend/web";
  5.  
  6. location / {
  7. index index.html index.htm index.php;
  8. #autoindex on;
  9. if (!-e $request_filename){
  10. rewrite ^/(.*) /index.php?r=$ last;
  11. }
  12. }
  13. location ~ \.php(.*)$ {
  14. fastcgi_pass 127.0.0.1:;
  15. fastcgi_index index.php;
  16. fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
  17. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  18. fastcgi_param PATH_INFO $fastcgi_path_info;
  19. fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
  20. include fastcgi_params;
  21. }
  22. }
  23.  
  24. server {
  25. listen ;
  26. server_name m.yiiui.com;
  27. root "C:/www/yiiui/myweb/web";
  28.  
  29. location / {
  30. index index.html index.htm index.php;
  31. #autoindex on;
  32. if (!-e $request_filename){
  33. rewrite ^/(.*) /index.php?r=$ last;
  34. }
  35. }
  36. location ~ \.php(.*)$ {
  37. fastcgi_pass 127.0.0.1:;
  38. fastcgi_index index.php;
  39. fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
  40. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  41. fastcgi_param PATH_INFO $fastcgi_path_info;
  42. fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
  43. include fastcgi_params;
  44. }
  45. }

设置B主机的负载域名:

  1. upstream yiiui.com{
  2. server 127.0.0.1:; #这里是你自己要做负载均衡的服务器地址1 # 本地windows可以,外网linux 要用局域网的IP
  3. server 192.168.2.101:; #这里是要参与负载均衡的地址2
  4. }
  5.  
  6. upstream m.yiiui.com{
  7. server 127.0.0.1: weight=; #这里是你自己要做负载均衡的服务器地址1
  8. server 192.168.2.101: weight=; #这里是要参与负载均衡的地址2
  9. }
  10.  
  11. server {
  12. listen ;
  13. server_name yiiui.com; #设置所有web服务器负载的共同域名
  14. location / {
  15. proxy_pass http://yiiui.com; #确定需要代理的URL,端口或socket。
  16. proxy_set_header Host $host;
  17. }
  18. }
  19.  
  20. server {
  21. listen ;
  22. server_name m.yiiui.com; #设置所有web服务器负载的共同域名
  23. location / {
  24. proxy_pass http://m.yiiui.com; #确定需要代理的URL,端口或socket。
  25. proxy_set_header Host $host;
  26. }
  27. }

参考:

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 的设置:

  1. # 设置session保存在mysql中
  2. 'session' => [
  3. 'class' => 'yii\web\DbSession',
  4. // Set the following if you want to use DB component other than
  5. // default 'db'.
  6. // 'db' => 'mydb',
  7. // To override default session table, set the following
  8. // 'sessionTable' => 'my_session',
  9. ],

参考:

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)算法自动删除不使用的缓存。

Nginx+DNS负载均衡实现的更多相关文章

  1. DNS负载均衡 Nginx 负载均衡的种类

    DNS负载均衡 当一个网站有足够多的用户的时候,假如每次请求的资源都位于同一台机器上面,那么这台机器随时可能会蹦掉.处理办法就是用DNS负载均衡技术,它的原理是在DNS服务器中为同一个主机名配置多个I ...

  2. DNS负载均衡与NGINX负载均衡策略

    负载均衡是指的是把请求均匀的分摊到多个服务器上处理.一般常见的负载均衡有两种:①客户端与反向代理服务器之间的DNS负载均衡②反向代理服务器与应用服务器之间的负载均衡(这种负载均衡有很多,可以是webl ...

  3. Nginx实现负载均衡功能

    一.什么是Nginx? Nginx是一款轻量级的Web 服务器.反向代理服务器.电子邮件(IMAP/POP3)代理服务器. 二.Nginx的优点: 高并发连接:官方测试Nginx能够支撑5万并发连接, ...

  4. 死磕nginx系列--使用nginx做负载均衡

    使用nginx做负载均衡的两大模块: upstream 定义负载节点池. location 模块 进行URL匹配. proxy模块 发送请求给upstream定义的节点池. upstream模块解读 ...

  5. Nginx的负载均衡的几种方式

    Nginx的负载均衡的那点事 本节就聊聊采用Nginx负载均衡之后碰到的问题: Session问题 文件上传下载 通常解决服务器负载问题,都会通过多服务器分载来解决.常见的解决方案有: 网站入口通过分 ...

  6. nginx的负载均衡的问题

    本节就聊聊采用Nginx负载均衡之后碰到的问题: Session问题 文件上传下载 通常解决服务器负载问题,都会通过多服务器分载来解决.常见的解决方案有: 网站入口通过分站链接负载(天空软件站,华军软 ...

  7. [转帖]利用nginx实现负载均衡 | 哈希算法,sticky模块实现session粘滞

    利用nginx实现负载均衡 | 哈希算法,sticky模块实现session粘滞 2018年08月02日 10:06:03 Minza 阅读数 483 https://blog.csdn.net/ha ...

  8. Nginx网络负载均衡,负载均衡,网络负载,网络均衡

    本节就聊聊采用Nginx负载均衡之后碰到的问题: Session问题 文件上传下载 通常解决服务器负载问题,都会通过多服务器分载来解决.常见的解决方案有: 网站入口通过分站链接负载(天空软件站,华军软 ...

  9. nginx+tomcat负载均衡

    最近练习nginx+tomcat负载均衡.根据一些资料整理了大体思路,最终实现了1个nginx+2个tomcat负载均衡. 安装JDK 1>进入安装目录,给所有用户添加可执行的权限 #chmod ...

随机推荐

  1. Mac下查看node等的安装路径

  2. mysql 修改表结构、表字段注释语句

    虽然现在有各种各样的工具可以直接对表结构进行修改,但是我还是喜欢使用语句进行修改.以下语句是对表增加字段.给字段加注释的语句 alter table orders add column isupdyq ...

  3. es6异步编程

    https://blog.csdn.net/tcy83/article/details/80274772 等一系列文章

  4. IIS8的SNI功能实现同一服务器多HTTPS站点

    名词解释: SNI指是一项用于改善SSL/TLS的技术,在SSLv3/TLSv1中被启用.它允许客户端在发起SSL握手请求时(具体说来,是客户端发出SSL请求中的ClientHello阶段),就提交请 ...

  5. python 内置函数详解

    懒得写了  参考1:https://www.cnblogs.com/xiao1/p/5856890.html 参考2:https://www.runoob.com/python/python-buil ...

  6. sketch格式文件转换成psd

    在做响应式页面的时间需要把px单位转换成rem才可以,但是sketch文件的格式不能随意转换成rem,最高只能到CSS rem 16px,不能满足我们的需求,因此需要一个工具来转换成psd格式文件,他 ...

  7. Apache Spark 章节1

    作者:jiangzz 电话:15652034180 微信:jiangzz_wx 微信公众账号:jiangzz_wy 背景介绍 Spark是一个快如闪电的统一分析引擎(计算框架)用于大规模数据集的处理. ...

  8. 简单的Web日志处理细节

  9. 在Windows2008r2 安装.net4.5

    WebApi 是比较高的环境下面开发 需要的环境是net4.5 或以上. Windows2008r2 里面没有这个环境必须自己安装.安装net4.5 必须sp1环境以上才能安装. 将Windows20 ...

  10. 【汇编语言】Win10 安装 DOXBox0.74

    1.下载package包,有用的只有前两个. 2.解压 masm 压缩文件,我把它解压到D盘. 3.双击 图1 中的 DOXBox 0.74.exe 进行安装. 4.打开 DOXBox0.74 (参考 ...