Ruby-on-rails getaddrinfo: nodename 或 servname 提供,或未知
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4132525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getaddrinfo: nodename nor servname provided, or not known
提问by ChronoPositron
I have a Ruby on Rails application that I am deploying on a computer running Mac OS X 10.6. The code where the problem arises is run by a delayed_job. The problem only occurs when it is run through delayed_job. If I run it within a console (rails console production) or call the API directly through cURL, it works without any problems. Also, the entire process works without issue in my development environment.
我有一个 Ruby on Rails 应用程序,我正在运行 Mac OS X 10.6 的计算机上部署它。出现问题的代码是由一个delayed_job 运行的。只有在通过delayed_job 运行时才会出现问题。如果我在控制台 ( rails console production) 中运行它或通过 cURL 直接调用 API,它可以正常工作。此外,整个过程在我的开发环境中没有问题。
Basically, the code works in one place, but for some reason, fails where it has to work. I've searched and found a few resources, but none of the suggestions apply or make any difference. When I log into the server and run the commands, everything works. But for some reason when they are run/started by Capistrano, it doesn't work.
基本上,代码在一个地方工作,但由于某种原因,在它必须工作的地方失败了。我搜索并找到了一些资源,但没有任何建议适用或有任何区别。当我登录服务器并运行命令时,一切正常。但是由于某种原因,当它们由 Capistrano 运行/启动时,它不起作用。
Any help is greatly appreciated.
任何帮助是极大的赞赏。
Important notes:
重要笔记:
- Ruby version: 1.9.2-p0
- Rails version: 3.0.1
- delayed_job version: latest (from collectiveidea/delayed_job)
- rest-client version: 1.6.1
- 红宝石版本:1.9.2-p0
- 导轨版本:3.0.1
- delay_job 版本:最新(来自collectionidea/delayed_job)
- 休息客户端版本:1.6.1
Code:
代码:
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 (note: the url is fake, but of the same form as the real one):
环境配置(注意: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.
Call where the error happens:
在发生错误的地方调用:
RestClient.get(API_URL, {:params => {:apinum => apinum}})
Error:
错误:
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 `'
采纳答案by Vijay Dev
The error occurs when the DNS resolution fails. Check if you can wget (or curl) the api url from the command line. Changing the DNS server and testing it might help.
当 DNS 解析失败时会出现此错误。检查是否可以从命令行 wget(或 curl)api url。更改 DNS 服务器并对其进行测试可能会有所帮助。
回答by Joshua Kunzmann
I ran into a similar situation today - deploying an app to a mac os x server, and receiving the 'getaddrinfo' message when I tried to access an external api. It turns out that the error occurs when the ssh session that originally launched the app is no longer active. That's why everything works perfectly if you ssh into your server and run commands manually (or launch the server manually) - as long as you keep your ssh session alive, this error won't occur.
我今天遇到了类似的情况 - 将应用程序部署到 mac os x 服务器,并在我尝试访问外部 api 时收到“getaddrinfo”消息。事实证明,当最初启动应用程序的 ssh 会话不再处于活动状态时,就会发生错误。这就是为什么如果您通过 ssh 进入服务器并手动运行命令(或手动启动服务器),一切都会完美运行的原因 - 只要您保持 ssh 会话处于活动状态,就不会发生此错误。
Whether this is a bug or a quirk in OS X, I'm not sure. Here's the page that led me to the solution - http://lists.apple.com/archives/unix-porting/2010/Jul/msg00001.html
这是 OS X 中的错误还是怪癖,我不确定。这是引导我找到解决方案的页面 - http://lists.apple.com/archives/unix-porting/2010/Jul/msg00001.html
All I had to do was update my capistrano task to launch the app using 'nohup'. So changing
我所要做的就是更新我的 capistrano 任务以使用“nohup”启动应用程序。如此变化
run "cd #{current_path} && RAILS_ENV=production unicorn_rails -c config/unicorn.rb -D"
to
到
run "cd #{current_path} && RAILS_ENV=production nohup unicorn_rails -c config/unicorn.rb -D"
did the trick for me.
帮我解决了这个问题。
Hope this helps somebody - it was quite a pain to figure out!
希望这对某人有所帮助 - 弄清楚这很痛苦!
回答by the Tin Man
rest-client's RestClientneeds the http:scheme when resolving the URL. It calls Net::HTTPfor you, which doesn't want the http:part, but rest-client takes care of that.
在解析 URL 时,rest-clientRestClient需要该http:方案。它需要Net::HTTP你,你不需要这http:部分,但 rest-client 会照顾到这一点。
Is the URL the actual one you are attempting to reach? example.org is a valid domain used for testing and documentation and is reachable; I'd expect the "api" and "api_endpoint" parts to fail and see that when I try to reach them.
该 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>'
Here's what I get using Curl:
这是我使用 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 "example.com",
"example.net","example.org"
or "example.edu" 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>
回答by Bagus Aji Santoso
I fixed this problem simply by closing and reopening the Terminal.
我只是通过关闭并重新打开终端来解决这个问题。
回答by coloradoblue
I restarted my computer (Mac Mountain Lion) and the problem fixed itself. Something having to do with the shell thinking it was disconnected from the internet I think.
我重新启动了我的计算机(Mac Mountain Lion),问题自行解决。我认为与外壳有关,认为它已与互联网断开连接。
Restarting your shell in some definite way may solve this problem as well. Simply opening up a new session/window however did not work.
以某种确定的方式重新启动 shell 也可以解决这个问题。简单地打开一个新的会话/窗口是行不通的。
回答by Mesut GUNES
I got the same error when I check the localhost is set in hosts file it is somehow not set. Setting localhost to 127.0.0.1 solved it.
当我检查主机文件中是否设置了 localhost 时,我遇到了同样的错误,但不知何故未设置。将 localhost 设置为 127.0.0.1 解决了它。
sudo vi /etc/hosts
>>
127.0.0.1 localhost
回答by David Zhang
To avoid this problem, we can bind to 127.0.0.1instead of localhost:
为了避免这个问题,我们可以绑定到127.0.0.1而不是localhost:
bin/rails server -b 127.0.0.1
bin/rails server -b 127.0.0.1
回答by Muir
I was seeing this error unrelated to rails. It turned out my test was trying to use a port that was too high (greater than 65535).
我看到这个错误与 rails 无关。结果我的测试试图使用太高的端口(大于 65535)。
This code will produce the error in question
此代码将产生有问题的错误
require 'socket'
Socket.getaddrinfo("127.0.0.1", "65536")
回答by Rockenstein2545
For me, I had to change a line of code in my local_env.ymlto get the rspec tests to run.
对我来说,我必须在我的代码中更改一行代码local_env.yml才能运行 rspec 测试。
I had originally had:
我原本有:
REDIS_HOST: 'redis'
REDIS_HOST: 'redis'
and changed it to:
并将其更改为:
REDIS_HOST: 'localhost'
REDIS_HOST: 'localhost'
and the test ran fine.
并且测试运行良好。
回答by Ruslan Valeev
In my config/application.ymlI have changed this
在我的config/application.yml我已经改变了这个
redis:
url: "redis://redis:6379/0"
to this
对此
redis:
url: "redis://localhost:6379/0"
and it works for me
它对我有用

