大网数据广告活动
服务器运维问答

Nginx sporadically fails to resolve hostname for proxy_pass

· 52CE 我爱测

I have a problem with my nginx reverse proxy setup: About 5% of requests get a 502 error, with a corresponding entry in the error.log reading: [error] 12314#12314: *749929 searxng.docker could not be resolved (3: Host not found),…

问题原文

原帖链接 · Shrugal · CC BY-SA 4.0

I have a problem with my nginx reverse proxy setup: About 5% of requests get a 502 error, with a corresponding entry in the error.log reading:

[error] 12314#12314: *749929 searxng.docker could not be resolved (3: Host not found), client: *redacted*, server: search.*redacted*, request: "GET / HTTP/2.0", host: "search.*redacted*"

So according to this the DNS resolution for the underyling SearXNG docker container failed. I have a DNS server on the same machine that maps *.docker domains to local docker containers, and running nslookup searxng.docker on it right before and after each request always returns the correct result (e.g. 172.17.0.3). The other ~95% of nginx requests work as well, so the DNS resolution seems to be fine.

Here is the server config (includes inlined):

server {
    listen 443 ssl;
    listen [::]:443 ssl;

    resolver 127.0.0.1 8.8.8.8;
 
    set $backend "http://searxng.docker:8080";

    server_name search.*redacted* ;

    ssl_certificate          *redacted*.pem;
    ssl_certificate_key      *redacted*.pem;
    ssl_protocols            TLSv1.2 TLSv1.3;
    ssl_ciphers              ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305;
    ssl_dhparam              *redacted*.pem;
    proxy_ssl_protocols      TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;

    location / {
        proxy_connect_timeout 60;
        proxy_read_timeout 60;
        proxy_send_timeout 60;
        proxy_intercept_errors off;
        proxy_http_version 1.1;
        proxy_set_header        Host               $http_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_pass $backend;
    }
}

And the /etc/resolv.conf

nameserver      127.0.0.1
nameserver      8.8.8.8
domain  *redacted*

The same proxy also serves other sites with pretty much identical configs, and all of them have this problem, so I don't think it's a problem with SearXNG specifically. I also tried a few setups for the DNS server. The device is a Synology NAS, so I first used their DNS server app (primary zone for docker domain, A records for containers). I also tried dnsmasq (default config) and CoreDNS (see below) both serving from the hosts file, as well as different TTL values from 10 seconds to 1 day. Resolving the *.docker hostnames worked with all of them, but nginx had the same problem with all of them too.

CoreDNS config (I tried 10, 60, 3600 and 86400 for TTL):

. {
    hosts {
        ttl 60
    }
}

Any idea what could cause nginx to fail to resolve the hostname seemingly at random, even though DNS resolution seems to work fine?

已采纳解答

原帖链接 · Shrugal · CC BY-SA 4.0

I think I found the problem: Removing the second DNS resolver from the nginx config seems to fix the issue. According to the docs nginx doesn't go through them one by one to find a hostname (as I thought), but picks one using RR. This means 50% of the time it picks Google's public DNS server, and ofc that can't answer requests for *.docker domains.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2024-01-15)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。