在 Debian 下使用 Nginx 时,如果日志中出现 DNS 解析失败(常见如 could not be resolved、host not found in upstream、no resolver defined 等),一般与 upstream / proxy_pass / fastcgi_pass 里的域名解析 有关。下面按原因 → 解决方案系统说明。
2026/01/15 10:00:00 [error] 1234#1234: *1 no resolver defined to resolve api.example.com,
client: 1.2.3.4, server: example.com, request: "GET / HTTP/1.1"
或
host not found in upstream "api.example.com"
或
could not be resolved (110: Operation timed out)
| 原因 | 说明 |
|---|---|
| ❌ 没有配置 resolver | Nginx 在 proxy_pass 使用变量或域名时必须显式指定 DNS |
| ❌ upstream 使用域名 | Nginx 启动时解析一次,DNS 变化不会自动更新 |
| ❌ DNS 服务器不可达 | /etc/resolv.conf 配置错误或 DNS 无法访问 |
| ❌ IPv6 DNS 问题 | Debian 默认启用 IPv6,DNS 不支持时会超时 |
| ❌ systemd-resolved 冲突 | 某些 Debian 版本下 /etc/resolv.conf 是 stub |
proxy_pass 使用 域名location /api/ {
resolver 8.8.8.8 1.1.1.1 valid=300s;
resolver_timeout 5s;
proxy_pass http://api.example.com;
}
✅ 注意
resolver 必须写在 location 或 server 块中http 块中(无效)upstream backend {
server api.example.com;
}
原因:
Nginx 启动时解析一次,DNS 变化不生效
upstream backend {
server 1.2.3.4:80;
}
location / {
resolver 8.8.8.8;
set $backend "api.example.com";
proxy_pass http://$backend;
}
cat /etc/resolv.conf
正常示例:
nameserver 8.8.8.8
nameserver 1.1.1.1
ls -l /etc/resolv.conf
若是软链接:
/etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.conf
✅ 解决方式(任选)
sudo ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf
resolver 127.0.0.53;
ping -6 api.example.com
如果卡住,可能是 IPv6 DNS 问题。
resolver 8.8.8.8 ipv6=off;
dig api.example.com
或
nslookup api.example.com
如果这里就失败,问题不在 Nginx,而在 DNS。
server {
listen 80;
server_name example.com;
location / {
resolver 8.8.8.8 1.1.1.1 valid=300s;
resolver_timeout 3s;
set $target "api.example.com";
proxy_pass http://$target;
}
}
1. Nginx 日志 → 是否 no resolver defined
2. proxy_pass 是否是域名
3. 是否使用 upstream + 域名
4. resolver 是否配置
5. /etc/resolv.conf 是否正常
6. 是否 IPv6 导致超时
你可以直接贴出:
cat /etc/resolv.conf我可以给你精确到行的修改方案 ✅