美文网首页
Nginx 反向代理与负载均衡

Nginx 反向代理与负载均衡

作者: Tinyspot | 来源:发表于2022-10-04 14:08 被阅读0次

1. 反向代理

1.1 反向代理配置

  • Module ngx_http_proxy_module
  • 默认已被编译进 Nginx
Syntax: proxy_pass URL;
Default:    —
Context:    location, if in location, limit_except

URL 必须以 http 或 https 开头

http {
    server {
        listen       80;
        server_name  localhost;
        location /nginx {
            proxy_pass http://localhost:8020/nginx/query;
        }
    }
}

1.2 proxy_pass 的 / 带与不带区别

  • 不带 / 表示 Nginx 不会修改用户 URL,直接透传给上游服务器
  • 带 / 表示 Nginx 会修改用户URL,会将 location 后的 URL 从用户 URL 中删除

示例:
请求URL: http://localhost:8020/nginx/query.json

location /nginx {
    proxy_pass http://localhost:8020;
}
# 到达服务器的请求 http://localhost:8020/nginx/query.json

location /nginx {
    proxy_pass http://localhost:8020/;
}
# 到达服务器的请求 http://localhost:8020/query.json

2. 负载均衡

  • 将请求代理到多台服务器去执行

在 IDEA 启动 多个服务,每次启动时改下端口 server.port=8020

idea-parallel-run.png

2.1 upstream 模块

  • Module ngx_http_upstream_module
  • 默认已被编译进 Nginx
  • 指令
    • upstream 定义服务 RUL
    • server 定义服务地址
    • zone 定义共享内存,用于跨 worker 子进程
    • keepalive 长连接的最大数量
    • keepalive_requests 单个长连接可以处理的HTTP请求数
    • keepalive_timeout 空闲长连接的最长保持时间
Syntax: upstream name { ... }
Default:    —
Context:    http

Syntax: server address [parameters];
Default:    —
Context:    upstream

Syntax: keepalive connections;
Default:    —
Context:    upstream

Syntax: keepalive_requests number;
Default:    
keepalive_requests 1000;
Context:    upstream

Syntax: keepalive_timeout timeout;
Default:    
keepalive_timeout 60s;
Context:    upstream
upstream urls {
    server 127.0.0.1:8020 weight=2 max_fail=3 fail_timeout=15;
    server 127.0.0.1:8021 weight=3;
    server 127.0.0.1:8022 backup;
    server 127.0.0.1:8022 down;
    keepalive 30;
}

server 参数详解

参数 作用
weight 默认为 1, weight 越大,权重越大
max_conns server 的最大并发连接数。默认为 0,表示不限制
fial_timeout 默认为 1,server 允许请求失败的次数,超过最大次数后,在 fail_timeout 时间内,新的请求不会分配给此机器
backup 其它所有非 backup 机器 down 或者忙的时候,请求 backup 机器
down 当前 server 不参与负载

2.3 轮询

http {
    upstream urls {
        server 127.0.0.1:8020;
        server 127.0.0.1:8021;
        server 127.0.0.1:8022;
    }

    server {
        listen       80;
        server_name  localhost;

        location / {
            proxy_pass http://urls;
        }
    }
}

测试

start nginx -c conf/nginx-load.conf
nginx -s quit
nginx -s reload

访问:http://localhost/nginx/query

2.4 权重

 upstream urls {
        server 127.0.0.1:8020 weight=2;
        server 127.0.0.1:8021 weight=3;
    }

3. 负载均衡算法

3.1 负载策略

  • wrr: weighted round-robin 权重轮询
  • ip_hash: 根据 IP 的哈希结果固定选择一个服务器
  • least_conn: 最少连接数算法,连接数越小越优先选择
  • 第三方负载均衡模块
    • fair

3.1 哈希算法

  • 将任意长度的进制值映射为固定长度的值;映射是不可逆的
Syntax: hash key [consistent];
Default:    —
Context:    upstream

示例:同一个 URL 会请求到同一个机器

upstream urls {
    hash $request_uri;
    server 127.0.0.1:8020;
    server 127.0.0.1:8021;
 }

3.1 ip_hash 算法

Syntax: ip_hash;
Default:    —
Context:    upstream

示例:

upstream urls {
    ip_hash;
    server 127.0.0.1:8020;
    server 127.0.0.1:8021;
 }

3.3 最少连接数算法

Syntax: least_conn;
Default:    —
Context:    upstream

通过共享内存实现

Syntax: zone name [size];
Default:    —
Context:    upstream

示例

upstream urls {
    zone cache 10M;
    least_conn;
    server 127.0.0.1:8020;
    server 127.0.0.1:8021;
}

4. 负载均衡服务器返回异常时的容错机制

4.1 proxy_next_upstream

Syntax: proxy_next_upstream error | timeout | invalid_header | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | http_429 | non_idempotent | off ...;
Default:    
proxy_next_upstream error timeout;
Context:    http, server, location
可选参数 描述
error
timeout
invalid_header 服务器返回无效响应
http_500
http_404
non_idempotent 非幂等请求失败时是否需要转发下一台服务器
off 禁用请求失败转发功能

测试,

http {
    upstream urls {
        server 127.0.0.1:8020;
        server 127.0.0.1:8022;
    }

    server {
        listen       80;
        server_name  localhost;
        location / {
            proxy_pass http://urls;
        }
    }
}

只启动一台机器 127.0.0.1:8020,但 nginx 配置里是两台
可以正常请求,因为默认是 proxy_next_upstream error timeout;

显式禁用,请求 8022的机器 会报错 502 Bad Gateway

location / {
    proxy_pass http://urls;
    proxy_next_upstream off;
}

相关文章

网友评论

      本文标题:Nginx 反向代理与负载均衡

      本文链接:https://www.haomeiwen.com/subject/zqfgurtx.html