简体   繁体   English

HTTP 解析错误,格式错误的请求 - Ruby on Rails

[英]HTTP parse error, malformed request - Ruby on Rails

I see the following error in Terminal when attempting to run a Ruby on Rails app.尝试运行 Ruby on Rails 应用程序时,我在终端中看到以下错误。

HTTP parse error, malformed request (): #<Puma::HttpParserError: Invalid HTTP format, parsing fails.>
2017-03-12 13:10:02 -0400: ENV: {"rack.version"=>[1, 3], "rack.errors"=>#<IO:<STDERR>>, "rack.multithread"=>true, "rack.multiprocess"=>false, "rack.run_once"=>false, "SCRIPT_NAME"=>"", "QUERY_STRING"=>"", "SERVER_PROTOCOL"=>"HTTP/1.1", "SERVER_SOFTWARE"=>"puma 3.4.0 Owl Bowl Brawl", "GATEWAY_INTERFACE"=>"CGI/1.2"}

The browser error:浏览器错误:

This site can’t provide a secure connection. localhost sent an invalid response. ERR_SSL_PROTOCOL_ERROR*

I have tried the following我已经尝试了以下

  • Clearing browser cache and restarting清除浏览器缓存并重新启动
  • Reverting back to an old commit in GIT that was working at the time恢复到当时正在工作的 GIT 中的旧提交
  • Restarting terminal重启终端
  • Running a different rails app that was functional运行另一个功能正常的 Rails 应用程序

Here are some possible solutions.以下是一些可能的解决方案。

  1. Make sure you are connecting through http://localhost:3000 and not https://localhost:3000 .确保您通过http://localhost:3000而不是https://localhost:3000 进行连接

  2. If the browser redirects to HTTPS and it's Google Chrome, try this solution that addresses an HSTS problem: https://stackoverflow.com/a/28586593如果浏览器重定向到 HTTPS 并且是 Google Chrome,请尝试解决 HSTS 问题的解决方案: https : //stackoverflow.com/a/28586593

  3. Make sure you do not have the production environment (if that's what you're serving) forcing HTTPS.确保您没有强制使用 HTTPS 的生产环境(如果这就是您所服务的环境)。 If that's the problem, comment this out or change true to false :如果这是问题所在,请将其注释掉或将true更改为false

    config/environments/production.rb配置/环境/production.rb

     # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. config.force_ssl = true

seems like you are trying to run HTTPS on your local.似乎您正在尝试在本地运行 HTTPS。 You need to have a TLS toolkit (like openSSL) installed on your local.您需要在本地安装 TLS 工具包(如 openSSL)。 OPENSSL for example .以 OPENSSL 为例

after you made sure of that, and if still not working, maybe you can find you're answer in the next Github issue.在您确定之后,如果仍然无法正常工作,也许您可​​以在下一个 Github 问题中找到答案。 Seems like a bug with Puma gem.似乎是 Puma gem 的错误。 GITHUB ISSUE TALK GitHub 问题讨论

For those reading this in the future, consider the following:对于那些将来阅读本文的人,请考虑以下事项:

  1. Did you change your server in your Gemfile.您是否在 Gemfile.xml 中更改了服务器? eg from Puma to Thin?例如从彪马到瘦?
  2. Have you set up an SSL certificate?您是否设置了 SSL 证书?
  3. Are you starting your webserver with SSL certificate flags?您是否使用 SSL 证书标志启动您的网络服务器?
  4. Is SSL turned on in your development/production environment - and what environment are you invoking?您的开发/生产环境中是否启用了 SSL - 您正在调用什么环境?

If you are ok with turning off SSL in your development environment you can do so by going to:如果您可以在开发环境中关闭 SSL,则可以通过以下方式执行此操作:

config/environments/development.rb and configuring: config/environments/development.rb和配置:

config.force_ssl = false

Here is some code that works for me, using puma, that invokes SSL certification (locally).这是一些对我有用的代码,使用 puma,调用 SSL 认证(本地)。 I have created my certificates and have dumped it in the relevant location:我已经创建了我的证书并将其转储到相关位置:

rails s -b 'ssl://localhost:3000?key=./.ssl/localhost.key&cert=./.ssl/localhost.crt'

When I want to run it in a production environment from my PC I using the following:当我想从我的 PC 在生产环境中运行它时,我使用以下命令:

rails s -b 'ssl://localhost:3000?key=./.ssl/localhost.key&cert=./.ssl/localhost.crt' -e production

HTH HTH

Access the app using a different browser, or if you are in Chrome access it in Incognito mode.使用不同的浏览器访问该应用程序,或者如果您在 Chrome 中以隐身模式访问它。 After this the error did not show in any browser again.在此之后,错误不再出现在任何浏览器中。 Remember to remove the config.force_ssl or set it to false in the development.rb file first.请记住首先在development.rb文件中删除config.force_ssl或将其设置为false

Encountered this today after adding and then removing the config.force_ssl = true config in our Rails 6 app's development.rb file.在我们的 Rails 6 应用程序的development.rb文件中添加然后删除config.force_ssl = true config 后,今天遇到了这个问题。 Tried to access the app in localhost, in a Chrome browser, and the same error showed.尝试在 localhost 中访问该应用程序,在 Chrome 浏览器中,并显示相同的错误。 Restared rails server several times, to no avail.多次重启 rails 服务器,无济于事。

The accessing it in different browser, where the force ssl version of the app client was never opened, worked.在不同的浏览器中访问它,其中应用程序客户端的 force ssl 版本从未打开,工作。

In my case it was silly mistake I started server on http and my url was pointing to https.在我的情况下,我在 http 上启动服务器并且我的 url 指向 https,这是一个愚蠢的错误。 I hope it would save someone's time ;)我希望它可以节省某人的时间;)

Faced this error as well, If the above solutions don't work, do a quick check to see if you have this meta tag in your application.html.erb也遇到此错误,如果上述解决方案不起作用,请快速检查您的 application.html.erb 中是否有此元标记

<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">

This will force Google Chrome to redirect localhost to HTTPS这将强制 Google Chrome 将本地主机重定向到 HTTPS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM