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的更多相关文章

  1. nginx做负载均衡器以及proxy缓存配置 - SegmentFault

    nginx做负载均衡器以及proxy缓存配置 - SegmentFault nginx做负载均衡器以及proxy缓存配置

  2. nginx 报错 upstream timed out (110: Connection timed out)解决方案【转】

    转自 nginx 报错 upstream timed out (110: Connection timed out)解决方案 - 为程序员服务http://outofmemory.cn/code-sn ...

  3. nginx基本配置与参数说明以及Nginx中的upstream轮询机制介绍

    转自:http://blog.csdn.net/happydream_c/article/details/54943802 一.nginx简介 Nginx (发音为[engine x])专为性能优化而 ...

  4. nginx负载均衡upstream参数配置

    一定要注意两台机器能够telnet 访问通过  如果不能通过则两台机器都执行一下 iptables -F 机器A: php-fpm配置[www]user = wwwgroup = wwwlisten ...

  5. 利用Nginx中的Upstream模块配置服务器负载均衡

    1. 前言 nginx有一个最大的功能就是可以实现服务器的负载均衡,本篇博文就利用nginx中的upstream模块来配置一个简单的负载均衡.关于nginx的安装和配置文件可以查阅博文:windows ...

  6. 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 ...

  7. nginx 报错 upstream timed out (110: Connection timed out)解决方案

    nginx 作PHP的web接口服务器. 在线上发现时不时经常崩溃.504,导致接口访问无响应回复. 查看日志: [error] 11618#0: *324911 upstream timed out ...

  8. Nginx中的upstream轮询机制介绍

    Nginx中upstream有以下几种方式: 1.轮询(weight=1) 默认选项,当weight不指定时,各服务器weight相同, 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器d ...

  9. nginx+webpy 出现 upstream timed out

    关于nginx配置webpy应用出现的错误 upstream timed out (: Connection timed out) while reading response header from ...

随机推荐

  1. Category 的一些事

    来源:伯乐在线 - Tsui YuenHong 链接:http://ios.jobbole.com/90422/ 点击 → 申请加入伯乐在线专栏作者 新增实践部分:偏方 Hook 进某些方法来添加功能 ...

  2. NFS安装及优化过程--centos6.6

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  3. Ubuntu下架设FTP服务器(转)

    Ubuntu下架设FTP服务器 Linux下提供了很多的ftp服务器,这里我选用了安全,快速,简单的vsftpd作为FTP服务器.本文是我在自己的Ubuntu 10.10 -32 位系统下搭建的.搭建 ...

  4. TripleDES之C#和PHP之间加密解密

    在C#常用加密解密一文中,介绍了几个加密解密方法,其中有个如何使用对称加密算法DES,此次说下DES的升级版,TripleDES. DES和TripleDES之间的关系可以参考下面的博文. 对称加密D ...

  5. Android下Notification,样式style,主题theme的功能实现

    一:Notification 1.NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVIC ...

  6. ElementUI 按需引入坑爹的点记录

    官网说的是这样的: 但实际上,应该是这样修改: { "presets": [ ["env", { "targets": { "br ...

  7. AR模型与数据平稳性之间的关系

    作者:桂. 时间:2017-12-19  21:39:08 链接:http://www.cnblogs.com/xingshansi/p/8068021.html 前言 前几天碰到一个序列分析的问题, ...

  8. Hadoop 读取文件API报错

    Exception in thread "main" org.apache.hadoop.hdfs.BlockMissingException: Could not obtain ...

  9. rsync同步时出现rsync: failed to set times on “xxxx”: Operation not permitted

    今天在同步数据的时候提示rsync: failed to set times on “xxxx”: Operation not permitted,一般来说要不是服务器时间不对或者权限没有设置好,下面 ...

  10. 在谷歌浏览器中安装防广告的插件(abp)

    1.打开谷歌浏览器 2.打开 设置-->见到"扩展程序"--->在搜索框中搜索"adb"-->点击"Adblock plus&quo ...