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

时间:2014-03-04 07:44:54

标签: ruby-on-rails ruby ssl https webrick

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

rails server [options]

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

根据我上面链接的文件:

  

这将使用自生成的自签名证书启动服务器。

并根据the documentation for WEBrick::Config

  

如果设置了SSLCertName,WEBrick可以自动创建自签名证书。

错误

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

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中尝试相同网址时出现以下错误:

enter image description here

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

访问日志

此外,当我从Chrome发出https://localhost:4430/robots.txt请求时,这里是WEBrick的访问日志内容,但我不知道它的含义是什么(看起来它是用十六进制编码的东西):< / p>

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。
  4. 摘要

    所以这就是我目前所处的位置,我不知道该怎么办。我知道我可以将自己的自签名证书文件(用OpenSSL之类的东西生成)传递给WEBrick,但文档说WEBrick可以自动生成自己的,我真的很想让它工作。

    我也知道我可以使用不同的网络服务器,例如Thin和--ssl选项,但同样,我想使用WEBrick,因为它是“开箱即用”的Web服务器Rails,我希望能够轻松快速地设置开发SSL Web服务器,而无需下载额外的宝石和类似的东西。

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

    那么有没有人对可能出错的内容有什么看法

1 个答案:

答案 0 :(得分:14)

好的,我弄清楚出了什么问题,我应该更加关注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。 &LT;面部棕榈&GT;