2012-07-10 23 views
7

我正在尋找一種簡單的方法來啓用通過Thin運行的獨立Sinatra應用程序中的SSL,而不必通過Thin命令行傳遞--ssl,和--ssl-cert-file參數。我可以在Thinat中啓用Sinatra中的SSL嗎?

是否可以直接在Sinatra應用程序中或通過config.ru文件定義它們?

我花了幾個小時尋找這個問題的答案,但到目前爲止還沒有找到任何可行的方法。

+0

如何在腳本文件中寫入命令行? – lidaobing 2012-07-10 02:48:59

回答

10

我剛花了幾個小時試圖找出自己的這一個。

事實證明,其initialization方法中Thin::Server.initialize丟棄ssl選項(這代表它的Backend情況下,能立刻將其sslnil,忽略你已經傳遞到Thin::Server.new任何SSL選項。這意味着你必須要。後您將一個服務器設置SSL選項

這裏是如何做到這一點:

class App < Sinatra::Base 

    # ... 

    def self.run! 
    rack_handler_config = {} 

    ssl_options = { 
     :private_key_file => '/path/to/foo.key', 
     :cert_chain_file => '/path/to/bar.crt', 
     :verify_peer => false, 
    } 

    Rack::Handler::Thin.run(self, rack_handler_config) do |server| 
     server.ssl = true 
     server.ssl_options = ssl_options 
    end 
    end 
end 

App.run! 
+0

一個非常有用的例子:-)謝謝 – rtacconi 2013-01-13 19:12:07

9

ŧ這些日子(sinatra 1.4.1)run!接受產生服務器的塊。所以,你可以這樣做:

MyApp.run! do |server| 
    ssl_options = { 
    :cert_chain_file => '/path/to/bar.crt', 
    :private_key_file => '/path/to/foo.key', 
    :verify_peer => false 
    } 
    server.ssl = true 
    server.ssl_options = ssl_options 
end 
+1

非常酷!這些選項在哪裏記錄? - 我似乎無法找到您的帖子以外的任何內容! – user1513388 2013-04-13 15:59:53

+1

http://rdoc.info/github/eventmachine/eventmachine/EventMachine/Connection:start_tls顯然是這裏發生的事情。 – Swizzlr 2013-12-16 17:05:30

0

我使用Rack::SslEnforcer與SSL運行西納特拉和薄(在Heroku上),這樣做:

if production? 
    require 'rack/ssl-enforcer' 
    use Rack::SslEnforcer 
end 

這應該是您啓用前:在您的會議文件。

相關問題