一: nginx 负载均衡配置
其实负载均衡的意思很简单明了, 网上很多原理一大堆的解释, 可能看的似懂非懂. 这里本人画了一个简单粗暴的原理图, 仅供参考:
解释: 其实 nginx 作为一个轻量级, 高性能的 web server 主要可以干的就两件事情,
第一件事就是直接作为 http server(代替 apache, 对 PHP 需要 FastCGI 处理器支持); 第二件事就是作为反向代理服务器实现负载均衡
因为 nginx 在处理并发方面的优势, 现在这个应用非常常见. 当然了 Apache 的 mod_proxy 和 mod_cache 结合使用也可以实现对多台 app server 的反向代理和负载均衡, 但是在并发处理方面 apache 还是没有 nginx 擅长.
这里介绍一种实践负载均衡 + 健康检查的方法.
直接 vim /usr/local/nginx/conf/nginx.conf 修改 upstream 段配置文件:
- 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"';
- sendfile on;
- keepalive_timeout 65;
- upstream worldcup {
- server 10.124.25.28:8001;
- server 10.124.25.29:8001;
- }
nginx 配置文件详解的时候也提到了, 注意 upstream 后面接的关键词, 如果需要单台, 同端口负载不同请求的时候, 需要制定不同 upstream, 以下提供一个实践实例.
实践示例, 仅供参考, 不做解释:
- worker_processes 1;
- events {
- worker_connections 1024;
- }
- http {
- include mime.types;
- default_type application/octet-stream;
- #log_format main '$remote_addr - $remote_user [$time_local]"$request" '
- sendfile on;
- keepalive_timeout 65;
- upstream keep_one {
- server 192.168.1.1:8080 weight=1 max_fails=2 fail_timeout=30s;
- server 192.168.1.2:8080 weight=1 max_fails=2 fail_timeout=30s;
- }
- upstream keep_two {
- server 192.168.1.3:8081 weight=1 max_fails=2 fail_timeout=30s;
- server 192.168.1.4:8081 weight=1 max_fails=2 fail_timeout=30s;
- }
- server {
- listen 80;
- server_name localhost;
- location / {
- root html;
- index index.html index.htm;
- }
- location /one {
- root html;
- index index.html index.htm;
- proxy_pass http://keep_one/;
- proxy_set_header Host $http_host;
- proxy_set_header Cookie $http_cookie;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For
- $proxy_add_x_forwarded_for;
- proxy_set_header X-Forwarded-Proto $scheme;
- client_max_body_size 300m;
- }
- location /two {
- root html;
- index index.html index.htm;
- proxy_pass http://keep_two/;
- proxy_set_header Host $http_host;
- proxy_set_header Cookie $http_cookie;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_set_header X-Forwarded-Proto $scheme;
- client_max_body_size 300m;
- }
- }
- }
提供两个负载接口, 同台服务器, 同个 IP, 同个端口.
二: nginx 负载后端的监控检查
其实以上配置文件中已经配置了健康检查的例子, 以下介绍两种健康检查的方式.
方法一:
添加 upstream 的时候, 直接 ip+port 后接 weight=1 max_fails=2 fail_timeout=30s;
- ### 如以下代码
- upstream fastdfs_tracker {
- server 192.168.1.1:8080 weight=1 max_fails=2 fail_timeout=30s;
- server 192.168.1.2:8080 weight=1 max_fails=2 fail_timeout=30s;
- }
解释: weight 为配置的权重, 在 fail_timeout 内检查 max_fails 次数, 失败则剔除均衡.
方法二:
添加 upstream 的时候, 在最后一行添加
- ### 如以下代码:
- upstream test{ #负载均衡配置, 默认的策略, 按时间先后, 有其他按 ip hash, 权重
- server 192.168.1.1:8080;
- server 192.168.1.2:8080;
- server 192.168.1.3:8080;
- check interval=3000 rise=2 fall=3 timeout=3000 type=http port=7070;
- }
解释:# interval=3000: 间隔 3 秒检查一次, rise=2: 检查 2 次 ok 后端节点 up,fall=3: 三次检查失败后端节点 down,timeout=3000: 超时时间 3 秒, type=http: 发 http 检查请求类型, port=8080 检查端口, 可省略, 默认和 server 192.168.1.1:8080 中的端口一致.
至此关于 nginx 最常用的负载均衡 + 健康检查已经配置完成, 后系列中还会介绍到相对常用的 nginx 的反向代理.
来源: https://www.cnblogs.com/maxtgood/p/9599068.html