在安全(https)上运行rails并重定向非安全(http)请求以通过Webricks确保安全

问题描述 投票:1回答:2

我正在使用以下环境:

Server: Webrick
Rails: 3.2.6
Ruby: 1.9.3p555

我在/ script / rails中添加了以下代码:

require 'rails/commands/server'
require 'rack'
require 'webrick'
require 'webrick/https'

if ENV['SSL'] == "true"
  module Rails
      class Server < ::Rack::Server
          def default_options
              super.merge({
                  :Port => 3000,
                  :environment => (ENV['RAILS_ENV'] || "production").dup,
                  :daemonize => false,
                  :debugger => false,
                  :pid => File.expand_path("tmp/pids/server.pid"),
                  :config => File.expand_path("config.ru"),
                  :SSLEnable => true,
                  :SSLVerifyClient => OpenSSL::SSL::VERIFY_NONE,
                  :SSLPrivateKey => OpenSSL::PKey::RSA.new(
                                   File.open("certs/project.key").read),
                  :SSLCertificate => OpenSSL::X509::Certificate.new(
                                   File.open("certs/project.crt").read),
                  :SSLCertName => [["CN", WEBrick::Utils::getservername]],
              })
          end
      end
  end
end

APP_PATH = File.expand_path('../../config/application',  __FILE__)
require File.expand_path('../../config/boot',  __FILE__)
require 'rails/commands'

我在/config/environments/production.rb中添加了以下行:

config.force_ssl = true

现在,我尝试了以下尝试:

  1. 在3000上启动滑轨

    SSL=true rails s -e production -p 3000

它在https://project.com上运行rails,但在http://project.com上出现404错误

  1. 在443上启动rails并在脚本中提到相同的端口:

    rvmsudo rails s -p 443

  2. 使用两个不同的pid分别在80和443上启动滑轨:

    rvmsudo rails s -p 80 -P PID1 rvmsudo rails s -p 443 -P PID2

4,最后我尝试将请求从443和80转发到3000:

sudo iptables -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 3000
sudo iptables -t nat -I PREROUTING -p tcp --dport 443 -j REDIRECT --to-ports 3000

我也尝试过用稀薄的宝石做同样的事情,但是结果是一样的。

ruby-on-rails ruby ruby-on-rails-3 ssl webrick
2个回答
8
投票

冒着被炒鱿鱼的风险...

锤子 ,只把所有东西都看成是钉子,这不是一个有效的计划。 有时,面对螺丝时,您必须找到螺丝起子

Webrick是Rails Web服务器最差的可用选项,仅在绝对必要时才应使用。 它太简单了,无法在您的Gemfile中放入稀薄的东西或彪马,并在一个更加幸福,线程安全的世界中前进。

在这方面,这是您理智的九步计划。

步骤1:停止。

步骤2:想一想。

步骤3:存在最佳实践是有原因的,例如,完美的前向保密性,对代理Rails应用程序的负载平衡,静态资产服务。

步骤4:决定停止重新发明轮子,并花时间学习您的手艺。

第5步:通过发行版的存储库安装Nginx

步骤6: 更改默认的Nginx主机,以通过SSL为您的域提供服务。

步骤7:将puma添加到您的Gemfile中,然后进行捆绑更新

步骤8: 设置puma init.d或upstart文件,使其在重启后仍然存在

步骤9:获利


0
投票

我在/ script / rails中添加了以下代码:

require 'rails/commands/server'
require 'rack'
require 'webrick'
require 'webrick/https'

if ENV['SSL'] == "true"
  module Rails
      class Server < ::Rack::Server
          def default_options
              super.merge({
                  :Port => 443,
                  :environment => (ENV['RAILS_ENV'] || "production").dup,
                  :daemonize => false,
                  :debugger => false,
                  :pid => File.expand_path("tmp/pids/server.pid"),
                  :config => File.expand_path("config.ru"),
                  :SSLEnable => true,
                  :SSLVerifyClient => OpenSSL::SSL::VERIFY_NONE,
                  :SSLPrivateKey => OpenSSL::PKey::RSA.new(
                                   File.open("certs/project.key").read),
                  :SSLCertificate => OpenSSL::X509::Certificate.new(
                                   File.open("certs/project.crt").read),
                  :SSLCertName => [["CN", WEBrick::Utils::getservername]],
              })
          end
      end
  end
end

APP_PATH = File.expand_path('../../config/application',  __FILE__)
require File.expand_path('../../config/boot',  __FILE__)
require 'rails/commands'

我在/config/environments/production.rb中添加了以下行:

config.force_ssl = true

使用两个不同的pid分别在80和443上启动滑轨:

SSL=true rails s -p 443 -e production   
rails s -p 80 -P SERVER2 -e production
© www.soinside.com 2019 - 2024. All rights reserved.