getaddrinfo:提供的节点名或服务名,或者未知


71

我有一个Ruby on Rails应用程序,正在部署在运行Mac OS X 10.6的计算机上。出现问题的代码是由delay_job运行的。仅当它通过delay_job运行时,才会出现此问题。如果我在控制台(rails console production)中运行它,或者直接通过cURL调用API,它就可以正常工作。而且,整个过程在我的开发环境中都可以正常工作。

基本上,代码在一个地方工作,但是由于某种原因,它在必须工作的地方失败。我已经搜索并找到了一些资源,但是没有任何建议适用或有所作为。当我登录服务器并运行命令时,一切正常。但是由于某些原因,当Capistrano运行/启动它们时,它不起作用。

任何帮助是极大的赞赏。

重要笔记:

码:

class CallApi < Struct.new(:num)
  def perform
    log "Entering perform"
    apinum = num || 5
    log "ApiNum = #{apinum}"
    results = attempt(2,10) do
      ActiveSupport::JSON.decode(RestClient.get(API_URL, {:params => {:apinum => apinum}}))
    end
    log "Results retrieved. (count: #{results.count})"
  end

  def log(message)
    Delayed::Worker.logger.info "[CallApi] #{Time.now} - #{message}"
  end
end

Environment Config(注意:URL是伪造的,但形式与真实的形式相同):

API_URL = "http://api.example.org/api_endpoint"
# Originally, I had "http://" before the beginning, but found a
# post mentioning that Net::Http.start didn't like that.
# So I tried it both ways.
# The same error occurs regardless of if the "http://" is there.

呼叫发生错误的地方:

RestClient.get(API_URL, {:params => {:apinum => apinum}})

错误:

getaddrinfo: nodename nor servname provided, or not known
/Users/api/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/net/http.rb:644:in `initialize'
/Users/api/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/net/http.rb:644:in `open'
/Users/api/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/net/http.rb:644:in `block in connect'
/Users/api/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/timeout.rb:44:in `timeout'
/Users/api/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/timeout.rb:87:in `timeout'
/Users/api/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/net/http.rb:644:in `connect'
/Users/api/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/net/http.rb:637:in `do_start'
/Users/api/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/net/http.rb:626:in `start'
/Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/rest-client-1.6.1/lib/restclient/request.rb:166:in `transmit'
/Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/rest-client-1.6.1/lib/restclient/request.rb:60:in `execute'
/Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/rest-client-1.6.1/lib/restclient/request.rb:31:in `execute'
/Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/rest-client-1.6.1/lib/restclient.rb:68:in `get'
/private/var/www/project-gemset/releases/20101109002137/lib/call_api.rb:7:in `block in perform'
/Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/attempt-0.2.0/lib/attempt.rb:70:in `attempt'
/Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/attempt-0.2.0/lib/attempt.rb:114:in `attempt'
/private/var/www/project-gemset/releases/20101109002137/lib/call_api.rb:6:in `perform'
/Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/bundler/gems/delayed_job-aba9905764c2/lib/delayed/backend/base.rb:77:in `invoke_job'
/Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/bundler/gems/delayed_job-aba9905764c2/lib/delayed/worker.rb:118:in `block (2 levels) in run'
/Users/api/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/timeout.rb:57:in `timeout'
/Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/bundler/gems/delayed_job-aba9905764c2/lib/delayed/worker.rb:118:in `block in run'
/Users/api/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/benchmark.rb:309:in `realtime'
/Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/bundler/gems/delayed_job-aba9905764c2/lib/delayed/worker.rb:117:in `run'
/Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/bundler/gems/delayed_job-aba9905764c2/lib/delayed/worker.rb:176:in `reserve_and_run_one_job'
/Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/bundler/gems/delayed_job-aba9905764c2/lib/delayed/worker.rb:102:in `block in work_off'
/Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/bundler/gems/delayed_job-aba9905764c2/lib/delayed/worker.rb:101:in `times'
/Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/bundler/gems/delayed_job-aba9905764c2/lib/delayed/worker.rb:101:in `work_off'
/Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/bundler/gems/delayed_job-aba9905764c2/lib/delayed/worker.rb:76:in `block (2 levels) in start'
/Users/api/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/benchmark.rb:309:in `realtime'
/Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/bundler/gems/delayed_job-aba9905764c2/lib/delayed/worker.rb:75:in `block in start'
/Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/bundler/gems/delayed_job-aba9905764c2/lib/delayed/worker.rb:72:in `loop'
/Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/bundler/gems/delayed_job-aba9905764c2/lib/delayed/worker.rb:72:in `start'
/Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/bundler/gems/delayed_job-aba9905764c2/lib/delayed/command.rb:100:in `run'
/Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/bundler/gems/delayed_job-aba9905764c2/lib/delayed/command.rb:79:in `block in run_process'
/Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/daemons-1.1.0/lib/daemons/application.rb:250:in `call'
/Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/daemons-1.1.0/lib/daemons/application.rb:250:in `block in start_proc'
/Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/daemons-1.1.0/lib/daemons/daemonize.rb:199:in `call'
/Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/daemons-1.1.0/lib/daemons/daemonize.rb:199:in `call_as_daemon'
/Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/daemons-1.1.0/lib/daemons/application.rb:254:in `start_proc'
/Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/daemons-1.1.0/lib/daemons/application.rb:294:in `start'
/Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/daemons-1.1.0/lib/daemons/application_group.rb:159:in `block (2 levels) in start_all'
/Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/daemons-1.1.0/lib/daemons/application_group.rb:158:in `fork'
/Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/daemons-1.1.0/lib/daemons/application_group.rb:158:in `block in start_all'
/Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/daemons-1.1.0/lib/daemons/application_group.rb:157:in `each'
/Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/daemons-1.1.0/lib/daemons/application_group.rb:157:in `start_all'
/Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/daemons-1.1.0/lib/daemons/controller.rb:80:in `run'
/Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/daemons-1.1.0/lib/daemons.rb:193:in `block in run_proc'
/Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/daemons-1.1.0/lib/daemons/cmdline.rb:112:in `call'
/Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/daemons-1.1.0/lib/daemons/cmdline.rb:112:in `catch_exceptions'
/Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/gems/daemons-1.1.0/lib/daemons.rb:192:in `run_proc'
/Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/bundler/gems/delayed_job-aba9905764c2/lib/delayed/command.rb:78:in `run_process'
/Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/bundler/gems/delayed_job-aba9905764c2/lib/delayed/command.rb:72:in `block in daemonize'
/Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/bundler/gems/delayed_job-aba9905764c2/lib/delayed/command.rb:70:in `times'
/Users/api/.rvm/gems/ruby-1.9.2-p0@project-gemset/bundler/gems/delayed_job-aba9905764c2/lib/delayed/command.rb:70:in `daemonize'
script/delayed_job:5:in `'

尝试通过Postmark发送电子邮件时,我突然收到了此消息。事实证明,他们的DNS无法解决问题,因此这就是我们的Rails应用中出现的错误。
Joshua Pinter

Answers:


51

DNS解析失败时,会发生此错误。检查您是否可以从命令行获取(或卷曲)api URL。更改DNS服务器并对其进行测试可能会有所帮助。


2
尽管这似乎是技术上的问题,但我仍然不知为什么会发生这种情况。当我登录服务器并运行命令时,一切正常。但是由于某些原因,当Capistrano运行/启动它们时,它不起作用。由于我没有找到任何解决方法,因此我将接受此答案并给其他人投票。
ChronoPositron

3
从睡眠中唤醒我的Mac,运行一些黄瓜测试,并得到此错误。我先关闭了wifi,然后又打开了,而且效果很好。似乎确实与DNS相关,但已将其修复。
Matt Vukas 2015年

1
帮助检查是否要设置任何http代理
tweekran

10

今天,我遇到了类似的情况-将应用程序部署到Mac OS X服务器,并在尝试访问外部api时收到“ getaddrinfo”消息。原来,当最初启动该应用程序的ssh会话不再活动时,就会发生该错误。这就是为什么如果您将ssh插入服务器并手动运行命令(或手动启动服务器),则一切运行正常的原因-只要使ssh会话保持活动状态,就不会发生此错误。

我不确定这是OS X中的错误还是怪癖。这是导致我找到解决方案的页面-http://lists.apple.com/archives/unix-porting/2010/Jul/msg00001.html

我要做的就是更新我的capistrano任务,以使用“ nohup”启动该应用程序。所以改变

run "cd #{current_path} && RAILS_ENV=production unicorn_rails -c config/unicorn.rb -D"

run "cd #{current_path} && RAILS_ENV=production nohup unicorn_rails -c config/unicorn.rb -D"

为我做了把戏。

希望这对某人有帮助-弄清楚这是很痛苦的!


对于那些有这个问题的人,resque您可以使用rakenohup来解决问题。即bundle exec nohup rake resque:work。谢谢约书亚!
2013年

9

解析URL时,其余客户RestClient需要该http:方案。它需要Net::HTTP您,这不需要http:部分,但其余客户会负责。

该网址是您尝试访问的实际网址吗?example.org是用于测试和记录的有效域,并且可以访问;我希望“ api”和“ api_endpoint”部分失败,并在尝试到达它们时看到这一点。

require 'socket'

IPSocket.getaddress('example.org') # => "2620:0:2d0:200::10"
IPSocket.getaddress('api.example.org') # => 
# ~> -:7:in `getaddress': getaddrinfo: nodename nor servname provided, or not known (SocketError)
# ~>    from -:7:in `<main>'

这是我使用Curl得到的:

greg-mbp-wireless:~ greg$ curl api.example.org/api_endpoint
curl: (6) Couldn't resolve host 'api.example.org'
greg-mbp-wireless:~ greg$ curl example.org/api_endpoint
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /api_endpoint was not found on this server.</p>
<hr>
<address>Apache Server at example.org Port 80</address>
</body></html>
greg-mbp-wireless:~ greg$ curl example.org
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
  <META http-equiv="Content-Type" content="text/html; charset=utf-8">
  <TITLE>Example Web Page</TITLE>
</HEAD> 
<body>  
<p>You have reached this web page by typing &quot;example.com&quot;,
&quot;example.net&quot;,&quot;example.org&quot
  or &quot;example.edu&quot; into your web browser.</p>
<p>These domain names are reserved for use in documentation and are not available 
  for registration. See <a href="http://www.rfc-editor.org/rfc/rfc2606.txt">RFC 
  2606</a>, Section 3.</p>
</BODY>
</HTML>

我在问题中使用的API_URL只是一个伪示例网址,其格式与实际格式相同。使用真实的URL直接在服务器上运行这些命令可以正常工作并返回所有内容。似乎只有通过Capistrano启动delay_job时才会出现此问题。(我将编辑问题以对此进行说明)
ChronoPositron 2010年

啊。好。example.com和它的兄弟姐妹是用于文档的,但是许多人尝试将其用于测试,这不会使它们走得太远。
Tin Man 2010年


5

我重新启动计算机(Mac Mountain Lion),问题得以解决。我认为与外壳有关的问题以为外壳已与互联网断开连接。

以某种确定的方式重新启动您的Shell也可以解决此问题。但是,仅仅打开一个新的会话/窗口是行不通的。


从您的wifi /以太网断开连接并重新连接也可能有效。
coloradoblue 2013年

我尝试了sudo postfix start并不断收到错误。重新启动Mac工作。
kk1957'14 2014年

是的,当我开启和关闭VPN时,似乎偶尔会发生。一个新的终端窗口将其修复。
杰里米·默里

5

为了避免这个问题,我们可以绑定127.0.0.1而不是localhost

bin/rails server -b 127.0.0.1


这对我有用,但是为什么更改主机文件不能解决问题?关于原因有什么想法吗?
rangeOShun

@rangeOShun我也不知道。我的一个macOS始终存在此问题,不仅对于Rails而言,对于所有其他问题。
David Zhang

更新:实际上,我尝试了几次尝试来解决它,但不幸的是,所有尝试都失败了。重新安装macOS的作品。大声笑
David Zhang

5

当我检查在主机文件中设置了localhost时,出现了相同的错误,但是没有设置。将localhost设置为127.0.0.1可以解决该问题。

sudo vi /etc/hosts
>>

127.0.0.1       localhost

2

我看到了与Rails无关的错误。事实证明,我的测试尝试使用的端口过高(大于65535)。

此代码将产生有问题的错误

require 'socket'
Socket.getaddrinfo("127.0.0.1", "65536")

不!没问题
IIllIIll

1

对我来说,我必须更改一行代码local_env.yml才能运行rspec测试。

我最初有:

REDIS_HOST: 'redis'

并将其更改为:

REDIS_HOST: 'localhost'

测试运行正常。


1

在我的config / application.yml中, 我已经更改了

redis:
    url: "redis://redis:6379/0"

对此

redis:
    url: "redis://localhost:6379/0"

对我有用


而且我忘记更改此内容后执行redis实例... sob ...如果redis在本地环境中,这是正确的。
V-SHY

0

在断开与Internet的连接时尝试开发时出现错误。但是,我正在工作的网站需要能够与其他一些网站进行对话,因此当它无法这样做时,它会感到窒息。连接到互联网修复了该错误。


0

如果以上所有方法均失败,请尝试转换为UNIX行尾,或执行以下操作:

brew install dos2unix
sudo dos2unix -c mac /private/etc/hosts

也许主机编码是错误的。

希望这可以帮助


0

我遇到了这个问题rake db:create。该页面引导我进入DNS问题。我检查了我的VPN连接,发现它由于某种原因已断开连接。我重新连接,现在耙工作顺利。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.