ruby-on-rails - 配置 WEBrick 以使用自动生成的自签名 SSL/HTTPS 证书

标签 ruby-on-rails ruby ssl https webrick

我想使用 SSL/HTTPS 在本地开发我的 Ruby on Rails 应用程序,但我在尝试设置服务器以使用 SSL 时遇到了问题。以下是我到目前为止已经尝试过的事情:

rails 服务器 [选项]

rails server 命令没有 ssl 选项(rails server --help):

Usage: rails server [mongrel, thin, etc] [options]
  -p, --port=port         Runs Rails on the specified port.
                          Default: 3000
  -b, --binding=ip        Binds Rails to the specified ip.
                          Default: 0.0.0.0
  -c, --config=file       Use custom rackup configuration file
  -d, --daemon            Make server run as a Daemon.
  -u, --debugger          Enable the debugger
  -e, --environment=name  Specifies the environment to run this server under
                          (test/development/production).
                          Default: development
                          
  -P, --pid=pid           Specifies the PID file.
                          Default: tmp/pids/server.pid

  -h, --help              Show this help message.

具有自动生成的自签名 SSL 证书的自定义 WEBrick 实例

我的代码

跟随 WEBrick documentation for HTTPS ,我制作了以下作为 ruby server.rb 运行的 Ruby 脚本:

require 'webrick'
include WEBrick

root = File.expand_path './public'

cert_name = [
  %w[CN localhost],
]

server = HTTPServer.new(
  :BindAddress => '127.0.0.1',
  :Port => '4430',
  :DocumentRoot => root,
  :SSLEnable => true,
  :SSLCertName => cert_name # LOOK! SSLCertName IS SET!
)

# Shutdown gracefully on signal interrupt CTRL-C
# http://www.ruby-doc.org/core-2.1.1/Kernel.html#method-i-trap
trap('INT') { server.shutdown }

server.start

根据我上面链接的文档:

This will start the server with a self-generated self-signed certificate.

并根据the documentation for WEBrick::Config ,

WEBrick can automatically create a self-signed certificate if :SSLCertName is set.

错误

当我启动服务器时,我得到以下输出:

INFO  WEBrick 1.3.1
INFO  ruby 2.1.1 (2014-02-24) [x86_64-darwin13.0]
INFO  WEBrick::HTTPServer#start: pid=26059 port=4430

但是,当我尝试访问 https://localhost:4430/robots.txt 时,我在 Chrome 33.0.1750.117 中收到以下错误:

enter image description here

当我在 Firefox 27.0.1 中尝试相同的 url 时出现以下错误:

enter image description here

我查找了 ssl_error_rx_record_too_long错误,看起来它可能是由一些不同的事情引起的。也许 WEBrick 仍在端口 80 上监听 HTTP 请求,但考虑到我明确将其设置为在端口 4430 上启用 SSL,这似乎很奇怪。

访问日志

此外,这是我从 Chrome 请求 https://localhost:4430/robots.txt 时来自 WEBrick 的访问日志内容,但我不知道其中的任何含义(它看起来像是用十六进制或其他方式编码的):

ERROR bad Request-Line `\x16\x03\x01\x02\x00\x01\x00\x01ü\x03\x03S\x15ußð'¦\x14·áÚOá,j\x7FÅ=üüNn#\x02ëý\x0Fø‚\x00\x00(À+À/\x00žÌ\x14Ì\x13\x00œÀ'.
localhost - - [04/Mar/2014:01:42:39 EST] "\x16\x03\x01\x02\x00\x01\x00\x01ü\x03\x03S\x15ußð'¦\x14·áÚOá,j\x7FÅ=üüNn#\x02ëý\x0Fø‚\x00\x00(À+À/\x00žÌ\x14Ì\x13\x00œÀ" 400 417
- -> 
ERROR bad Request-Line `\x16\x03\x01\x02\x00\x01\x00\x01ü\x03\x02S\x15ußj\x05ç©!€¿'ÄÃåë!t…ß\x06pDÒÒ4?”»7\x19\x00\x00\x1EV\x00À'.
localhost - - [04/Mar/2014:01:42:39 EST] "\x16\x03\x01\x02\x00\x01\x00\x01ü\x03\x02S\x15ußj\x05ç©!€¿'ÄÃåë!t…ß\x06pDÒÒ4?”»7\x19\x00\x00\x1EV\x00À" 400 398
- -> 
ERROR bad Request-Line `\x16\x03\x01\x02\x00\x01\x00\x01ü\x03\x01S\x15ußñom¾u<n¨ý9yö“¤Øcƒ{½wh)M@š1;\x00\x00\x1EV\x00À'.
localhost - - [04/Mar/2014:01:42:39 EST] "\x16\x03\x01\x02\x00\x01\x00\x01ü\x03\x01S\x15ußñom¾u<n¨ý9yö“¤Øcƒ{½wh)M@š1;\x00\x00\x1EV\x00À" 400 392
- -> 
ERROR bad URI `\x04ËB¿É\\ ˆ2ðiwñ·*\x02\x06^´\x00@v\x00\x00\x14\x00ÿV\x00\x009\x005\x003\x002\x00\x05\x00\x04\x00/\x00'.
localhost - - [04/Mar/2014:01:42:39 EST] "\x16\x03\x00\x00?\x01\x00\x00;\x03\x00S\x15uß…N®ˆ\r\x04ËB¿É\\ ˆ2ðiwñ·*\x02\x06^´\x00@v\x00\x00\x14\x00ÿV\x00\x009\x005\x003\x002\x00\x05\x00\x04\x00/\x00" 400 389
- -> \x04ËB¿É\\ ˆ2ðiwñ·*\x02\x06^´\x00@v\x00\x00\x14\x00ÿV\x00\x009\x005\x003\x002\x00\x05\x00\x04\x00/\x00

SSL 模块的 Ruby 源代码

此外,我检查了 Ruby source code for the SSL module ,但我没有看到任何明显的原因说明这可能不起作用:

def setup_ssl_context(config) # :nodoc:
  unless config[:SSLCertificate]
    cn = config[:SSLCertName]
    comment = config[:SSLCertComment]
    cert, key = Utils::create_self_signed_cert(1024, cn, comment) # LOOK HERE!
    config[:SSLCertificate] = cert
    config[:SSLPrivateKey] = key
  end
  # etc...
end

# Higher up in the file...
def create_self_signed_cert(bits, cn, comment)
  # etc ...
  cert = OpenSSL::X509::Certificate.new
  cert.version = 2
  cert.serial = 1
  name = OpenSSL::X509::Name.new(cn)
  cert.subject = name
  cert.issuer = name
  # etc ...
end

我的环境

以下是我用于开发的以下内容:

  1. OS X Mavericks。
  2. ruby 2.1.1。
  3. rails 4.0.3。

总结

这就是我目前所处的位置,我不确定如何继续。我知道我可以将我自己的自签名证书文件(使用 OpenSSL 之类的东西生成)传递给 WEBrick,但是文档说 WEBrick 可以自动生成它自己的证书文件,我真的很想让它发挥作用。

我也知道我可以使用不同的网络服务器,比如 Thin 及其 --ssl 选项,但我还是想使用 WEBrick,因为它是“out-of-the- box"的 Rails 网络服务器,我希望能够轻松快速地设置开发 SSL 网络服务器,而无需下载额外的 gem 和类似的东西。

我也知道 this solution存在,但同样,我对让 WEBrick 自动生成自己的证书很感兴趣(此外,该解决方案对于我正在尝试做的事情来说似乎有点过于复杂)。

那么有没有人知道哪里可能出了问题

最佳答案

好吧,我知道出了什么问题,我应该更加注意 the instructions for HTTPS in WEBrick ,这是示例中的确切代码:

require 'webrick'
require 'webrick/https' # SEE THIS?

cert_name = [
  %w[CN localhost],
]

server = WEBrick::HTTPServer.new(:Port => 8000,
                                 :SSLEnable => true,
                                 :SSLCertName => cert_name)

看到那行说 require 'webrick/https' 了吗?我的原始配置中没有。 我认为我不需要它。

添加后,我的脚本开始通过 HTTPS 提供服务,我终于可以连接到 https://localhost:4430/robots.txt。 <脸掌>

关于ruby-on-rails - 配置 WEBrick 以使用自动生成的自签名 SSL/HTTPS 证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22165854/

相关文章:

java - struts2强制将https url重定向到http

html - ruby rails : Increase value of object in scaffold with button_to

ruby-on-rails - 进行连接时选择唯一行

Ruby:替换捕获的字符串部分的优雅方式

ssl - 到 'wss://test.example.com:8090/' 的 WebSocket 连接失败 : WebSocket opening handshake timed out

php - 无法获取本地颁发者证书/SOAP-ERROR : Parsing WSDL: Couldn't load from

ruby-on-rails - 使用 Rails 和 Devise 进行功能测试。在我的固定装置中放什么?

ruby-on-rails - 如何使用RVM卸载gemset内除rails之外的所有gem?

ruby-on-rails - Rails 模型方法 : which is preferable?

ruby-on-rails - mark_for_destruction 在 before_save