Getaddrinfo: 提供了 nodename 或 servname,或者不知道

我有一个 Ruby on Rails 应用程序,我正在一台运行 Mac OS X 10.6的计算机上部署它。出现问题的代码由 late _ job 运行。问题只有在延迟 _ 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 是假的,但是与真正的 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 `'
155031 次浏览

当 DNS 解析失败时发生错误。检查是否可以从命令行 wget (或 curl) api url。更改 DNS 服务器并测试它可能会有所帮助。

Rest-client 的 RestClient在解析 URL 时需要 http:方案。它为您调用 Net::HTTP,它不需要 http:部分,但 rest-client 负责这一点。

URL 是您试图访问的实际 URL 吗?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>

今天我也遇到了类似的情况——将一个应用程序部署到 mac os x 服务器上,当我试图访问一个外部 API 时,收到了“ getaddrinfo”消息。原来,当最初启动应用程序的 ssh 会话不再活动时,就会发生错误。这就是为什么在 ssh 进入服务器并手动运行命令(或手动启动服务器)时一切都运行良好的原因——只要保持 ssh 会话处于活动状态,就不会发生此错误。

这到底是 OSX 的一个 bug 还是一个怪癖,我不确定。这就是引导我找到解决方案的那一页—— 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"

帮了我大忙。

希望这对某些人有所帮助——弄清楚这个问题真的很痛苦!

我重新启动了我的电脑(Mac Mountain Lion) ,问题自己解决了。我认为这和外壳认为它与互联网断开有关。

以某种明确的方式重新启动 shell 也可以解决这个问题。然而,简单地打开一个新的会话/窗口并不起作用。

我看到这个错误与 Rails 无关。结果我的测试试图使用一个太高的端口(大于65535)。

这段代码将产生有问题的错误

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

我在试图开发时,断开了与互联网的连接,得到了错误。然而,我正在工作的网站需要能够与其他一些网站交谈,所以当它不能这样做的时候,它窒息了。连接到互联网修复了错误。

我通过关闭和重新打开终端来解决这个问题。

如果以上所有操作都失败,请尝试转换为 UNIX 行结束符,或者执行以下操作:

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

也许主机编码错了。

希望这能帮上忙

我在运行 rake db:create的时候出了点问题。这个页面给了我关于 DNS 问题的线索。我检查了我的 VPN 连接,发现它由于某种原因断开了。我恢复了联系,现在耙子工作顺利。

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

Bin/Railsserver-b 127.0.0.1

我得到了同样的错误,当我检查本地主机是设置在主机文件中,它是不知何故没有设置。将 localhost 设置为127.0.0.1解决了这个问题。

sudo vi /etc/hosts
>>


127.0.0.1       localhost

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

我原来有:

REDIS_HOST: 'redis'

然后改成了:

REDIS_HOST: 'localhost'

测试结果很好。

在我的 < strong > config/application.yml 中 我改变了这一切

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

这个

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

对我很有效