2013-02-08 65 views
2

在机架应用程序中,如何判断哪个Web服务器作为Rack Handler运行?在机架应用程序中,我如何知道哪个Web服务器正在运行?

例如,从config.ru内,我想接通我是否正在运行的WEBrick:

unless running_webrick? 
    redirect_stdout_and_stderr_to_file 
end 

run App 

 

def running_webrick? 
    ??? 
end 
+0

我想知道这可能对您有所帮助。 http://stackoverflow.com/questions/7193635/change-default-server-for-rails – Vinay 2013-02-08 11:30:06

回答

0

环境哈希传递到每一个组件堆栈中有一个SERVER_SOFTWARE键。运行此并遵守网页上的输出:如果有rackup执行

require 'rack' 

class EnvPrinter 

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

end 

run EnvPrinter.new 

,在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 
相关问题