我可以使用 Thin 在 Sinatra 中启用 SSL 吗? [英] Can I enable SSL in Sinatra with Thin?

查看:36
本文介绍了我可以使用 Thin 在 Sinatra 中启用 SSL 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种简单的方法来在通过 Thin 运行的独立 Sinatra 应用程序中启用 SSL,而无需传递 --ssl--ssl-key-file--ssl-cert-file 参数通过 Thin 命令行输入.

I'm looking for a simple way to enable SSL in a standalone Sinatra application running via Thin without having to pass the --ssl, --ssl-key-file and --ssl-cert-file parameters in via the Thin command line.

是否可以直接在 Sinatra 应用程序中或通过 config.ru 文件定义它们?

Is it possible to define them directly within the Sinatra app or via a config.ru file?

我花了几个小时寻找这个问题的答案,但到目前为止还没有找到任何有效的方法.

I've spent several hours looking for an answer to this question, but so far have not found anything that works.

推荐答案

我只是花了几个小时试图自己解决这个问题.

I just spent a few hours trying to figure this one out myself.

事实证明,Thin::Server.initialize 在它的 initialization 方法中丢弃了 ssl 选项(它委托给它的 Backend,它立即将其 ssl 设置为 nil,忽略您传递给 Thin::Server.new.这意味着您必须在实例化服务器之后设置 ssl 选项.)

It turns out that Thin::Server.initialize discards ssl options during its initialization method (it delegates to its instance of Backend, which immediately sets its ssl to nil, ignoring any ssl options you've passed into Thin::Server.new. This means you have to to set ssl options after you've instantiated a server.)

方法如下:

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!

这篇关于我可以使用 Thin 在 Sinatra 中启用 SSL 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆