我的服务器今天抛出了这个,这是一个我以前从未见过的Node.js错误:

Error: getaddrinfo EAI_AGAIN my-store.myshopify.com:443
    at Object.exports._errnoException (util.js:870:11)
    at errnoException (dns.js:32:15)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:78:26)

我想知道这是否与今天影响Shopify和许多其他服务的DynDns DDOS攻击有关。这里有一篇关于这个的文章。

我的主要问题是dns.js做什么?它属于节点的哪一部分?如何在不同的域上重新创建此错误?


当前回答

对于那些每天执行数千或数百万个请求,并且需要解决此问题的人:

在服务器上执行大量请求时,得到getaddrinfo EAI_AGAIN错误是很正常的。Node.js本身不执行任何DNS缓存,它委托所有与操作系统相关的DNS。

你需要记住,每个http/https请求都会执行一个DNS查找,这可能会变得相当昂贵,为了避免这个瓶颈和getaddrinfo错误,你可以实现一个DNS缓存。

http。请求(和https)接受一个默认为dns的查找属性。

http.get('http://example.com', { lookup: yourLookupImplementation }, response => {
    // do something here with response
});

我强烈建议使用一个已经测试过的模块,而不是自己编写DNS缓存,因为您必须正确处理TTL,以及其他一些事情,以避免难以跟踪错误。

我个人使用缓存查找,这是一个得到使用(见dnsCache选项)。

您可以在特定的请求中使用它

const http = require('http');
const CacheableLookup = require('cacheable-lookup');

const cacheable = new CacheableLookup();

http.get('http://example.com', {lookup: cacheable.lookup}, response => {
    // Handle the response here
});

或全球

const http = require('http');
const https = require('https');
const CacheableLookup = require('cacheable-lookup');

const cacheable = new CacheableLookup();

cacheable.install(http.globalAgent);
cacheable.install(https.globalAgent);

注意:请记住,如果一个请求不是通过Node.js的http/https模块执行的,在全局代理上使用.install不会对所述请求产生任何影响,例如使用undici发出的请求

其他回答

这是与hosts文件设置相关的问题。 在hosts文件中添加以下代码行 Ubuntu系统:/etc/hosts

127.0.0.1   localhost

windows: c:\windows\System32\drivers\etc\hosts

127.0.0.1   localhost

@xerq指向正确,这里有更多的参考 http://www.codingdefined.com/2015/06/nodejs-error-errno-eaiagain.html

我得到了同样的错误,我通过更新“hosts”文件在Windows操作系统的这个位置下解决了这个问题

C:\Windows\System32\drivers\etc

希望能有所帮助!!

在我的情况下,问题是docker网络的ip分配范围,详见这篇文章

EAI_AGAIN是一个DNS查找超时错误,意味着它是一个网络连接错误或代理相关的错误。

我的主要问题是dns.js做什么?

dns.js用于节点获取域的ip地址(简而言之)。

更多信息: http://www.codingdefined.com/2015/06/nodejs-error-errno-eaiagain.html

我得到这个错误后,我最近添加了一个新的网络到我的docker-compose文件。

我最初有这些服务:

services:
  frontend:
    depends_on:
      - backend
    ports:
      - 3005:3000
  
  backend:
    ports:
      - 8005:8000

我决定添加一个新的网络,它承载着我想让我的前端服务能够访问的其他服务,所以我这样做了:

networks:
  moar:
    name: moar-network
    attachable: true

services:
  frontend:
    networks:
      - moar
    depends_on:
      - backend
    ports:
      - 3005:3000
  
  backend:
    ports:
      - 8005:8000

不幸的是,上述情况导致我的前端服务在默认网络上不再可见,而只在moar网络中可见。这意味着前端服务不能再代理请求到后端,因此我得到如下错误:

试图代理到:localhost:3005/graphql/时发生错误

解决方案是将默认网络添加到前端服务的网络列表中,如下所示:

networks:
  moar:
    name: moar-network
    attachable: true

services:
  frontend:
    networks:
      - moar
      - default # here
    depends_on:
      - backend
    ports:
      - 3005:3000
  
  backend:
    ports:
      - 8005:8000

现在我们好了!


最后一件事,如果您想查看在给定网络中运行的服务,可以使用docker network inspect <network_name>命令。这帮助我发现前端服务不再是默认网络的一部分。