使用Puma的SSL,HTTP解析错误,格式错误的请求

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

尝试使用HTTPS为在远程服务器上的专用网络中提供的rails应用程序配置Puma。 Puma Docs使它看起来像是可能的,并且它们提供了这个命令:

puma -b 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert'

采取的步骤(用于测试目的):

生成私钥和自签名证书

openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/puma_test.key -out /etc/zzz_puma_test.crt

启动Puma

rvmsudo rails s -p 443 -b 'ssl://127.0.0.1?key=/etc/puma_test.key&cert=/etc/zzz_puma_test.crt'

当我启动服务器时,在日志中我看到了我觉得奇怪的事情:Listening on tcp://0.0.0.0:443就像Puma仍然在http中启动而不是https。启动puma时,这里是终端日志:

=> Booting Puma
=> Rails 4.2.8 application starting in development on http://ssl://127.0.0.1?key=/etc/puma_test.key&cert=/etc/zzz_puma_test.crt:443
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Puma starting in single mode...
* Version 3.12.0 (ruby 2.3.3-p222), codename: Llamas in Pajamas
* Min threads: 0, max threads: 16
* Environment: development
* Listening on tcp://0.0.0.0:443

当我尝试访问该站点时,我在终端中收到此错误:

HTTP解析错误,格式错误的request():#Pop :: HttpParserError:HTTP格式无效,解析失败。

在firefox浏览器中,它提供了这样的反馈:

SSL收到的记录超过了允许的最大长度。错误代码:SSL_ERROR_RX_RECORD_TOO_LONG

奇怪的是,我在我的计算机上本地执行上面的确切步骤,在我的计算机上本地提供的应用程序,一切正常。

  • 我创建了一个新的应用程序
  • 跑上面的行生成密钥和自签名证书
  • 跑上面的行来启动服务器
  • 在启动服务器的终端中,它显示它正在正确监听(换句话说:从不提及http):Listening on ssl://127.0.0.1:443?key...cert...
  • 连接到https://localhost/blogs工作得很好。

也许它与我使用linux机器而不是我的mac这一事实有关?或者,也许是我的测试应用程序存在于我的网络中的远程服务器上?我查看了生成此错误的方法。这是parse_error, line 95 of events.rb

资源已经看过了:

我尝试过调整上面的rails s命令,并进行了许多不同的小改动:

  • 将IP从127.0.0.1改为0.0.0.0
  • 不要指定-p 443选项
  • 指定实际的内部IP地址而不是127.0.0.1(这是在内部网络上提供的)

我还试图删除我的浏览历史记录,并尝试从多个浏览器访问该网站。

任何帮助表示赞赏,谢谢!

ruby-on-rails ruby puma
2个回答
2
投票

我用Ubuntu 16.04.5 LTS服务器(HWE内核)构建了一个新的虚拟机,安装了RVM,Ruby 2.3.3和Rails 4.2.8,遵循你的repro指令,并且...它运行良好。我使用0.0.0.0作为监听地址,并从主机访问了访客的地址。

Guest:
mwp@ubuntu:~/Code/blag$ export rvmsudo_secure_path=1
mwp@ubuntu:~/Code/blag$ rvmsudo rails s -p 443 -b 'ssl://0.0.0.0?key=/etc/puma_test.key&cert=/etc/zzz_puma_test.crt'
=> Booting Puma
=> Rails 4.2.8 application starting in development on http://ssl://0.0.0.0?key=/etc/puma_test.key&cert=/etc/zzz_puma_test.crt:443
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Puma starting in single mode...
* Version 3.12.0 (ruby 2.3.3-p222), codename: Llamas in Pajamas
* Min threads: 0, max threads: 16
* Environment: development
* Listening on ssl://0.0.0.0:443?key=/etc/puma_test.key&cert=/etc/zzz_puma_test.crt
Use Ctrl-C to stop
Host:
mwp@macos:~$ curl -Ik https://192.168.10.137
HTTP/1.1 200 OK
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Content-Type: text/html; charset=utf-8
ETag: W/"b56dd5f9363ed0f7bd4d11c36d9471dd"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 555223a6-3f70-49cf-8b30-92d3047ff8a6
X-Runtime: 0.009792
Guest:
Started HEAD "/" for 192.168.10.112 at 2019-02-28 15:39:19 -0600
Cannot render console from 192.168.10.112! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by Rails::WelcomeController#index as */*
  Rendered /home/mwp/.rvm/gems/ruby-2.3.3/gems/railties-4.2.8/lib/rails/templates/rails/welcome/index.html.erb (0.6ms)
Completed 200 OK in 5ms (Views: 4.4ms | ActiveRecord: 0.0ms)

显然,这里缺少其他东西:服务器上的一些配置,Gemfile中的一些Gem,或者Rails项目中的一些配置。


1
投票

这是最终为我工作的解决方案:

首先,我必须使用ssl_bind指令创建一个puma配置文件:

# /<path_to_app>/puma/development.rb
ssl_bind '127.0.0.1', '9292', {
   cert: ‘/etc/puma_test.key',
   key: ‘/etc/zzz_puma_test.crt'
}

然后我不得不用puma启动服务器而不是rails s。无论出于何种原因,我都无法让rails s工作。在启动puma的命令中,我必须确保指定-C和puma配置文件的路径:

rvmsudo puma -b 'ssl://0.0.0.0:443?key=/etc/puma_test.key&cert=/etc/zzz_puma_test.crt' -C /<path_to_app>/puma/development.rb
© www.soinside.com 2019 - 2024. All rights reserved.