如何在localhost上为Ruby on Rails应用程序运行SSL证书?

时间:2018-11-27 02:55:11

标签: ruby-on-rails ruby ssl

运行Ruby 1.9.3,Rails 4.2.10并在开发中使用Thin服务器。

我的应用程序在Heroku上使用SSL。需要在我的本地Mac上运行SSL。

我遵循了本教程。

https://www.botreetechnologies.com/blog/enable-ssl-for-rails-development-environment-two-minutes

这是我的Procfile:

web: bundle exec rails server thin start -p 3001 -e development --ssl --ssl-verify --ssl-key-file server.key --ssl-cert-file server.crt

这是我在跟踪中收到的错误

Invalid request: Invalid HTTP format, parsing fails.
    /Users/jn/.rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/thin-1.6.4/lib/thin/request.rb:84:in `execute'
    /Users/jn/.rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/thin-1.6.4/lib/thin/request.rb:84:in `parse'
    /Users/jn/.rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/thin-1.6.4/lib/thin/connection.rb:39:in `receive_data'
    /Users/jn/.rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/eventmachine-1.0.8/lib/eventmachine.rb:193:in `run_machine'
    /Users/jn/.rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/eventmachine-1.0.8/lib/eventmachine.rb:193:in `run'
    /Users/jn/.rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/thin-1.6.4/lib/thin/backends/base.rb:73:in `start'
    /Users/jn/.rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/thin-1.6.4/lib/thin/server.rb:162:in `start'
    /Users/jn/.rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/rack-1.6.4/lib/rack/handler/thin.rb:19:in `run'
    /Users/jn/.rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/rack-1.6.4/lib/rack/server.rb:286:in `start'
    /Users/jn/.rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/railties-4.2.4/lib/rails/commands/server.rb:80:in `start'
    /Users/jn/.rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:80:in `block in server'
    /Users/jn/.rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:75:in `tap'
    /Users/jn/.rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:75:in `server'
    /Users/jn/.rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    /Users/jn/.rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/railties-4.2.4/lib/rails/commands.rb:17:in `<top (required)>'
    script/rails:6:in `require'
    script/rails:6:in `<main>'

更新:

尽管我的procfile包含有关cert的信息。我通过这样的终端手动启动THIN rails服务器。

bundle exec rails server thin start -p 3001 -e development

所以不确定是否正在使用Procfile吗?

更新2 ========

试图使用bundle exec thin start -p 3001 --ssl 将force_ssl设置为false

服务器启动

jn$ bundle exec thin start -p 3001 --ssl
/Users/jn/.rbenv/versions/1.9.3-p551/lib/ruby/gems/1.9.1/gems/thin-1.6.4/lib/thin/backends/base.rb:103: warning: epoll is not supported on this platform
Using rack adapter
Thin web server (v1.6.4 codename Gob Bluth)
Maximum connections set to 1024
Listening on 0.0.0.0:3001, CTRL+C to stop

尝试打https://localhost:3001,服务器错误停止

libc++abi.dylib: terminating with uncaught exception of type std::runtime_error: Encryption not available on this event-machine
Abort trap: 6

=====更新3:======

尝试过的Puma.io失败了相同的问题(无法建立安全连接)。 尝试过的Puma-dev甚至无法识别.dev URl 尝试再次手动添加证书和密钥(无法建立安全连接)。

App在http://localhost:3000处加载我们的应用程序登录表单。一旦我登录该应用程序,便使用http加载了正确的用户帐户页面,然后大约1秒钟后的页面被重新分配到相同的用户帐户页面,但是https提示“无法建立安全连接”。不知道为什么浏览器强迫我使用https?应用中没有任何我可以“查找/替换”的https设置。

生成了一个新的secret_key,因为它认为短20个字符而不是30个字符。粘贴到secret_key.rb重新启动的服务器中。同样的SSL问题。

4 个答案:

答案 0 :(得分:1)

您可以使用puma-dev进行本地开发。它使HTTPS可以轻松使用。

  

Puma-dev也自动通过SSL启用应用程序。首次运行puma-dev时,可能会导致出现一个对话框,提示您输入密码。发生了什么事,puma-dev生成了自己的CA证书,该证书存储在〜/ Library / Application Support / io.puma.dev / cert.pem中。

答案 1 :(得分:0)

更新:根据您的最新评论。您需要使用

来启动本地服务器
foreman start -f Procfile

或使用

bundle exec thin start -p 3001 --ssl –-ssl-key-file ~/.ssl/localhost.key –-ssl-cert-file ~/.ssl/localhost.crt

但是此配置文件在生产中将无法使用。您可能要在开发中使用备用Procfile-dev文件

原始:

在Procfile中,尝试对使用SSL的端口3001进行硬编码

web: bundle exec thin start -p 3001 --ssl –-ssl-key-file ~/.ssl/localhost.key –-ssl-cert-file ~/.ssl/localhost.crt

更新2:本地开发人员完全不需要证书,您应该可以允许通过Chrome浏览器访问

bundle exec thin start -p 3001 --ssl  #this should work without any other arguments

然后您需要将端口传递给浏览器

https://localhost:3001

您也可以尝试在application.rb中对此进行注释

#config.force_ssl = true

那只能在production.rb中设置

关于为什么不能安装gem install foreman的原因,我不确定为什么不能安装,这听起来像是计算机上的某种网络问题,您是否有防火墙阻止?

答案 2 :(得分:0)

将应用程序更新为2.3.1,使puma-dev可以正常工作。 删除了引发错误的config.action_cable.disable_request_forgery_protection = true

答案 3 :(得分:0)

打开终端并按照以下步骤操作

  1. openssl req -x509 -sha256 -nodes -newkey rsa:2048 -days 365 -keyout localhost.key -out localhost.crt

  2. sudo nano /usr/local/share/ca-certificates/localhost.crt并从
    复制文本 原始文件localhost.crt保存

  3. sudo update-ca-certificates

  4. 添加宝石“稀薄”

  5. 用于运行服务器“瘦启动--ssl --ssl-key-file localhost.key --ssl-cert-file localhost.crt”