负载均衡,我认为是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需要安装第三方模块才能使用。

最新文章

  1. [daily][device][bluetooth] 蓝牙怎么办!(archlinux下驱动蓝牙鼠标,以及三星手机)
  2. javascript 中的location.reload
  3. Chrome设计文档-多进程架构
  4. 【转】Ecshop 后台增加一个左侧列表菜单menu菜单的方法
  5. 2715:谁拿了最多奖学金-poj
  6. Smarty基础用法
  7. Hibernate学习(三)自动建表
  8. (NO.00001)iOS游戏SpeedBoy Lite成形记(二十四)
  9. RabbitMQ队列的使用
  10. Struts2内建拦截器
  11. unity3D客户端框架
  12. pat--7-11 出栈序列的合法性(25 分)
  13. CentOS 下搭建SVN
  14. 洛谷 P3629 [APIO2010]巡逻 解题报告
  15. android显示TextView文字的倒影效果
  16. WPF 如何加载图片
  17. VS调试-添加命令行参数
  18. POJ1247-Magnificent Meatballs
  19. Android MVP模式简单易懂的介绍方式 (三)
  20. [APIO2015]巴厘岛的雕塑

热门文章

  1. c#反射优化 表达式树
  2. 向对象(OO)程序设计
  3. 一起做RGB-D SLAM (5)
  4. SQL数据透视
  5. HDU 2602 Bone Collector (01背包DP)
  6. 如何Android中加入扫描名片功能
  7. mysql复制表以及复制数据库
  8. Python 数据分析—第九章 数据聚合与分组运算
  9. js拼接字符串传值,子窗口传值
  10. C++三种野指针及应对/内存泄露