type
status
date
slug
summary
tags
category
icon
password

1. 问题描述

如果在Nginx 中,“proxy_pass” 中配置的是域名地址,那么Nginx 只有在 start / restart / reload 时,才会连接一次域名服务器解析域名,缓存解析的结果,后续则不会根据解析结果的 TTL 进行自动更新。
如果遇到了域名地址配置有的IP在动态变化,例如使用DDNS对家用的动态公网IPV4/IPV6解析,那就会出现 Nginx 把请求转发到一个过期的 IP 地址的情况。
这个说法在官方博客中有提到:
As NGINX starts up or reloads its configuration, it queries a DNS server to resolve backends.example.com. The DNS server returns the list of three backends discussed above, and NGINX uses the default Round Robin algorithm to load balance requests among them. NGINX chooses the DNS server from the OS configuration file /etc/resolv.conf.
This method is the least flexible way to do service discovery and has the following additional drawbacks:
  • If the domain name can’t be resolved, NGINX fails to start or reload its configuration.
  • NGINX caches the DNS records until the next restart or configuration reload, ignoring the records’ TTL values.
  • We can’t specify another load‑balancing algorithm, nor can we configure passive health checks or other features defined by parameters to the server directive, which we’ll describe in the next section.
  • Nginx 在启动/重载的时候回去解析转发的域名
  • 如果域名无法解析 Nginx 就无法启动
  • 只有下次重启/重载的时候才会重新去解析,启动后无视TTL

2. 解决方案

我们可以指定 DNS 解析服务器并设置 DNS 刷新频率
其中,resolver 就是 DNS 服务器的地址(此处使用的是114.114.114.114,可以自己查询最近的DNS服务器), valid 是 DNS 刷新的频率。
一定要用关键词替换的方式查询DNS才会生效。

3. Reforence

CoreELEC系统遥控器按键适配免费网盘:InfiniCLOUD – 永久免费45GB,支持WebDAV
墨羽
墨羽
谁还不是个傻子呢?
公告
type
status
date
slug
summary
tags
category
icon
password
🎉博客轻量计划已启动🎉
-- 感谢您的支持 ---
内容迁移已完成
👏各功能正在逐步完善👏
请稍候…