Nginx作为负载均衡器upstream
Nginx中与proxy模块结合使用的模块中,最常用的当属upstream模块。upstream模块可定义一个新的上下文,它包含了一组upstream服务器,这些服务器可能被赋予了不同的权重、不同的类型甚至可以基于维护等原因被标记为down。
upstream模块常用的指令有:
ip_hash:基于客户端IP地址完成请求的分发,它可以保证来自于同一个客户端的请求始终被转发至同一个upstream服务器,实现会话保持;
keepalive:每个worker进程为发送到upstream服务器的连接所缓存的个数;
least_conn:最少连接调度算法;
server:定义一个upstream服务器的地址,还可包括一系列可选参数,如:
weight:权重;
max_fails:最大失败连接次数,失败连接的超时时长由fail_timeout指定;
fail_timeout:等待请求的目标服务器发送响应的时长;
backup:用于fallback的目的,所有服务均故障时才启动此服务器;
down:手动标记其不再处理任何请求;
upstream backend {
server backend1.example.com weight=5;
server backend2.example.com:8080;
server unix:/tmp/backend3;
server backup1.example.com:8080 backup;
server backup2.example.com:8080 backup;
}
server {
location / {
proxy_pass http://backend;
}
}
测试环境
nginx:192.168.2.168
httpd1:192.168.2.169
httpd2:192.168.2.170
nginx配置http中定义:
upstream webservice {
server 192.168.2.169 weight=1 max_fails=2 fail_timeout=5s;
server 192.168.2.170 weight=1 max_fails=2 fail_timeout=5s;
server 127.0.0.1:8080 backup;
}
server中定义
location ~* ^/bbs/ {
proxy_pass http://webservice;
proxy_set_header X-Real-IP $remote_addr;
}
另外定义back server:
server {
listen 8080;
server_name 127.0.0.1;
root /web/errorpages;
index index.html;
}
负载负载均衡:


upstream模块的负载均衡算法主要有三种,轮调(round-robin)、ip哈希(ip_hash)和最少连接(least_conn)三种。
注意:ip_hash被使用时,back server不能使用,wegiht失效
测试中,停掉httpd1,nginx自动检查server健康状况,只负载到httd2,再次停掉httd2,会负载到back server;

使用ip_hash算法:
upstream webservice {
ip_hash;
server 192.168.2.169 weight=1 max_fails=2 fail_timeout=5s;
server 192.168.2.170 weight=1 max_fails=2 fail_timeout=5s;
}
测试时只会负载到httpd2
此外,upstream模块也能为非http类的应用实现负载均衡,如下面的示例定义了nginx为memcached服务实现负载均衡之目的。
upstream memcachesrvs {
server 172.16.100.6:11211;
server 172.16.100.7:11211;
}
server {
location / {
set $memcached_key "$uri?$args";
memcached_pass memcachesrvs;
error_page 404 = @fallback;
}
location @fallback {
proxy_pass http://127.0.0.1:8080;
}
}
Nginx作为负载均衡器upstream的更多相关文章
- nginx做负载均衡器以及proxy缓存配置 - SegmentFault
nginx做负载均衡器以及proxy缓存配置 - SegmentFault nginx做负载均衡器以及proxy缓存配置
- nginx 报错 upstream timed out (110: Connection timed out)解决方案【转】
转自 nginx 报错 upstream timed out (110: Connection timed out)解决方案 - 为程序员服务http://outofmemory.cn/code-sn ...
- nginx基本配置与参数说明以及Nginx中的upstream轮询机制介绍
转自:http://blog.csdn.net/happydream_c/article/details/54943802 一.nginx简介 Nginx (发音为[engine x])专为性能优化而 ...
- nginx负载均衡upstream参数配置
一定要注意两台机器能够telnet 访问通过 如果不能通过则两台机器都执行一下 iptables -F 机器A: php-fpm配置[www]user = wwwgroup = wwwlisten ...
- 利用Nginx中的Upstream模块配置服务器负载均衡
1. 前言 nginx有一个最大的功能就是可以实现服务器的负载均衡,本篇博文就利用nginx中的upstream模块来配置一个简单的负载均衡.关于nginx的安装和配置文件可以查阅博文:windows ...
- nginx: [emerg] directive "upstream" has no opening "{" in /application/nginx-1.6.3/conf/nginx.conf:13 ...
修改nginx.conf配置文件时,报以下错误: [root@bqh-lb- nginx]# vim conf/nginx.conf [root@bqh-lb- nginx]# sbin/nginx ...
- nginx 报错 upstream timed out (110: Connection timed out)解决方案
nginx 作PHP的web接口服务器. 在线上发现时不时经常崩溃.504,导致接口访问无响应回复. 查看日志: [error] 11618#0: *324911 upstream timed out ...
- Nginx中的upstream轮询机制介绍
Nginx中upstream有以下几种方式: 1.轮询(weight=1) 默认选项,当weight不指定时,各服务器weight相同, 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器d ...
- nginx+webpy 出现 upstream timed out
关于nginx配置webpy应用出现的错误 upstream timed out (: Connection timed out) while reading response header from ...
随机推荐
- java 实现唯一ID生成器
2014-11-08 内容存档在evernote,笔记名"java 实现唯一ID生成器"
- Mac 安装任何来源的文件
1.Mac 安装任何来源的文件 安装软件提示文件损坏怎么处理,打开 DMG 文件提示损坏怎么处理,来自不信任的开发者怎么处理,macOS Sierra 如何安装任何来源的文件. 非常肯定的告诉您不是我 ...
- solrj索引操作
添加索引 Solr添加文档至索引: http://www.cnblogs.com/dennisit/p/3621717.html 删除索引: 每天索引记录有一个唯一标识,索引的删除通过唯一标识操作,如 ...
- 【Linux】双向重导向命令tee
想个简单的东西,我们知道 > 会将数据流整个传送给文件或装置,因此我们除非去读取该文件或装置, 否则就无法继续利用这个数据流.万一我想要将这个数据流的处理过程中将某段信息存下来,应该怎么做? 利 ...
- gitlab hook declined错误
在向gitlab提交工程的时候,出现错误提示: remote: GitLab: You are not allowed to access master!remote: error: hook dec ...
- [AaronYang风格]微软Unity2.X系统学习笔记,记录
读者约定: Unity我直接简写U了 Unity Dependency Injection(DI) 欢迎学习Unity,通过学完下面的几个流程的引导,你应该就可以很顺利的应用Unity到你的项目中去了 ...
- appium简明教程(10)——控件定位基础
狭义上讲,UI级的自动化测试就是让机器代替人去点来点去的过程. 但机器去点什么(点上面还是点左边),怎么点(是长按还是轻触),这些东西是必须由代码的编写者所指示清楚的. 控件定位就是解决机器点什么的问 ...
- 基于 Transaction 类的分布式显式事务
自.NET2.0以来增加了System.Transactions命名空间,为.NET应用程序带来了一个新的事务编程模型. 这个命名空间提供了几个依赖的TransactionXXX类.Transacti ...
- js 冷门的 label 语法
https://github.com/Tencent/vConsole/blob/dev/src/lib/query.js#L142 https://www.cnblogs.com/hjbky/p/6 ...
- mac 下python使用venv 虚拟环境
1.安装virtualenv :pip3 install virtualenv 2.创建虚拟环境命令:virtualenv --no-site-packages venv 在当前目录创建一个虚拟环境v ...