背景:我有两台机器,分别是backend1(主机器)backend2(backup),大致如下:
upstream backend {
server backend1.example.com:8080;
server backend2.example.com:8080 backup;
}
我想实现的是 在服务backend1
机器如果链接超时或者5xx那么就直接到backend2
这台机器上,等backend1
机器如果恢复可以正常访问backend1
。以下是我在网上找的资料本地配置的
...
http {
...
upstream nuxt_upstream {
server backend1.example.com:8080 max_fails=3 fail_timeout=10s;
server backend2.example.com:8080 backup;
}
server {
listen 80;
server_name api;
location / {
# 配置响应服务器的错误类型
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_next_upstream_timeout 5s; # 重试总共的时间
proxy_next_upstream_tries 3; # 重试几次,包含第一次
proxy_pass http://nuxt_upstream;
proxy_set_header Host $host;
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;
# 配置相应服务器的超时时间
proxy_connect_timeout 2s; # 连接后台服务器的超时时间
proxy_read_timeout 2s; # 从后台服务器读取数据的超时时间
proxy_send_timeout 2s; # 向后台服务器发送数据的超时时间
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
但是我不是很清楚的有几个点:
- 当一个请求进入nginx的时候我通过
proxy_next_upstream
捕获了当前错误的类型进行上游服务的轮询,设置的proxy_next_upstream_timeout 5s
具体作用在哪?包含proxy_xx_timeout
的时间吗? - 我在网上查询得知
proxy_xx_timeout
配置是跟upstream
其中一个sever相关的?那么如果我在设置backend1.example.com:8080 max_fails=3 fail_timeout=10s;
里面的fail_timeout
对外面设置的proxy_xx_timeout
有冲突吗?他们的关系是什么?
总结
就是我在upstream
设置了 fail_timeout=10s
和在外面设置的 proxy_next_upstream_timeout 5s
加上proxy_xx_timeout 2s
他们执行的顺序是什么?什么冲突吗?
辛苦相关的linux大神解答一下!
1、 server backend1.example.com:8080 max_fails=3 fail_timeout=10s, 表示backend1在失败3次以后就被标记为不可用,标记为不可用后Nginx将在10秒内不再尝试连接它;
2、proxy_next_upstream_timeout 5s, nginx反代到upstream里面的server如果出现失败会继续重试连接下一个server,直到proxy_next_upstream_timeout的超时时间或者到达proxy_next_upstream_tries 次数。