1、轮询

轮询即Round Robin,根据Nginx配置文件中的顺序,依次把客户端的Web请求分发到不同的后端服务器。
配置的例子如下:

http{
upstream sampleapp {
server <<dns entry or IP Address(optional with port)>>;
server <<another dns entry or IP Address(optional with port)>>;
}
....
server{
listen ;
...
location / {
proxy_pass http://sampleapp;
}
}

上面只有1个DNS入口被插入到upstream节,即sampleapp,同样也在后面的proxy_pass节重新提到。

2、最少连接

Web请求会被转发到连接数最少的服务器上。
配置的例子如下:

http{
upstream sampleapp {
least_conn;
server <<dns entry or IP Address(optional with port)>>;
server <<another dns entry or IP Address(optional with port)>>;
}
....
server{
listen ;
...
location / {
proxy_pass http://sampleapp;
}
}

上面的例子只是在upstream节添加了least_conn配置。其它的配置同轮询配置。

3、IP地址哈希

前述的两种负载均衡方案中,同一客户端连续的Web请求可能会被分发到不同的后端服务器进行处理,因此如果涉及到会话Session,那么会话会比较复杂。常见的是基于数据库的会话持久化。要克服上面的难题,可以使用基于IP地址哈希的负载均衡方案。这样的话,同一客户端连续的Web请求都会被分发到同一服务器进行处理。
配置的例子如下:

http{
upstream sampleapp {
ip_hash;
server <<dns entry or IP Address(optional with port)>>;
server <<another dns entry or IP Address(optional with port)>>;
}
....
server{
listen ;
...
location / {
proxy_pass http://sampleapp;
}
}

上面的例子只是在upstream节添加了ip_hash配置。其它的配置同轮询配置。

4、基于权重的负载均衡

基于权重的负载均衡即Weighted Load Balancing,这种方式下,我们可以配置Nginx把请求更多地分发到高配置的后端服务器上,把相对较少的请求分发到低配服务器。
配置的例子如下:

http{
upstream sampleapp {
server <<dns entry or IP Address(optional with port)>> weight=;
server <<another dns entry or IP Address(optional with port)>>;
}
....
server{
listen ;
...
location / {
proxy_pass http://sampleapp;
}
}

上面的例子在服务器地址和端口后weight=2的配置,这意味着,每接收到3个请求,前2个请求会被分发到第一个服务器,第3个请求会分发到第二个服务器,其它的配置同轮询配置。

还要说明一点,基于权重的负载均衡和基于IP地址哈希的负载均衡可以组合在一起使用。

我的配置,基于最简单的轮训:

#user  nobody;
worker_processes ; #error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #pid logs/nginx.pid; events {
worker_connections ;
} 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 ;
keepalive_timeout ; #gzip on; upstream testapp{
server
192.168.20.50:8001;
server 192.168.20.50:8002
;
}

server {
listen ;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / {
root html;
index index.html index.htm;
proxy_pass http://testapp;
} #error_page /.html; # redirect server error pages to the static page /50x.html
#
error_page /50x.html;
location = /50x.html {
root html;
} # proxy the PHP scripts to Apache listening on 127.0.0.1:
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:;
# 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 ;
# listen somename:;
# server_name somename alias another.alias; # location / {
# root html;
# index index.html index.htm;
# }
#} # HTTPS server
#
#server {
# listen 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;
# }
#} }

说明:基于两台服务器8001和8002。这种方式真的是轮训,在服务器控制台输出轮流访问,并且不会访问出现故障的服务器。

参考:

http://www.jb51.net/article/60523.htm(以上大部分内容转自此篇文章)

Nginx负载均衡配置实例(转)的更多相关文章

  1. Nginx负载均衡配置实例详解

    负载均衡是我们大流量网站要做的一个东西,下面我来给大家介绍在Nginx服务器上进行负载均衡配置方法,希望对有需要的同学有所帮助哦. 负载均衡 先来简单了解一下什么是负载均衡,单从字面上的意思来理解就可 ...

  2. Nginx负载均衡配置实例详解(转)

    负载均衡是我们大流量网站要做的一个东西,下面我来给大家介绍在Nginx服务器上进行负载均衡配置方法,希望对有需要的同学有所帮助哦. 负载均衡 先来简单了解一下什么是负载均衡,单从字面上的意思来理解就可 ...

  3. [转载]Nginx负载均衡配置实例详解

    负载均衡是我们大流量网站要做的一个东西,下面我来给大家介绍在Nginx服务器上进行负载均衡配置方法,希望对有需要的同学有所帮助哦. 负载均衡 先来简单了解一下什么是负载均衡,单从字面上的意思来理解就可 ...

  4. Nginx负载均衡配置实例

    面对高并发的问题,企业往往会从两个方面来解决.其一,从硬件上面,提升硬件的配置,增加服务器的性能:另外,就是从软件上,将数据库和WEB服务器分离,使数据库和WEB服务器都能够充分发挥各自的性能,并且二 ...

  5. Nginx做NodeJS应用负载均衡配置实例

    这篇文章主要介绍了Nginx做NodeJS应用负载均衡配置实例,本文直接给出配置实例,需要的朋友可以参考下. 负载均衡可以把用户的请求分摊到多个服务器上进行处理,从而实现了对海量用户的访问支持.负载均 ...

  6. nginx高性能WEB服务器系列之六--nginx负载均衡配置+健康检查

    nginx系列友情链接:nginx高性能WEB服务器系列之一简介及安装https://www.cnblogs.com/maxtgood/p/9597596.htmlnginx高性能WEB服务器系列之二 ...

  7. Tomcat集群,Nginx集群,Tomcat+Nginx 负载均衡配置,Tomcat+Nginx集群

    Tomcat集群,Nginx集群,Tomcat+Nginx 负载均衡配置,Tomcat+Nginx集群 >>>>>>>>>>>> ...

  8. Nginx负载均衡配置简单配置方法

    http://www.jb51.net/article/121235.htm Nginx作为负载均衡服务器,用户请求先到达nginx,再由nginx根据负载配置将请求转发至不同的Web服务器.下面通过 ...

  9. Mall电商项目总结(二)——nginx负载均衡配置和策略

    1. nginx配置文件 用户在浏览器上输入,http://www.xwld.site/ 实际上是在访问服务器80端口,nginx 监听80端口,将用户的请求转发到8080和9080端口 . upst ...

随机推荐

  1. 用xtrabackup实现mysql的主从复制 阿里云rds到自己创建mysql

    来源 http://blog.51cto.com/825536458/1803968参考https://segmentfault.com/a/1190000003063874 如果我们用传统的mysq ...

  2. 利用Resttemplate进行put请求

    开发中,最常用的是post.get这两种.今天我给大家展示一个利用put请求的demo,其实put请求跟post请求没啥区别,但是没有返回值. void put(String var1, @Nulla ...

  3. 记如何解决蓝桥杯中to_string和atoi等无法使用的问题

    #include<iostream> #include<sstream> using namespace std; int main() {   // int 转 string ...

  4. CodeForces:699B-One Bomb

    B. One Bomb time limit per test1 second memory limit per test256 megabytes Problem Description You a ...

  5. java null 空指针

    对于Java程序员来说,null是令人头痛的东西.时常会受到空指针异常(NPE)的骚扰.连Java的发明者都承认这是他的一项巨大失误.Java为什么要保留null呢?null出现有一段时间了,并且我认 ...

  6. 项目-开发手机app

    一.  安装Hbuilder,和夜神安卓模拟器 注:夜神模拟器,如过windows中安装了hyper-v,需要卸载,不然会死机 二. Hbuilder简介 官网:http://www.dcloud.i ...

  7. vs系列自带的localdb数据库当做数据库

    我在学习mvc4框架的时候,因为是笔记本,觉得装了一个vs就很大了,再装一个sql server 就更麻烦,因此,就想到使用vs自带的localDB,直接生成预览的时候是没有问题的,但是当把mvc4的 ...

  8. 【03】图解原型和原型链by魔芋

    [03]图解原型和原型链 一图胜前言             请先结合图解原型和原型链这张图. 可以分为4种情况. 情况1: Object有: constructor:是Function. __pro ...

  9. 逻辑回归(Logistic Regression)算法小结

    一.逻辑回归简述: 回顾线性回归算法,对于给定的一些n维特征(x1,x2,x3,......xn),我们想通过对这些特征进行加权求和汇总的方法来描绘出事物的最终运算结果.从而衍生出我们线性回归的计算公 ...

  10. [android篇]声明权限

    要实施您自己的权限,必须先使用一个或多个 <permission> 元素在 AndroidManifest.xml 中声明它们. 实际上,在开发过程中,当我们使用了某些系统特性的功能,且此 ...