2

Sinatra 应用程序,Ubuntu 12

#config.ru
require 'sinatra/base'
require 'openssl'
require 'webrick'
require 'webrick/https'

class App1 < Sinatra::Base
  get '/' do
    'app1'
  end
end

class App2 < Sinatra::Base
  get '/' do
    'app2'
  end
end

app = Rack::Builder.new do 
  map '/app1' do
    run App1
  end
  map '/app2' do
    run App2
  end
end

webrick_options = {
  :Port               => 8443,
  :Logger             => WEBrick::Log::new($stdout, WEBrick::Log::DEBUG),
  :DocumentRoot       => "./public",
  :SSLEnable          => true,
  :SSLCertificate     => OpenSSL::X509::Certificate.new(  File.open("/etc/ssl/certs/ssl-cert-snakeoil.pem").read),
  :SSLPrivateKey      => OpenSSL::PKey::RSA.new(          File.open("/etc/ssl/private/ssl-cert-snakeoil.key").read),
  :SSLCertName        => [ [ "CN",WEBrick::Utils::getservername ] ]
}

Rack::Handler::WEBrick.run app, webrick_options

然后我运行它

sudo rackup

它运行并且似乎没问题:

[2012-11-06 16:10:14] INFO  WEBrick 1.3.1
[2012-11-06 16:10:14] INFO  ruby 1.9.3 (2012-04-20) [x86_64-linux]
[2012-11-06 16:10:14] DEBUG TCPServer.new(0.0.0.0, 8443)
[2012-11-06 16:10:14] DEBUG TCPServer.new(::, 8443)
.......

但是当我去http://localhost:8443/app1我得到

[2012-11-06 17:07:25] ERROR OpenSSL::SSL::SSLError: SSL_accept returned=1 errno=0 state=unknown state: http request
        /usr/lib/ruby/1.9.1/openssl/ssl-internal.rb:164:in `accept'
[2012-11-06 17:07:25] ERROR OpenSSL::SSL::SSLError: SSL_accept returned=1 errno=0 state=unknown state: http request
        /usr/lib/ruby/1.9.1/openssl/ssl-internal.rb:164:in `accept'
[2012-11-06 17:07:25] ERROR OpenSSL::SSL::SSLError: SSL_accept returned=1 errno=0 state=unknown state: http request
        /usr/lib/ruby/1.9.1/openssl/ssl-internal.rb:164:in `accept'

以及页面中的“此网页不可用”标签。

我究竟做错了什么?

4

1 回答 1

0

WEBrick 将浏览器的 HTTP 请求解释为格式错误的 HTTPS 请求。

要改为发出 HTTPS 请求,请访问https://localhost:8443/app1

您总是可以运行第二个服务器,例如在端口 8080 上,它将 HTTP 请求重定向到 HTTPS URL。

于 2013-09-23T14:40:48.750 回答