Nginx+DNS负载均衡实现
2019独角兽企业重金招聘Python工程师标准>>>
负载均衡有多种实现方法,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节点下添加 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 要用局域网的IPserver 192.168.2.101:81; #这里是要参与负载均衡的地址2 }upstream m.yiiui.com{server 127.0.0.1:82 weight=200; #这里是你自己要做负载均衡的服务器地址1server 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)算法自动删除不使用的缓存。
转载于:https://my.oschina.net/u/2935389/blog/3039643
Nginx+DNS负载均衡实现相关推荐
- Nginx+DNS负载均衡
今天看了很多关于nginx负载均衡的博客,人家推荐的都是自己的ip来做负载,但是同样有说DNS负载均衡,刚开始我也是一头雾水,慢慢的分析才知道真正意义上的Nginx+DNS负载均衡. 1. nginx ...
- Nginx+LVS+DNS 负载均衡
1.Nginx的负载均衡属于七层负载均衡,会建立TCP链接,产生流量的那种 1.轮训法 2.随机 3.加权轮训 4.加权随机 5.原地址哈希法(能保证每次请求都打在同一台机器上) 6.最少连接数 最小 ...
- DNS负载均衡和NGINX负载均衡
负载均衡:把请求均匀的分摊到多个服务器上处理 两种负载均衡: 客户端与反向代理服务器之间的DNS负载均衡 服务器与反向代理服务器之间的负载均衡,可以使apache+tomcat负载均衡,也可以是ngn ...
- DNS负载均衡与NGINX负载均衡策略
负载均衡是指的是把请求均匀的分摊到多个服务器上处理.一般常见的负载均衡有两种:①客户端与反向代理服务器之间的DNS负载均衡②反向代理服务器与应用服务器之间的负载均衡(这种负载均衡有很多,可以是webl ...
- Nginx文档阅读笔记-DNS load balancing(DNS负载均衡)
目录 官方解释 演示及配置 官方解释 DNS负载均衡是DNS服务器的配置,这个配置可以把客户端请求分配到不同的服务器上,这个服务器可以是网站,也可能是邮箱系统,在互联网上的系统都可以做负载均衡. DN ...
- lnmp架构——nginx的负载均衡
lnmp架构--nginx的负载均衡 1 什么是nginx 2 nginx的作用 3 nginx的特点 4 nginx的安装以及优化 4.1 安装nginx 4.2 优化 5 nginx主配置文件操作 ...
- 用NGINX做负载均衡,keepalived高可用
实验环境,四台虚拟机,两台做负载均衡,两台做RS IP地址:两台负载均衡分别为:10.0.0.7:10.0.0.8(高可用keepalived) 两台 RS主机地址为: 10.0.0.9:10.0.0 ...
- Nginx做负载均衡的模块
负载均衡模块 使用nginx做负载均衡的两大模块: upstream 定义负载节点池 ocation 模块 进行URL匹配. proxy模块 发送请求给upstream定义的节点池 upstream模 ...
- 使用nginx做负载均衡
使用nginx做负载均衡的两大模块: upstream 定义负载节点池. location 模块 进行URL匹配. proxy模块 发送请求给upstream定义的节点池. upstream模块解读 ...
最新文章
- CentOS6.3配置yum源
- SIP穿越NATFireWall解决方案
- python常用内置模块-Python内置模块和第三方模块
- html的分类与特点
- 科大星云诗社动态20210424
- JailbreakMe.com-最新浏览器模式破解iPhones,iPads和iPod Touches方法
- word List25
- micro/protoc-gen-micro 不见了
- 02 算术、字符串与变量(1)
- 神东煤矿:煤矿管控难?且看帆软如何助力其智慧化生产管控
- C#中增量类功能的方式之 继承与扩展
- java 内存溢出分析_用一段时间后java内存溢出问题分析(转)
- join和子查询效率_SparkSQL连接查询中的谓词下推处理(上)
- HDU 1874 畅通工程续 最短路
- 理解UIScrollView
- COM中关于使用DLL的一些知识点
- Java,List转json,json转List
- 嵌入式常用通讯协议2(CAN协议)
- APM-Skywalking调研及实施报告
- 登录 不输入账号密码 不能到下一个页面 没办法通过网址直接进去
热门文章
- .net core 1.1 mysql_Asp.net Core 1.1 升级后操作mysql出错的解决办法|chu
- ubuntu 开机后不动_Ubuntu启动时停止的问题
- mysql强制指定索引_mysql强制索引和禁止某个索引
- android tabhost的使用方法,android TabHost(选项卡)的使用方法
- Android内部类监听和多对象监听方法
- Python字符串常用方法(split,partition,maketrans,strip...)
- Eclipse 自动清理未使用 Import
- linux手术后10年,经历正颌手术10年后遗症的我想说40岁做正颌手术我很后悔
- 20190814:(leetcode习题)移动零 (补)
- oracle中varchar与varchar2的区别