Nginx负载均衡配置实例(转)
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负载均衡配置实例(转)的更多相关文章
- Nginx负载均衡配置实例详解
负载均衡是我们大流量网站要做的一个东西,下面我来给大家介绍在Nginx服务器上进行负载均衡配置方法,希望对有需要的同学有所帮助哦. 负载均衡 先来简单了解一下什么是负载均衡,单从字面上的意思来理解就可 ...
- Nginx负载均衡配置实例详解(转)
负载均衡是我们大流量网站要做的一个东西,下面我来给大家介绍在Nginx服务器上进行负载均衡配置方法,希望对有需要的同学有所帮助哦. 负载均衡 先来简单了解一下什么是负载均衡,单从字面上的意思来理解就可 ...
- [转载]Nginx负载均衡配置实例详解
负载均衡是我们大流量网站要做的一个东西,下面我来给大家介绍在Nginx服务器上进行负载均衡配置方法,希望对有需要的同学有所帮助哦. 负载均衡 先来简单了解一下什么是负载均衡,单从字面上的意思来理解就可 ...
- Nginx负载均衡配置实例
面对高并发的问题,企业往往会从两个方面来解决.其一,从硬件上面,提升硬件的配置,增加服务器的性能:另外,就是从软件上,将数据库和WEB服务器分离,使数据库和WEB服务器都能够充分发挥各自的性能,并且二 ...
- Nginx做NodeJS应用负载均衡配置实例
这篇文章主要介绍了Nginx做NodeJS应用负载均衡配置实例,本文直接给出配置实例,需要的朋友可以参考下. 负载均衡可以把用户的请求分摊到多个服务器上进行处理,从而实现了对海量用户的访问支持.负载均 ...
- nginx高性能WEB服务器系列之六--nginx负载均衡配置+健康检查
nginx系列友情链接:nginx高性能WEB服务器系列之一简介及安装https://www.cnblogs.com/maxtgood/p/9597596.htmlnginx高性能WEB服务器系列之二 ...
- Tomcat集群,Nginx集群,Tomcat+Nginx 负载均衡配置,Tomcat+Nginx集群
Tomcat集群,Nginx集群,Tomcat+Nginx 负载均衡配置,Tomcat+Nginx集群 >>>>>>>>>>>> ...
- Nginx负载均衡配置简单配置方法
http://www.jb51.net/article/121235.htm Nginx作为负载均衡服务器,用户请求先到达nginx,再由nginx根据负载配置将请求转发至不同的Web服务器.下面通过 ...
- Mall电商项目总结(二)——nginx负载均衡配置和策略
1. nginx配置文件 用户在浏览器上输入,http://www.xwld.site/ 实际上是在访问服务器80端口,nginx 监听80端口,将用户的请求转发到8080和9080端口 . upst ...
随机推荐
- 博弈论入门 Bash 、Nim 、Wythoff's Game结论及c++代码实现
SG函数先不说,给自己总结下三大博弈.和二进制及黄金分割联系密切,数学真奇妙,如果不用考试就更好了. 1.Bash Game:n个物品,最少取1个,最多取m个,先取完者胜. 给对手留下(m+1)的倍数 ...
- bzoj5368 [Pkusc2018]真实排名
题目描述: bz luogu 题解: 组合数计数问题. 首先注意排名指的是成绩不小于他的选手的数量(包括他自己). 考虑怎么增大才能改变排名. 小学生都知道,对于成绩为$x$的人,让他自己不动并让$\ ...
- Linux-nginx服务(三)
nginx的安装 官方:http://nginx.org/packages/centos/7/x86_64/RPMS Fedora-EPEL:https://mirrors.aliyun.com/ep ...
- Linux内核——进程管理之CFS调度器(基于版本4.x)
<奔跑吧linux内核>3.2笔记,不足之处还望大家批评指正 建议阅读博文https://www.cnblogs.com/openix/p/3262217.html理解linux cfs调 ...
- subprocess模块windows系统命令和linux系统命令
windows系统 查看所有进程 tasklist 查找指定进程 tasklist | findstr pycharm 程序名称 PID(大写) 数量 大小 python exe 2640 conso ...
- 【实验吧】因缺思汀的绕过&&拐弯抹角&&Forms&&天网管理系统
<?php error_reporting(); if (!isset($_POST['uname']) || !isset($_POST['pwd'])) { echo '<form a ...
- ios开发中遇到的文件和字符的问题大总结
我今天遇到的NSString问题 今天遇到一个字符串空指针问题,让我明白了许多 其实我们定义一个NSString * string,其实是定义了一个字符串指针,现在string没有指向任何地方,我们必 ...
- vue 的 scroller 使用
一 安装 使用npm 安装npm install vue-scroller -d 二 引入 import VueScroller from "vue-scroller" Vue.u ...
- 【LeetCode】Reorder Log Files(重新排列日志文件)
这道题是LeetCode里的第937道题. 题目描述: 你有一个日志数组 logs.每条日志都是以空格分隔的字串. 对于每条日志,其第一个字为字母数字标识符.然后,要么: 标识符后面的每个字将仅由小写 ...
- 树状数组 gcd 查询 Different GCD Subarray Query
Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K ( ...