简体   繁体   English

getaddrinfo: nodename 或 servname 提供,或未知

[英]getaddrinfo: nodename nor servname provided, or not known

I have a Ruby on Rails application that I am deploying on a computer running Mac OS X 10.6.我有一个 Ruby on Rails 应用程序,我正在运行 Mac OS X 10.6 的计算机上部署它。 The code where the problem arises is run by a delayed_job.出现问题的代码是由一个delayed_job 运行的。 The problem only occurs when it is run through delayed_job.只有在通过delayed_job 运行时才会出现问题。 If I run it within a console ( rails console production ) or call the API directly through cURL, it works without any problems.如果我在控制台( rails console production )中运行它或直接通过 cURL 调用 API,它可以正常工作。 Also, the entire process works without issue in my development environment.此外,整个过程在我的开发环境中没有问题。

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红宝石版本:1.9.2-p0
  • Rails version: 3.0.1导轨版本:3.0.1
  • delayed_job version: latest (from collectiveidea/delayed_job ) delay_job 版本:最新(来自collectionidea/delayed_job
  • rest-client version: 1.6.1休息客户端版本: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 `'

The error occurs when the DNS resolution fails.当 DNS 解析失败时会出现此错误。 Check if you can wget (or curl) the api url from the command line.检查是否可以从命令行 wget(或 curl)api url。 Changing the DNS server and testing it might help.更改 DNS 服务器并对其进行测试可能会有所帮助。

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.我今天遇到了类似的情况 - 将应用程序部署到 mac os x 服务器,并在我尝试访问外部 api 时收到“getaddrinfo”消息。 It turns out that the error occurs when the ssh session that originally launched the app is no longer active.事实证明,当最初启动应用程序的 ssh 会话不再处于活动状态时,就会发生错误。 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.这就是为什么如果您通过 ssh 进入服务器并手动运行命令(或手动启动服务器),一切都会完美运行的原因 - 只要您保持 ssh 会话处于活动状态,就不会发生此错误。

Whether this is a bug or a quirk in OS X, I'm not sure.这是 OS X 中的错误还是怪癖,我不确定。 Here's the page that led me to the solution - http://lists.apple.com/archives/unix-porting/2010/Jul/msg00001.html这是引导我找到解决方案的页面 - 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'.我所要做的就是更新我的 capistrano 任务以使用“nohup”启动应用程序。 So changing如此变化

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!希望这对某人有所帮助 - 弄清楚这很痛苦!

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

rest-client's RestClient needs the http: scheme when resolving the URL.在解析 URL 时,rest-client 的RestClient需要http:方案。 It calls Net::HTTP for you, which doesn't want the http: part, but rest-client takes care of that.它为您调用Net::HTTP ,它不需要http:部分,但 rest-client 会处理它。

Is the URL the actual one you are attempting to reach?该 URL 是您尝试访问的实际 URL 吗? example.org is a valid domain used for testing and documentation and is reachable; example.org 是用于测试和文档的有效域,并且可以访问; I'd expect the "api" and "api_endpoint" parts to fail and see that when I try to reach them.我希望“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 &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>

I restarted my computer (Mac Mountain Lion) and the problem fixed itself.我重新启动了我的计算机(Mac Mountain Lion),问题自行解决。 Something having to do with the shell thinking it was disconnected from the internet I think.我认为与外壳有关,认为它已与互联网断开连接。

Restarting your shell in some definite way may solve this problem as well.以某种确定的方式重新启动 shell 也可以解决这个问题。 Simply opening up a new session/window however did not work.简单地打开一个新的会话/窗口是行不通的。

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

bin/rails server -b 127.0.0.1

I got the same error when I check the localhost is set in hosts file it is somehow not set.当我检查主机文件中是否设置了 localhost 时,我遇到了同样的错误,但不知何故未设置。 Setting localhost to 127.0.0.1 solved it.将 localhost 设置为 127.0.0.1 解决了它。

sudo vi /etc/hosts
>>

127.0.0.1       localhost

I was seeing this error unrelated to rails.我看到这个错误与 rails 无关。 It turned out my test was trying to use a port that was too high (greater than 65535).结果我的测试试图使用太高的端口(大于 65535)。

This code will produce the error in question此代码将产生有问题的错误

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

For me, I had to change a line of code in my local_env.yml to get the rspec tests to run.对我来说,我必须在local_env.yml更改一行代码才能运行 rspec 测试。

I had originally had:我原本有:

REDIS_HOST: 'redis'

and changed it to:并将其更改为:

REDIS_HOST: 'localhost'

and the test ran fine.并且测试运行良好。

In my config/application.yml I 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它对我有用

I got the error while trying to develop while disconnected to the Internet.我在断开 Internet 连接的情况下尝试开发时遇到错误。 However, the website I was working on needs to be able to talk to some other websites, so it choked when it couldn't do so.但是,我正在处理的网站需要能够与其他一些网站对话,所以当它不能这样做时,它就卡住了。 Connecting to the internet fixed the error.连接到互联网修复了错误。

If all of the above fails, try to convert to UNIX Line Endings, or do:如果以上所有方法都失败,请尝试转换为 UNIX Line Endings,或执行以下操作:

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

Maybe the hosts encoding is wrong.也许主机编码是错误的。

hope this helps希望这可以帮助

I had this problem running rake db:create .我在运行rake db:create遇到了这个问题。 This page clued me into the DNS issue.这个页面让我了解了 DNS 问题。 I checked my VPN connection and found it was disconnected for some reason.我检查了我的 VPN 连接,发现它由于某种原因断开连接。 I reconnected and now rake worked without a hitch.我重新连接,现在耙子工作顺利。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 redis local SocketError:getaddrinfo:提供nodename或servname,或者不知道 - redis local SocketError: getaddrinfo: nodename nor servname provided, or not known 太阳黑子:重新索引错误-getaddrinfo:提供的节点名或服务名,或者未知 - sunspot:reindex error - getaddrinfo: nodename nor servname provided, or not known Ejabberd / Rails“ getaddrinfo:提供的节点名或服务名,或者未知” - Ejabberd / Rails “getaddrinfo: nodename nor servname provided, or not known” 启动问题 - getaddrinfo:提供nodename或servname,或者未知(SocketError) - start up issue — getaddrinfo: nodename nor servname provided, or not known (SocketError) / sidekiq / getaddrinfo中的SocketError:提供的nodename或servname,或者未知 - SocketError at /sidekiq/ getaddrinfo: nodename nor servname provided, or not known SocketError (getaddrinfo: nodename or servname provided, or not known): Rails Mailer - SocketError (getaddrinfo: nodename nor servname provided, or not known): Rails Mailer Rails 4.2 + Paperclip + Amazon s3:getaddrinfo:提供的节点名或服务名,或者未知 - Rails 4.2 + Paperclip + Amazon s3: getaddrinfo: nodename nor servname provided, or not known CHEF服务器-错误:网络错误:getaddrinfo:提供的节点名或服务名,或者未知 - CHEF Server--Error: Network Error: getaddrinfo: nodename nor servname provided, or not known rails服务器无法启动; getaddrinfo:提供的nodename或servname,或者未知(SocketError) - rails server cannot start; getaddrinfo: nodename nor servname provided, or not known (SocketError) Redis-To-Go难度-getaddrinfo:提供的节点名或服务名,或者未知,Rails 4 - Redis-To-Go difficulty - getaddrinfo: nodename nor servname provided, or not known, Rails 4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM