如何让 Sinatra 通过 HTTPS/SSL 工作?

发布于 2024-09-19 01:20:32 字数 159 浏览 6 评论 0原文

正如标题所示,谷歌没有提供任何与此相关的有用信息。

如何为 Sinatra 应用程序设置和配置 HTTPS/SSL?

如何创建 HTTPS 路由?

我以前从未在我的应用程序中使用过 HTTPS,也没有调整 Rack/其他内容的经验,所以我很欣赏详细的答案。

As the title says, Google doesn't give anything useful concerning this.

How do I set up and configure HTTPS/SSL for Sinatra apps?

How do I create a HTTPS route?

I have never used HTTPS for my apps before and have no experience tweaking Rack/whatever, so I appreciate detailed answers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

べ映画 2024-09-26 01:20:32

这似乎对我有用:

require 'sinatra/base'
require 'webrick'
require 'webrick/https'
require 'openssl'

CERT_PATH = '/opt/myCA/server/'

webrick_options = {
        :Port               => 8443,
        :Logger             => WEBrick::Log::new($stderr, WEBrick::Log::DEBUG),
        :DocumentRoot       => "/ruby/htdocs",
        :SSLEnable          => true,
        :SSLVerifyClient    => OpenSSL::SSL::VERIFY_NONE,
        :SSLCertificate     => OpenSSL::X509::Certificate.new(  File.open(File.join(CERT_PATH, "my-server.crt")).read),
        :SSLPrivateKey      => OpenSSL::PKey::RSA.new(          File.open(File.join(CERT_PATH, "my-server.key")).read),
        :SSLCertName        => [ [ "CN",WEBrick::Utils::getservername ] ]
}

class MyServer  < Sinatra::Base
    post '/' do
      "Hellow, world!"
    end            
end

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

[提示 http://www .networkworld.com/columnists/2007/090507-dr-internet.html]

this seems to do it for me:

require 'sinatra/base'
require 'webrick'
require 'webrick/https'
require 'openssl'

CERT_PATH = '/opt/myCA/server/'

webrick_options = {
        :Port               => 8443,
        :Logger             => WEBrick::Log::new($stderr, WEBrick::Log::DEBUG),
        :DocumentRoot       => "/ruby/htdocs",
        :SSLEnable          => true,
        :SSLVerifyClient    => OpenSSL::SSL::VERIFY_NONE,
        :SSLCertificate     => OpenSSL::X509::Certificate.new(  File.open(File.join(CERT_PATH, "my-server.crt")).read),
        :SSLPrivateKey      => OpenSSL::PKey::RSA.new(          File.open(File.join(CERT_PATH, "my-server.key")).read),
        :SSLCertName        => [ [ "CN",WEBrick::Utils::getservername ] ]
}

class MyServer  < Sinatra::Base
    post '/' do
      "Hellow, world!"
    end            
end

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

[hat tip to http://www.networkworld.com/columnists/2007/090507-dr-internet.html]

烈酒灼喉 2024-09-26 01:20:32

我认为使用 rack-ssl 是最好的选择。

然后你只需执行以下操作:

class Application < Sinatra::Base
  use Rack::SSL

  get '/' do
    'SSL FTW!'
  end
end

所有 http:// 调用都会重定向到 https://

I think using rack-ssl is the best option.

Then you just do:

class Application < Sinatra::Base
  use Rack::SSL

  get '/' do
    'SSL FTW!'
  end
end

and all http:// calls are redirected to https://

绝情姑娘 2024-09-26 01:20:32

我想您需要设置您的 Web 服务器(而不是 Sinatra)才能使用 SSL。在 Sinatra 中,您可以使用 request.secure? 方法来检查 SSL 使用情况。

SSL + Nginx:第一篇文章第二个

I guess you need to setup your Web-server, not Sinatra, to work with SSL. In Sinatra you can use the request.secure? method to check for the SSL usage.

SSL + Nginx: the first article, the second one.

与君绝 2024-09-26 01:20:32

我修改了 richard_bw 的代码,以便能够使用 Ctrl+C 关闭或重新启动它:

require 'sinatra/base'
require 'webrick'
require 'webrick/https'
require 'openssl'

class MyServer  < Sinatra::Base
    post '/' do
      "Hello, world!\n"
    end            
end

CERT_PATH = '/opt/myCA/server/'

webrick_options = {
  :Port               => 8443,
  :Logger             => WEBrick::Log::new($stderr, WEBrick::Log::DEBUG),
  :DocumentRoot       => "/ruby/htdocs",
  :SSLEnable          => true,
  :SSLVerifyClient    => OpenSSL::SSL::VERIFY_NONE,
  :SSLCertificate     => OpenSSL::X509::Certificate.new(  File.open(File.join(CERT_PATH, "server.crt")).read),
  :SSLPrivateKey      => OpenSSL::PKey::RSA.new(          File.open(File.join(CERT_PATH, "server.key")).read),
  :SSLCertName        => [ [ "CN",WEBrick::Utils::getservername ] ],
  :app                => MyServer
}
Rack::Server.start webrick_options

I modified code of richard_bw as to be able close or restart it with Ctrl+C:

require 'sinatra/base'
require 'webrick'
require 'webrick/https'
require 'openssl'

class MyServer  < Sinatra::Base
    post '/' do
      "Hello, world!\n"
    end            
end

CERT_PATH = '/opt/myCA/server/'

webrick_options = {
  :Port               => 8443,
  :Logger             => WEBrick::Log::new($stderr, WEBrick::Log::DEBUG),
  :DocumentRoot       => "/ruby/htdocs",
  :SSLEnable          => true,
  :SSLVerifyClient    => OpenSSL::SSL::VERIFY_NONE,
  :SSLCertificate     => OpenSSL::X509::Certificate.new(  File.open(File.join(CERT_PATH, "server.crt")).read),
  :SSLPrivateKey      => OpenSSL::PKey::RSA.new(          File.open(File.join(CERT_PATH, "server.key")).read),
  :SSLCertName        => [ [ "CN",WEBrick::Utils::getservername ] ],
  :app                => MyServer
}
Rack::Server.start webrick_options
誰認得朕 2024-09-26 01:20:32

为了避免多个服务器,这里的 webrick 特定答案很好,但 webrick 特定。

使用Puma时,可以简化配置:

require 'sinatra/base'

class MyServer  < Sinatra::Base
    post '/' do
      "Hello, world!\n"
    end
end

Rack::Server.start app: MyServer, Host: "ssl://0.0.0.0:8443?key=privkey.pem&cert=cert.pem"

For avoiding multiple servers, the webrick specific answers here are fine, but webrick specific.

When using Puma, the configuration can be simplified:

require 'sinatra/base'

class MyServer  < Sinatra::Base
    post '/' do
      "Hello, world!\n"
    end
end

Rack::Server.start app: MyServer, Host: "ssl://0.0.0.0:8443?key=privkey.pem&cert=cert.pem"
一杆小烟枪 2024-09-26 01:20:32

经过广泛搜索后,我能找到的最简单的解决方案是 Frank 此处

只需将以下内容放在 Sinatra 经典应用程序的顶部即可强制您的应用程序使用 HTTPS:

require 'rack/ssl-enforcer'
use Rack::SslEnforcer

The easiest solution I could find after a broad search, is the solution posted by Frank here.

Simply place the following at the top of your Sinatra classic app to force your application to use HTTPS:

require 'rack/ssl-enforcer'
use Rack::SslEnforcer
复古式 2024-09-26 01:20:32

处理程序已从机架库移至机架库。以下是对我有用的

#!/usr/bin/env ruby

require 'sinatra'
require 'slim'
require 'webrick'
require 'openssl'

script_root = File.expand_path(File.dirname(__FILE__))

webrick_options = { 
  :Port               => 8443,
  :Logger             => WEBrick::Log::new($stderr, WEBrick::Log::DEBUG),
  :DocumentRoot       => "/ruby/htdocs",
  :SSLEnable          => true,
  :SSLVerifyClient    => OpenSSL::SSL::VERIFY_NONE,
  :SSLCertificate     => OpenSSL::X509::Certificate.new(File.open("#{script_root}/ssl/public.crt").read),
  :SSLPrivateKey      => OpenSSL::PKey::RSA.new(File.open("#{script_root}/ssl/private.key").read),
  :SSLCertName        => [ [ "CN",WEBrick::Utils::getservername ] ] 
}

class WebServer  < Sinatra::Base
  get('/') { slim :index }
end

Rackup::Handler::WEBrick.run(WebServer, **webrick_options)

The handler has been moved from the rack library to rackup. Below is what worked for me

#!/usr/bin/env ruby

require 'sinatra'
require 'slim'
require 'webrick'
require 'openssl'

script_root = File.expand_path(File.dirname(__FILE__))

webrick_options = { 
  :Port               => 8443,
  :Logger             => WEBrick::Log::new($stderr, WEBrick::Log::DEBUG),
  :DocumentRoot       => "/ruby/htdocs",
  :SSLEnable          => true,
  :SSLVerifyClient    => OpenSSL::SSL::VERIFY_NONE,
  :SSLCertificate     => OpenSSL::X509::Certificate.new(File.open("#{script_root}/ssl/public.crt").read),
  :SSLPrivateKey      => OpenSSL::PKey::RSA.new(File.open("#{script_root}/ssl/private.key").read),
  :SSLCertName        => [ [ "CN",WEBrick::Utils::getservername ] ] 
}

class WebServer  < Sinatra::Base
  get('/') { slim :index }
end

Rackup::Handler::WEBrick.run(WebServer, **webrick_options)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文