Nginx ngx_http_upstream_module[原]

本文介绍了Nginx中ngx_http_upstream_module模块的使用方法,包括如何定义服务器组进行负载均衡,以及ip_hash等特性配置示例。
Nginx ngx_http_upstream_module[原]
2012年01月08日
  模块官方解释:
  The ngx_http_upstream_module module allows to define groups of servers that can be referenced from the proxy_pass and fastcgi_pass directives. 
  总结:该模块主要用作做负载均衡时使用
  下面是一个配置例子:
  upstream [b]sv1 [/b]{
  server 127.0.0.1 : 8080;
  server 127.0.0.1 : 9999;
  }
  server {
  listen 8888;
  server_name localhost;
  charset utf - 8;
  location / {
  index index.jsp index.html;
  proxy_pass http: //[b]sv1;[/b]
  proxy_set_header Host $host;
  proxy_set_header X - Real - IP $remote_addr;
  proxy_set_header X - Forwarded - For $proxy_add_x_forwarded_for;
  }
  }
  语法介绍:
  (1)[b]ip_hash[/b]:
  官方解释:
  Specifies that a group should use a balancing method where requestsare distributed between servers based on client IP addresses.A class C network containing the client IP address is used as a hashing key.The method ensures that requests of the same client will always bepassed to the same server except when this server is considered downin which case client requests will be passed to another server andmost probably it will also be the same server.
  总结:每个request按访问ip的hash结果分配,这样每个request访问固定一个后端服务器,可以解决session同步的问题,但是存在单点问题。
  配置例子:
  upstream backend {
  ip_hash;
  server backend1.example.com;
  server backend2.example.com;
  server backend3.example.com [b]down[/b];
  server backend4.example.com;
  }
  上面加粗down的意思,标示该server已经宕机,这样请求就不会pass过去。这种适用于在生产线上临时移除某台已经宕机的server。要注意如果采用ip散列的方式做负载均衡,那么权重属性(weight--下面做解释)就不起作用了。
  (2)[b]server[/b] name [parameters]:
  官方解释:
  Sets a name and other parameters of the server.A name can be a domain name, an address, a port, or apath of the UNIX-domain socket.If a domain name resolves to several addresses, all of them are used.
  总结:设置服务名称,一般为域名或者ip地址,取决于你的负载架构。
  其他可选参数:
  1.down :当前的server不参与负载。
  2.weight :值越大,负载的权重就越大。 默认为1。
  3.max_fails :允许请求失败的次数,默认为1.
  4.fail_timeout:请求server超时时间,默认为10秒。
  5.backup:备份机器,当主服务器宕机,该备份机开始工作。
  配置实例:
  upstream backend {
  server backend1.example.com weight=5;
  server 127.0.0.1:8080 max_fails=3 fail_timeout=30s;
  server unix:/tmp/backend3;
  server backup1.example.com:8080 backup;
  }
  注意,设置了max_fails,当失败次数超过最大次数时,返回proxy_next_upstream和fastcgi_next_upstream模块定义的错误。404状态除外。
  
### Nginx Windows 下使用 ngx_http_upstream_check_module 模块 Nginx 的 Windows 版本默认并不包含第三方模块,如 `ngx_http_upstream_check_module`。该模块用于主动健康检查,能够定期探测后端服务器状态,并在服务器不可用时将其从负载均衡池中隔离,从而提升系统的稳定性和可用性。在 Windows 环境下使用该模块,需要手动编译或寻找已经集成该模块的 Nginx 构建版本。 由于官方提供的 Windows Nginx 发行版通常不包含此模块,因此用户需要自行编译 Nginx 并在配置中添加 `--add-module` 参数指向 `ngx_http_upstream_check_module` 的源码路径。例如: ```bash ./configure --with-http_ssl_module --add-module=/path/to/ngx_http_upstream_check_module ``` 在 Windows 上进行编译时,通常需要借助 MSYS 或 Cygwin 工具链,或者使用第三方构建工具链来完成。此外,也可以寻找社区维护的 Windows 构建版本,这些版本可能已经集成了 `ngx_http_upstream_check_module` 模块。 配置文件中可以使用如下方式定义带有健康检查的 upstream 块: ```nginx upstream backend { server 172.16.0.23:80; server 172.16.0.24:80; check interval=3000 rise=2 fall=3 timeout=1000 type=http; check_http_send "HEAD / HTTP/1.0\r\n\r\n"; check_http_expect_alive http_2xx http_3xx; } ``` 此配置表示每 3 秒检查一次后端服务器,连续 2 次成功标记为可用,连续 3 次失败标记为不可用,超时时间为 1 秒,并且通过 HTTP 请求进行探测。该模块的健康检查机制相较于默认的被动检查更为及时和主动,能有效避免请求发送到故障节点上[^4]。 --- ### 示例配置:使用 ngx_http_upstream_check_module ```nginx http { upstream cluster { server 172.16.0.23:80; server 172.16.0.24:80; check interval=5000 rise=2 fall=3 timeout=2000 type=http; check_http_send "GET /health HTTP/1.0\r\n\r\n"; check_http_expect_alive http_2xx; } server { listen 80; server_name example.com; location / { proxy_pass http://cluster; } } } ``` 上述配置中,Nginx 将每隔 5 秒向后端服务器发送一个 `/health` 请求进行健康检查。如果连续 3 次失败,则将该服务器标记为不可用;如果连续 2 次成功,则重新启用该服务器。该配置可以有效提升系统的可用性[^4]。 --- ### 注意事项 - 在 Windows 上构建 Nginx 并集成第三方模块时,需要确保开发环境配置正确,包括编译器、依赖库等。 - 由于官方未提供集成 `ngx_http_upstream_check_module` 的 Windows 构建版本,建议使用社区维护的构建版本或自行编译。 - 使用 `ngx_http_upstream_check_module` 模块时,需要在配置中明确启用并设置健康检查参数,以实现主动探测功能[^4]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值