ruby-on-rails - 工头、Rails、Puma SSL/HTTPS

标签 ruby-on-rails ssl https puma foreman

如何组合这两个命令,以便 Rails 在 puma 命令中使用 -b 选项?

puma: puma -b 'ssl://127.0.0.1:3000?key=/Users/cdrum/.ssh/server.key&cert=/Users/cdrum/.ssh/server.crt' -b 'tcp://127.0.0.1:3001'
rails: bundle exec rails s --binding=127.0.0.1 -p 3000 -e $RAILS_ENV puma

目前我得到这个错误:

puma/binder.rb:272:in `initialize': Address already in use - bind(2) for"127.0.0.1" port 3000 (Errno::EADDRINUSE)

或者,我可以告诉 Rails 查找已经在运行的 puma 实例,而不是尝试再次启动它吗?

最佳答案

您实际上可以通过单独运行puma 并将所有配置存储在config/puma.rb 中来解决这个问题。不同的证书。管理但不管:

threads_count = ENV.fetch('RAILS_MAX_THREADS') { 5 }
threads threads_count, threads_count
port ENV.fetch('PORT') { 3000 }
environment ENV.fetch('RAILS_ENV') { 'development' }
plugin :tmp_restart

localhost_key = "#{Dir.pwd}/#{File.join('config', 'certs', 'localhost.key')}"
localhost_cert = "#{Dir.pwd}/#{File.join('config', 'certs', 'localhost.crt')}"

unless File.exist?(localhost_key)
  def generate_root_cert(root_key) # rubocop:disable Metrics/AbcSize
    root_ca = OpenSSL::X509::Certificate.new
    root_ca.version = 2
    root_ca.serial = 0x0
    root_ca.subject = OpenSSL::X509::Name.parse '/C=BE/O=A1/OU=A/CN=localhost'
    root_ca.issuer = root_ca.subject
    root_ca.public_key = root_key.public_key
    root_ca.not_before = Time.now
    root_ca.not_after = root_ca.not_before + 2 * 365 * 24 * 60 * 60
    root_ca.sign(root_key, OpenSSL::Digest::SHA256.new)
    root_ca
  end

  root_key = OpenSSL::PKey::RSA.new(2048)
  file = File.new(localhost_key, 'wb')
  file.write(root_key)
  file.close

  root_cert = generate_root_cert(root_key)
  file = File.new(localhost_cert, 'wb')
  file.write(root_cert)
  file.close
end

ssl_bind '0.0.0.0', '8443', key: localhost_key, cert: localhost_cert

现在 HTTP 和 HTTPS 都适用于我的 Rails 应用。

Procfile 现在只是:

web: puma

确实需要一个special puma gem 的分支以避免 SSL 错误。

关于ruby-on-rails - 工头、Rails、Puma SSL/HTTPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54994088/

相关文章:

css - 在 IE 上,CSS 字体仅在通过内部链接导航时有效

java - 使用 .pem 文件在 java 中发送 https 请求

javascript - 如何生成动态列

ruby-on-rails - 使用 heroku rails 时出错,找不到数据库客户端 psql。检查您的 $PATH 并重试

apache - 如何使用反向代理确保与外部服务器的安全连接?

ssl - 使用 Fiddler 捕获 Azure Active Directory 登录期间的交互

ruby-on-rails - 验证码图像在我的生产环境中不起作用,但开发正常

ruby-on-rails - 当我部署我的 Rails 项目时,Capistrano 3 没有创建 'current' 目录

apache - 404 页面上的 SSL https 错误,但其他页面在启用重定向的情况下工作正常

python - Fiddler 请求到 Python Requests 请求