Nginx反向代理的使用
一、Nginx的基本命令
- nginx:启动 nginx
- nginx -t :测试配置文件是否有语法错误
- nginx -s reopen:重启Nginx
- nginx -s reload:重新加载Nginx配置文件,然后以优雅的方式重启Nginx
- nginx -s stop:强制停止Nginx服务
- nginx -s quit:优雅地停止Nginx服务
- nginx -v或-V 显示版本信息
- nginx -c filename : 设置配置文件
二、反向代理配置
1.max_fails 和 fail_timeout说明,默认1次 和 10秒 ,一般要调整的,否则容易误判。比如max_fails设置成3
(1)fail_timeout周期内,如果失败次数达到max_fails次,那么判定节点死掉。到达fail_timeout后又会自动恢复。
如果因为连接不上节点,判断全部节点死掉,那么报错:no live upstreams while connecting to upstream 返回给浏览器502 Bad Gateway
如果因为节点响应超时,判断全部节点死掉,那么报错:no live upstreams while reading response header from upstream 返回给浏览器502 Bad Gateway
节点未判断失效 且 响应超时返回的错误编码:504 Gateway Timeout
(2)Nginx记录失效节点的可能的6种状态(time out、connect refuse、500、502、503、504),Nginx 默认connect refuse和time out状态为准,不以HTTP错误状态进行判断失败。
但是可以配置,后四项5XX需要配置在proxy_next_upstream中的状态才可以生效。
说明:time out 包括连接超时(proxy_connect_timeout 默认60秒) 和 响应超时( proxy_read_timeout 默认60秒)。
2.重试逻辑:默认错误和超时会重试,而后端响应超时不会重试。
Syntax: proxy_next_upstream error | timeout | invalid_header | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | off ...;
Default: proxy_next_upstream error timeout; 默认错误和超时会重试。
Context: http, server, location
timeout:在与服务器建立连接,向其传递请求或读取响应头时发生超时,而不是业务处理时间。
Syntax: proxy_next_upstream_tries number; 重试次数
Default: proxy_next_upstream_tries 0; 默认为0,表示不限制。
Context: http, server, location