繁体   English   中英

在机架应用程序中,如何确定正在运行的Web服务器?

[英]Within a rack application, how can I tell which web server is running?

在机架应用程序中,如何确定哪个Web服务器作为机架处理程序运行?

例如,从config.ru中,我要打开是否正在运行WEBrick:

unless running_webrick?
  redirect_stdout_and_stderr_to_file
end

run App

def running_webrick?
   ???
end

传递到堆栈中每个组件的环境哈希具有SERVER_SOFTWARE密钥。 运行此命令并观察网页上的输出:

require 'rack'

class EnvPrinter

  def call env
    [200, {'content-type' => 'text/plain'}, [env.inspect]]
  end

end

run EnvPrinter.new

如果使用rackup执行,则将使用webrick作为服务器(这是默认设置),而SERVER_SOFTWARE将类似于WEBrick/1.3.1 (Ruby/1.9.3/2013-01-15) 如果使用独角兽,它将类似于Unicorn 4.5.0 此机架式代码根据运行在哪个服务器上返回自定义响应:

require 'rack'

class EnvPrinter

  def call env
    response = case env.fetch('SERVER_SOFTWARE')
               when /webrick/i then 'Running on webrick'
               when /unicorn/i then 'Running on unicorn'
               when /thin/i then 'Running on thin'
               else "I don't recognize this server"
               end
    [200, {'content-type' => 'text/plain'}, [response]]
  end

end

run EnvPrinter.new

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM