我試圖設置一個使用自簽名證書通過https工作的非常簡單的Sinatra和Webrick服務器。我想將所有http流量重定向到https,這可能是在這種設置?WEBrick&Sinatra:如何將所有http流量重定向到https
我有兩個紅寶石如下文件:
sinatra_ssl.rb
#sinatra_ssl.rb
require 'webrick/https'
module Sinatra
class Application
def self.run!
certificate_content = File.open(ssl_certificate).read
key_content = File.open(ssl_key).read
server_options = {
:Host => bind,
:Port => port,
:SSLEnable => true,
:SSLCertificate => OpenSSL::X509::Certificate.new(certificate_content),
:SSLPrivateKey => OpenSSL::PKey::RSA.new(key_content)
}
Rack::Handler::WEBrick.run self, server_options do |server|
[:INT, :TERM].each { |sig| trap(sig) { server.stop } }
server.threaded = settings.threaded if server.respond_to? :threaded=
set :running, true
end
end
end
end
編輯:機架/ SSL myapp.rb的新增需求
#myapp.rb
require 'sinatra'
require 'path/to/sinatra_ssl'
require 'rack/ssl'
use Rack::SSL
set :server, 'WEBrick'
set :ssl_certificate, "path/to/server.crt"
set :ssl_key, "path/to/server.key"
set :port, 8443
get '/' do
'Hello World!'
end
目前,如果我訪問https://localhost:8443
我收到了Hello World!
消息,但如果我訪問http://localhost:8443
,我得到ERR_EMPTY_RESPONSE
,因爲服務器沒有路由。
有沒有辦法做到這一點重定向?
我不知道你是如何部署你的應用程序,但我通常將它們部署在像nginx這樣的反向代理之後,並且你可以輕鬆地將所有http流量重定向到https –
@ Sirl33tname謝謝,我可能最終會這樣做,但我只是在本地進行測試現在想知道是否可以直接使用WEBricks – Aly