nginx(四)-负载均衡
负载均衡,我认为是nginx最重要的功能了。那什么是负载均衡呢。
比如有一个服务,它访问量很大,一台机器吃不消了,怎么办,我们准备两台。分一部分的请求出来。现在有两台服务器提供这个服务。我们访问其中一个就行,这就有另外一个问题,就像通往某个地方有两条路,万一所有人都选择走同一条路,那这条路不是还堵吗?这就用到了负载均衡的功能了。
负载均衡目前有自带的三种方式,还有两种常用的第三方策略。
1.RR(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务某一台机器down掉了,能自动剔除。
简单配置
upstream springboot_test{
server 127.0.0.1:8084;
server 127.0.0.1:8085;
}
location / {
proxy_pass http://springboot_test;
}
#user nobody;
worker_processes 1; #error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #pid logs/nginx.pid; events {
worker_connections 1024;
} http {
include mime.types;
default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on;
#tcp_nopush on; #keepalive_timeout 0;
keepalive_timeout 65; #gzip on; upstream springboot_test{
server 127.0.0.1:8084 weight=1;
server 127.0.0.1:8085 weight=2;
} server {
listen 9000;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / {
proxy_pass http://springboot_test;
} #error_page 404 /404.html; # redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
} # proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#} # deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
} # another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias; # location / {
# root html;
# index index.html index.htm;
# }
#} # HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost; # ssl_certificate cert.pem;
# ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on; # location / {
# root html;
# index index.html index.htm;
# }
#} }
点击展开
2.权重
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
例如:
upstream springboot{
server localhost:8084 weight=9;
server localhost:8085 weight=1;
}
那么10次一般只会有一次会访问到8085,9次会访问8084.
3.ip_hash
上面的两种方式都存在一个问题,那就是下一个请求来的时候,请求可能分到另外一个服务器,当我们的程序不是无状态的时候(采用了session保存数据),这时候就有一个很大的问题。比如,把登录信息保存到session中,那么跳转到另外一台服务器的时候就需要重新登录了。所以很多时候我们需要一个用户只询问同一台服务器,那么就需要用ip_hash了。ip_hash的每个请求按访问ip的hash结果分配,这样每个访客固定访问一台后端服务器,可以解决session问题。
upstream springboot{
ip_hash;
server localhost:8084;
server localhost:8085;
}
4.fair(第三方)
按后端服务器的响应时间来分配请求,相应时间短的优先分配。
upstream springboot{
fair;
server localhost:8084;
server localhost 8085;
}
5.url_hash(第三方)
按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效,在upstream 中加入hash语句,server语句中不能写入weight等其他参数,hash_method里使用hash算法。
upstream springboot{
hash $request_url;
hash_method crc32;
server localhost:8084;
server localhost:8085;
}
注意:4和5需要安装第三方模块才能使用。
nginx(四)-负载均衡的更多相关文章
- 使用Nginx实现负载均衡
使用Nginx实现负载均衡 一.nginx简介 nginx是一个高性能的HTTP服务器和反向代理服务器.它起初是俄罗斯人Igor Sysoev开发的,至今支撑者俄罗斯的很多大型的网站. 二.nginx ...
- Nginx的负载均衡 - 整体架构
Nginx的负载均衡 - 整体架构 Nginx版本:1.9.1 我的博客:http://blog.csdn.net/zhangskd Nginx目前提供的负载均衡模块: ngx_http_upstre ...
- [转载] nginx的负载均衡
原文:http://www.srhang.me/blog/2014/08/27/nginx-loabbalance/ Nginx负载均衡 一.特点 1.1 应用情况 Nginx做为一个强大的Web服务 ...
- Nginx实现负载均衡&Nginx缓存功能
一.Nginx是什么 Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器.Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambl ...
- 利用nginx实现负载均衡和动静分离
1.Nginx介绍 Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器 . Nginx 是由 ...
- Nginx实现负载均衡功能
一.什么是Nginx? Nginx是一款轻量级的Web 服务器.反向代理服务器.电子邮件(IMAP/POP3)代理服务器. 二.Nginx的优点: 高并发连接:官方测试Nginx能够支撑5万并发连接, ...
- windows配置nginx实现负载均衡集群 -请求分流
windows配置nginx实现负载均衡集群 一.windows上安装nginx 1.下载nginx的windows版本http://nginx.org/en/download.html 2.把压缩文 ...
- Nginx的负载均衡
什么是负载均衡 负载均衡主要通过专门的硬件设备或者通过软件算法实现.通过硬件设备实现的负载均衡效果好.效率高.性能稳定,但是成本比较高.通过软件实现的负载均衡主要依赖于均衡算法的选择和程序的健壮性.均 ...
- liunx 利用nginx 实现负载均衡
一般采用软件实现负载均衡的有Nginx.apache.nginx 近年来使用频繁,其官网上面显示可以承载5万并发访问量,太牛了. nginx 相比 apache优势明显:Nginx 服务程序比较稳定, ...
- nginx的负载均衡配置,常用策略
场景:nginx是一款非常优秀的负载均衡服务器,小巧而且性能强悍,中小型企业的首选. 下面介绍nginx的负载均衡的几种常见的配置以及优缺点 第一种:轮询(默认) 优点:实现简单 缺点:不考虑每台服务 ...
随机推荐
- /etc/profile、~/.bash_profile等几个文件的执行过程
/etc/profile.~/.bash_profile等几个文件的执行过程 摘自:http://blog.chinaunix.net/uid-14735472-id-3190130.html 分类 ...
- java8 Lambda表达式的10个例子(转)
原文:http://jobar.iteye.com/blog/2023477 Java8中Lambda表达式的10个例子 例1 用Lambda表达式实现Runnable接口 Java代码 收藏代码// ...
- <a>标签的用法以及[@text_cut]
<a href="${a.url}" target="_blank">[@text_cut s=a.title len=titLen append= ...
- js 格式化时间
//格式化时间 function time_format(time) { return new Date(parseInt(time) * 1000).toLocaleString().replace ...
- mysql按照天或小时group分组统计
select DATE_FORMAT( deteline, "%Y-%m-%d %H" ) , COUNT( * ) FROM test GROUP BY DATE_FORMAT( ...
- Centos 7 修改默认的运行级别
Runlevel System State 0 Halt the system 1 Single user mode 2 Basic multi user mode 3 Multi user mode ...
- google chrome 调试技巧:监控 DOM 元素被修改
在很多时候, 页面上一个元素的属于被修改.删除,子节点的添加与修改,很难一下找到对应的代码,在 google chrome 开发者工具里, 提供了对 DOM 元素的监控: 在 Elements 标签, ...
- ML.NET Cookbook --- 1.如何从文本文件中加载数据?
使用ML.NET中的TextLoader扩展方法从文本文件中加载数据.你需要知道在文本文件中数据列在那里,它们的类型是什么,在文本文件中什么位置可以找到它们. 请注意:对于ML.NET只读取文件的某些 ...
- ViewController关闭自身返回前一个View
点击按钮号响应的函数 @IBAction func onBack(sender : AnyObject) { self.dismissViewControllerAnimated(true, comp ...
- [USACO09FEB] 改造路Revamping Trails | [JLOI2011] 飞行路线
题目链接: 改造路 飞行路线 其实这两道题基本上是一样的,就是分层图的套路题. 为什么是分层图呢?首先,我们的选择次数比较少,可以把这几层的图建出来而不会爆空间.然后因为选择一个边权为0的路线之后我们 ...