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的负载均衡的几种常见的配置以及优缺点 第一种:轮询(默认) 优点:实现简单 缺点:不考虑每台服务 ...
随机推荐
- ubuntu14.04下安装qt5
1.sudo apt-get install build-essential 2.先打开终端快捷键ctrl+t 3. 然后输入: sudo apt-get install cmake qt5-defa ...
- AngularJS基本使用
简介 AngularJS是Google开源的前端JS结构化框架 Angular关注的是动态展示页面数据, 并与用户进行交互.其主体不再是DOM,而是页面中的动态数据 AngularJS特性(优点) 双 ...
- 实践作业4---DAY3阶段二。
第二阶段是用户调研,我们小组选择了一个5班的同学,作为采访对象.采访比较详实,但是样本太少,不太有说服力. 具体采访详情如下: 问:请问您使用喜欢发表Blog么? 答:肯定有啊. 问:都用什么网站发表 ...
- PropertiesConfiguration 修改配置文件的信息,不打乱顺序
需引入jar包 <!-- https://mvnrepository.com/artifact/commons-configuration/commons-configuration --> ...
- k8s启动Pod遇到CrashLoopBackOff的解决方法
1.用kubectl get pod 当看到上面的状态后执行第2步 2.查看pod详情 [root@cc hzb]# kubectl describe pod ceph-mysql-hzb-pod 找 ...
- Hadoop-2.4.0分布式安装手册
目录 目录 1 1. 前言 2 2. 部署 2 2.1. 机器列表 2 2.2. 主机名 2 2.2.1. 临时修改主机名 3 2.2.2. 永久修改主机名 3 2.3. 免密码登录范围 4 3. 约 ...
- 20169214 2016-2017-2 《移动平台开发实践》Android程序设计 实验报告
实验四 Android程序设计 课堂练习 实验题目 采用后缀表达式法,设计一个建议计算器,实现+.-.*./四种运算. 代码实现 码云链接 关键代码部分及结果如下: Android程序实验 Andro ...
- [LeetCode 题解]: Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...
- jquery实现简单抽奖功能
一直纠结要怎么用jquery实现抽奖功能,看别人很多都是用flash制作的,找了很多资料,最终找到一个比较适合需求的,我做了些许调整,以下是代码展示(复制下来可以直接使用). 先上图:
- TSQL--SORT MERGE JOIN
算法:对两表排序,然后对两表依次扫描,找到符合条件的结果集 sort(T1); seort(T2); int k=0;--for T1 index int m=0;--for T2 index whi ...