一、关于Nginx的负载均衡
在服务器集群中,Nginx起到一个代理服务器的角色(即反向代理),为了避免单独一个服务器压力过大,将来自用户的请求转发给不同的服务器。详情请查看我的另一篇博客。
二、Nginx负载均衡策略
负载均衡用于从“upstream”模块定义的后端服务器列表中选取一台服务器接受用户的请求。一个最基本的upstream模块是这样的,模块内的server是服务器列表:
#动态服务器组 upstream dynamic_zuoyu { server localhost:8080; #tomcat 7.0 server localhost:8081; #tomcat 8.0 server localhost:8082; #tomcat 8.5 server localhost:8083; #tomcat 9.0 }
在upstream模块配置完成后,要让指定的访问反向代理到服务器列表:
#其他页面反向代理到tomcat容器 location ~ .*$ { index index.jsp index.html; proxy_pass http://dynamic_zuoyu; }
这就是最基本的负载均衡实例,但这不足以满足实际需求;目前Nginx服务器的upstream模块支持6种方式的分配:
轮询 | 默认方式 |
weight | 权重方式 |
ip_hash | 依据ip分配方式 |
least_conn | 最少连接方式 |
fair(第三方) | 响应时间方式 |
url_hash(第三方) | 依据URL分配方式 |
在这里,只详细说明Nginx自带的负载均衡策略,第三方不多描述。
1、轮询
最基本的配置方法,上面的例子就是轮询的方式,它是upstream模块默认的负载均衡默认策略。每个请求会按时间顺序逐一分配到不同的后端服务器。
有如下参数:
fail_timeout | 与max_fails结合使用。 |
max_fails | 设置在fail_timeout参数设置的时间内最大失败次数,如果在这个时间内,所有针对该服务器的请求都失败了,那么认为该服务器会被认为是停机了, |
fail_time | 服务器会被认为停机的时间长度,默认为10s。 |
backup | 标记该服务器为备用服务器。当主服务器停止时,请求会被发送到它这里。 |
down | 标记服务器永久停机了。 |
注意:
- 在轮询中,如果服务器down掉了,会自动剔除该服务器。
- 缺省配置就是轮询策略。
- 此策略适合服务器配置相当,无状态且短平快的服务使用。
2、weight
权重方式,在轮询策略的基础上指定轮询的几率。例子如下:
#动态服务器组 upstream dynamic_zuoyu { server localhost:8080 weight=2; #tomcat 7.0 server localhost:8081; #tomcat 8.0 server localhost:8082 backup; #tomcat 8.5 server localhost:8083 max_fails=3 fail_timeout=20s; #tomcat 9.0 }
在该例子中,weight参数用于指定轮询几率,weight的默认值为1,;weight的数值与访问比率成正比,比如Tomcat 7.0被访问的几率为其他服务器的两倍。
注意:
- 权重越高分配到需要处理的请求越多。
- 此策略可以与least_conn和ip_hash结合使用。
- 此策略比较适合服务器的硬件配置差别比较大的情况。
3、ip_hash
指定负载均衡器按照基于客户端IP的分配方式,这个方法确保了相同的客户端的请求一直发送到相同的服务器,以保证session会话。这样每个访客都固定访问一个后端服务器,可以解决session不能跨服务器的问题。
#动态服务器组 upstream dynamic_zuoyu { ip_hash; #保证每个访客固定访问一个后端服务器 server localhost:8080 weight=2; #tomcat 7.0 server localhost:8081; #tomcat 8.0 server localhost:8082; #tomcat 8.5 server localhost:8083 max_fails=3 fail_timeout=20s; #tomcat 9.0 }
注意:
- 在nginx版本1.3.1之前,不能在ip_hash中使用权重(weight)。
- ip_hash不能与backup同时使用。
- 此策略适合有状态服务,比如session。
- 当有服务器需要剔除,必须手动down掉。
4、least_conn
把请求转发给连接数较少的后端服务器。轮询算法是把请求平均的转发给各个后端,使它们的负载大致相同;但是,有些请求占用的时间很长,会导致其所在的后端负载较高。这种情况下,least_conn这种方式就可以达到更好的负载均衡效果。
#动态服务器组 upstream dynamic_zuoyu { least_conn; #把请求转发给连接数较少的后端服务器 server localhost:8080 weight=2; #tomcat 7.0 server localhost:8081; #tomcat 8.0 server localhost:8082 backup; #tomcat 8.5 server localhost:8083 max_fails=3 fail_timeout=20s; #tomcat 9.0 }
注意:
- 此负载均衡策略适合请求处理时间长短不一造成服务器过载的情况。
5、第三方策略
第三方的负载均衡策略的实现需要安装第三方插件。
①fair
按照服务器端的响应时间来分配请求,响应时间短的优先分配。
#动态服务器组 upstream dynamic_zuoyu { server localhost:8080; #tomcat 7.0 server localhost:8081; #tomcat 8.0 server localhost:8082; #tomcat 8.5 server localhost:8083; #tomcat 9.0 fair; #实现响应时间短的优先分配 }
②url_hash
按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,要配合缓存命中来使用。同一个资源多次请求,可能会到达不同的服务器上,导致不必要的多次下载,缓存命中率不高,以及一些资源时间的浪费。而使用url_hash,可以使得同一个url(也就是同一个资源请求)会到达同一台服务器,一旦缓存住了资源,再此收到请求,就可以从缓存中读取。
#动态服务器组 upstream dynamic_zuoyu { hash $request_uri; #实现每个url定向到同一个后端服务器 server localhost:8080; #tomcat 7.0 server localhost:8081; #tomcat 8.0 server localhost:8082; #tomcat 8.5 server localhost:8083; #tomcat 9.0 }
三、总结
以上便是6种负载均衡策略的实现方式,其中除了轮询和轮询权重外,都是Nginx根据不同的算法实现的。在实际运用中,需要根据不同的场景选择性运用,大都是多种策略结合使用以达到实际需求。
nginx实现负载均衡和动静分离
nginx配置(windows配置),供大家参考,具体内容如下
以下是我的项目用到的一份配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
|
以上就是我的一个配置。基本上需要注意的都在配置文件中注解。我把几个重要的地方单独拿出来讲一下。
1.集群的配置,我在上面的配置里有定义多个集群,集群按字面的意思理解就是由多台服务器构成的一个集合,典型的例子如
1 2 3 4 5 |
|
这样的一个配置,这个集群包含了两个分支,我们可以在两台服务器上搭建相同的项目(上述的例子是在同样的服务器,不同的端口部署相同的项目,因为笔者的服务器有限),当有请求是需要这个集群来处理的时候,nginx会随机分配,当然也可以配置权重来设置两个server的访问概率。这就是负载均衡的原理。我们在多台服务器上部署相同的项目,利用nginx对请求进行转发,这样可以降低只有一台服务器所造成的的负载过大,而且当其中一台服务器挂掉以后,nginx会分配另外一台服务器来工作,这样就不会造成服务停止了。
2.server配置项代表的是一个代理服务器,上面的文件中我们配置了两个文件,分别监听wonyen.com(www.wonyen.com)这两个域名的80和8886端口,所有访问wonyen.com:80(即wonyen.com)这个域名下的请求,都按照第一个server所定义的规则去转发,而所有访问wonyen.com:8886下的请求,则会按照第二个server所定义的规则去转发。
3.我们甚至可以通过配置来处理多个域名,看以下的例子。下面的例子我配置了两个域名的规则,一个是iis服务器,一个是tomcat服务器,主要目的是为了解决80端口只能被一个程序使用的问题。如果iis用了80,tomcat就用不了,反之亦然。所以我给iis和tomcat都分配除了80以外的端口,而把80端口留给niginx。由nginx来分配请求给不同的网站。
复制代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
|
4.还有一个就是动静分离,说得通俗一点就是,把请求数据(动)与请求图片(静)分开,在tomcat里,当我们没有做动静分离的时候,tomcat把对图片的请求也会当成一个动态的请求,而处理动态请求是比较费性能的(至于为什么,我也不太清楚)。所以我们可以使用nginx配置来实现动静分离。
我的做法是把其中一个tomcat项目放在nginx的根目录下,这样,我们就可以通过以下方式来配置,实现当我们访问图片,js,css等静态资源的时候,都到一个指定的目录去访问。这样做的好处除了节省性能,还有一个就是我们不需要在所有的负载均衡服务器中都同步保留这些静态资源,只需要在一个地方保留就好了。配置如下
1 2 3 4 5 |
|
5.既然读取静态资源是从这个目录读取的,那么我们必须考虑如何存储静态资源,特别是当我们做了负载均衡以后,在我们的 项目中上传图片的请求有可能在任意一个集群的分支中被调用,比如我们的集群中有A,B两台服务器,他们都有可能做上传图片这件事情,如果A调用了上传图片这个请求,则图片则被上传到了A这台服务器上,反之就是B上面。这样势必导致A,B两台服务器上的静态图片是不同步的,当我们要访问这些图片的时候,(假设此时我们还没做动静分离)就有可能出现访问不到的情况。由于上一步我们做了动静分离,现在的问题就演变为,如何把这些A,B服务器上传的图片,都同步到我们做动静分离的那个文件夹下。人工或者程序去同步都很麻烦,我的做法是指定一台服务器(也就是nginx安装的那台服务器)的tomcat项目(也就是部署在nginx根目录下的那个tomcat项目),让它专门来负责上传图片的工作,这样所有的图片都由这个tomcat项目来上传,也就保证了静态库中的图片就是完整的图片。为此我配置了一个集群,如下。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
因为我把所有涉及到附件上传的请求都加上了Att或者Fill的后缀,当nginx捕获这些后缀名的请求的时候,就会把他们都交给 http://xdxfile.com这个集群,也就是119.10.52.28:8081这个项目。
6.做了负载均衡以后,有一个不得不面临的问题就是内存数据的同步,我们在程序中有时候会把一些数据存放在内存中,典型的一类数据就是session。如何让session数据在集群的各个分支中共享session呢,这边要用到一个新的东西,叫做redis。我会在下一篇文章中详细地介绍。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
Nginx负载均衡配置简单配置方法
Nginx配置文件
1) 从Nginx官网下载 http://nginx.org/en/download.html
2) 安装Nginx,并找到nginx.conf文件(C:\nginx\conf\nginx.conf);
在http中加入配置:
加权轮询,按服务器的性能给予权重,本例是1:2分配
1 2 3 4 |
|
ip_hash轮询方法,不可给服务器加权重,nginx会让相同的客户端ip请求相同的服务器
1 2 3 4 5 |
|
根据服务器的本身的性能差别及职能,可以设置不同的参数控制。
down 表示负载过重或者不参与负载
weight 权重过大代表承担的负载就越大
backup 其它服务器时或down时才会请求backup服务器
max_fails 失败超过指定次数会暂停或请求转往其它服务器
fail_timeout 失败超过指定次数后暂停时间
server配置如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
1.查看Nginx版本:
C:\nginx>nginx -v
2.启动Nginx:
C:\nginx>start nginx
启动Nginx需要占用80端口,常见错误:bind() to 0.0.0.0:8080 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions),需要把系统的80端口关掉,
检查端口:netstat -aon | findstr :80
3.判断Nginx是否启动:
tasklist /fi "imagename eq nginx.exe"
4.停止:
C:\nginx>nginx.exe -s stop
5.重新载入Nginx:
C:\nginx>nginx.exe -s reload
如何通过nginx负载均衡跳转https
web端拷贝证书与密钥
scp -rp -P52113 /application/nginx/conf/key 10.0.0.5:/application/nginx/conf/
在nginx负载均衡服务端配置
vim /application/nginx/conf/nginx.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
|
#检查nginx负载均衡配置
/application/nginx/sbin/nginx -t
#重启nginx负载均衡
/application/nginx/sbin/nginx -s stop
/application/nginx/sbin/nginx
浏览器访问测试
注意修改hosts对应的是负载均衡的IP地址信息
关于nginx+tomcat搭建反向代理时加载静态资源找不到的问题
本文链接:https://blog.csdn.net/zhaoxiaohua125/article/details/78751953
1 、在配置nginx的反向代理时在localtion下需要
upstream tomcat_nginx{
server 127.0.0.1:8888;
server 127.0.0.1:8889;
}
location / {
proxy_pass http://tomcat_nginx;
proxy_set_header Host $host;//解决加载静态资源找不到的问题
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect default ;
root html;
index index.html index.htm;
proxy_connect_timeout 1;//解决多个tomcat一个崩溃后加载慢的问题
proxy_read_timeout 1;//解决多个tomcat一个崩溃后加载慢的问题
proxy_send_timeout 1; //解决多个tomcat一个崩溃后加载慢的问题
}
2.当nginx代理端口不是80的时候需要修改tomcat的server.xml文件将connector的节点中增加一个proxyPort="nginx的端口号" nginx代理端口的位置
————————————————
版权声明:本文为CSDN博主「zhaoxiaohua125」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zhaoxiaohua125/article/details/78751953