首页 > 系统相关 >Nginx超时重试、保护机制

Nginx超时重试、保护机制

时间:2024-07-02 16:31:15浏览次数:20  
标签:http next 重试 Nginx proxy timeout upstream 超时

1. 超时配置

http {
    upstream backend {
        server backend1.example.com;
        server backend2.example.com;
        server backend3.example.com;
    }
 
    server {
        location / {
            proxy_pass http://backend;
            proxy_connect_timeout 10s;
            proxy_send_timeout 10s;
            proxy_read_timeout 10s;
            proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        }
    }
}

  proxy_connect_timeout :定义了Nginx尝试与上游服务器建立连接的超时时间。

  proxy_send_timeout :定义了Nginx向上游服务器发送请求的超时时间。

  proxy_read_timeout : 定义了Nginx读取上游服务器响应的超时时间。

  proxy_next_upstream : 在指定的错误或超时发生时,Nginx会尝试将请求传递到下一个上游服务器,这里指定了会发生重试的错误类型。

 

 

proxy_next_upstream 语法:

  设置当连接upstream服务器集群中的某个服务器第一次失败时,指定在哪些情况下将请求传递到下一个服务器

语法:    proxy_next_upstream error | timeout | invalid_header | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | http_429 | non_idempotent | off ...;
默认:    proxy_next_upstream error timeout;
使用位置:    http, server, location
  • error # 与服务器建立连接,向其传递请求或读取响应头时发生错误;
  • timeout # 在与服务器建立连接,向其传递请求或读取响应头时发生超时;
  • invalid_header # 服务器返回空的或无效的响应;
  • http_500 # 服务器返回代码为500的响应;
  • http_502 # 服务器返回代码为502的响应;
  • http_503 # 服务器返回代码为503的响应;
  • http_504 # 服务器返回代码504的响应;
  • http_403 # 服务器返回代码为403的响应;
  • http_404 # 服务器返回代码为404的响应;
  • http_429 # 服务器返回代码为429的响应(1.11.13);
  • non_idempotent # 通常,请求与 非幂等 方法(POST,LOCK,PATCH)不传递到请求是否已被发送到上游服务器(1.9.13)的下一个服务器; 启用此选项显式允许重试此类请求;
  • off # 禁用将请求传递给下一个服务器。

 

2. 重试配置

2.1 重试配置,方式1

  默认配置是没有做重试机制进行限制的,也就是会尽可能去重试直至失败。

server {
    proxy_next_upstream error timeout;
    proxy_next_upstream_timeout 15s;
    proxy_next_upstream_tries 5;
}

proxy_next_upstream_timeout 语法:

  设置重试的超时时间,超时后不再重试,给用户返回错误,默认为0,即不做限制

语法:    proxy_next_upstream_timeout time;
默认:    proxy_next_upstream_timeout 0; 
使用位置:    http, server, location

 

proxy_next_upstream_tries 语法:

  设置重试的最大次数,若超过重试次数,也不再重试,默认为0,即不做限制(proxy_next_upstream_timeout时间内允许proxy_next_upstream_tries次重试,包括第一次)

语法:    proxy_next_upstream_tries number;
默认:    proxy_next_upstream_tries 0;
使用位置:    http, server, location

 

2.2 重试配置,方式2

  对upstream中某单一服务器的限制

  • max_fails:最大失败次数(0为标记一直可用,不检查健康状态)
  • fail_timeout:失败时间(当fail_timeout时间内失败了max_fails次,标记服务不可用fail_timeout时间后会再次激活次服务)
upstream httpget {
    server 192.168.111.101:8080 max_fails=5 fail_timeout=10s;
    server 192.168.111.102:8080;
}

 

2.3 整合示例

proxy_connect_timeout 3s;
proxy_next_upstream_timeout 6s;
proxy_next_upstream_tries 3;

upstream test {
    server 127.0.0.1:8001 fail_timeout=60s max_fails=2; # Server A
    server 127.0.0.1:8002 fail_timeout=60s max_fails=2; # Server B
    server 127.0.0.1:8003 fail_timeout=60s max_fails=2; # Server C
}

 

标签:http,next,重试,Nginx,proxy,timeout,upstream,超时
From: https://www.cnblogs.com/huanshilang/p/18280096

相关文章

  • 多个vue项目nginx部署流程
    nginx部署流程#在nginx.conf中配置#usernobody;worker_processes1;#error_loglogs/error.log;#error_loglogs/error.lognotice;#error_loglogs/error.loginfo;#pidlogs/nginx.pid;events{worker_connections1024;}http{incl......
  • Nginx跨域问题
    目录Nginx跨域实现跨域场景跨域问题的解决方案Nginx配置跨域解决Nginx跨域实现首先大家要搞清楚什么是跨域,为什么会有跨域情况的出现。哪些情况属于跨域?跨域:由于浏览器的同源策略,即属于不同域的页面之间不能相互访问各自的页面内容注:同源策略,单说来就是同协议,同域名,同端口UR......
  • 网络安全:Nginx安全问题使1400多万台服务器容易受到ddos攻击
    据外媒报道,近日nginx被爆出存在安全问题,有可能会致使1400多万台服务器易遭受DoS攻击。而导致安全问题的漏洞存在于HTTP/2和MP4模块中。新版本的NginxWeb服务器已于11月6日发布,用于修复影响1.15.6,1.14.1之前版本的多个安全问题,该漏洞允许潜在的攻击者触发拒绝服务(Do......
  • Nginx(openresty) X-Forwarded-For $proxy_add_x_forwarded_for 多层代理 通过map分割
    1nginx配置#配置多层反向代理,配置如下proxy_passhttp://ip或者域名/;proxy_connect_timeout60;proxy_send_timeout60;proxy_read_timeout60;proxy_set_headerUpgrade$h......
  • CH5XX串口中断接收超时
    1.串口1初始化:/*配置串口1:先配置IO口模式,再配置串口*/GPIOA_SetBits(GPIO_Pin_9);GPIOA_ModeCfg(GPIO_Pin_8,GPIO_ModeIN_PU);//RXD-配置上拉输入GPIOA_ModeCfg(GPIO_Pin_9,GPIO_ModeOut_PP_5mA);//TXD-配置推挽输出,注意先让IO口输出高电平U......
  • ingress-nginx部署-helm方式
    helm安装ingress-nginxIngress-NginxController支持多种方式安装:使用heml安装chart使用kubectlapply,使用YAML文件;详情可参考:https://kubernetes.github.io/ingress-nginx/deploy/本文实践使用helm安装ingress-nginx环境信息#k8s版本root@master1:~#kubectlgetno......
  • 生产环境部署Nginx服务器双机热备部署-keepalived(多种模式教程)
    前言:今天演示下生产环境keepalived的部署方式,安装模式有很多,比如说主备模型和双主模型,主备分:抢占模式和非抢占模式。这里我会一一展开说具体怎么配置一、双节点均部署Nginx:第一步:上传安装包到/usr/local/第二步:安装编译依赖(使用普通用户需要家sudo)yuminstallgccgcc-c......
  • nginx proxy_pass
    结果:http://abc.com/api/zwgrid/v1/export/laodongli结果:http://abc.com/api/zwgrid/v1/export/laodongli......
  • Windows上实现nginx的多负载,实现高可用,NLB 替代keepalived
    【转】https://blog.csdn.net/fcclzydouble/article/details/122841013NLB就是网络负载平衡,windowsServer2012中该功能允许你将传入的请求传播到最多达32台的服务器上,即可以使用最多32台服务器共同分担对外的网络请求服务。网络负载平衡,保证即使是在负载很重的情况下它们也能......
  • Ingress-Nginx 安装
    Ingress-Nginx安装配置一个Web服务器或者负载均衡器比想象的难。大多数Web服务的配置文件非常相似。有些应用需要一些奇怪的需求,但是在大多数的情况下,可以使用相同的逻辑以达到预期的效果。Ingress资源体现了这一思想,ingress控制器处理了上述奇怪的需求。Ingress控制......