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 ...
随机推荐
- memcached全面剖析--2.理解memcached的内存存储
下面是<memcached全面剖析>的第二部分. 发表日:2008/7/9 作者:前坂徹(Toru Maesaka) 原文链接:http://gihyo.jp/dev/feature/01 ...
- 树莓派UFW防火墙简单设置
ufw是一个主机端的iptables类防火墙配置工具,比较容易上手.如果你有一台暴露在外网的树莓派,则可通过这个简单的配置提升安全性. 安装方法 sudo apt-get install ufw 当然 ...
- POJ 3683 Priest John's Busiest Day (2-SAT)
Priest John's Busiest Day Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6900 Accept ...
- struts2 标签变形和 样式class无效 问题解决方法
在jsp使用Struts2标签的时候会发现,出现严重变形问题. <s:textfield type="text" name="username" labe ...
- 【struts2】Action的生命周期
Struts2的Action的生命周期是:Struts2为每个请求都重新初始化一个Action的实例.可以稍微改造一下代码来验证一下. 给HelloWorldAction加上一个public无参的构造 ...
- 一个Demo带你彻底掌握View的滑动冲突
本文已授权微信公众号:鸿洋(hongyangAndroid)在微信公众号平台原创首发. 近期在又一次学习Android自己定义View这一块的内容.遇到了平时开发中常常碰到的一个棘手问题:View的滑 ...
- 水晶报表填充.Net Objects数据源
Crystal Reports(水晶报表)是一款商务智能(BI)软件,主要用于设计及产生报表.是业内最专业.功能最强的报表系统. 查看网络资料及课本图书,鲜有介绍通过.NET Objects作为数据源 ...
- 讲讲python“=”运算符上的优雅语法
心路历程: 之前学linux,虽然学的行算不错,不过总感觉差了点什么,自己找不到也说不出来:直到有一天我看到别人mount上了一个普通文件: 当时给我的感觉这太不可思议了,这个文件又不是块设备:后来脑 ...
- msf web_delivery模块攻击
目标机:win7 ip:192.168.31.136 攻击机:kai liunx ip:192.168.31.54 一. ...
- haproxy 同一域名下分发请求
http://www.th7.cn/Program/java/201608/936162.shtml https://my.oschina.net/lockupme/blog/733375 还有一点要 ...