简体   繁体   English

Rails ActionController :: BadRequest在生产服务器上导致500服务器错误

[英]Rails ActionController::BadRequest causes 500 Server Error on production server

We have a Turkish website and some old links are crawled by some search engines. 我们有一个土耳其网站,一些旧链接被一些搜索引擎抓取。 The links seem to be malformed or cannot be processed and therefore causing ActionController::BadRequest error. 链接似乎格式不正确或无法处理,因此导致ActionController::BadRequest错误。 On the local machine with development env this causes to return the Rails error page with ActionController::BadRequest . 在具有development env的本地机器上,这导致返回带有ActionController::BadRequest的Rails错误页面。

But on the Server we get an 500 server error . 但在服务器上我们得到500 server error This issues is discussed on several other pages like here . 这个问题在这里的其他几个页面上讨论过。 But none of the solutions helped. 但是这些解决方案都没有帮助。

In both cases we would like to redirect to a page not found -page. 在这两种情况下,我们都希望重定向到page not foundpage not found -page。

I already tried to rescue_from ActionController::BadRequest and rescue_from ActionController::RoutingError in ApplicationController because of the aforementioned article, where they state that BadRequest turns into RoutingError . 由于前面提到的文章,我已经尝试在ApplicationController尝试rescue_from ActionController::BadRequestrescue_from ActionController::RoutingError ,其中他们声明BadRequest变成RoutingError

But neither of them worked. 但他们都没有奏效。

I hope someone had the same problem and already resolved it. 我希望有人遇到同样的问题并且已经解决了。 Thanks in advance for your answers. 提前感谢您的回答。

Edit: 编辑:

One example problem-url is http://localhost:3000/Di%c5%ef%bf%bd-f%c4%b1r%c3%a7as%c4%b1 . 一个示例问题-url是http://localhost:3000/Di%c5%ef%bf%bd-f%c4%b1r%c3%a7as%c4%b1

Terminal output: 终端输出:

ActionController::BadRequest (ActionController::BadRequest):
  actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:37:in `block in call'
  actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:33:in `each'
  actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:33:in `call'
  actionpack (4.0.0) lib/action_dispatch/journey/router.rb:71:in `block in call'
  actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `each'
  actionpack (4.0.0) lib/action_dispatch/journey/router.rb:59:in `call'
  actionpack (4.0.0) lib/action_dispatch/routing/route_set.rb:655:in `call'
  newrelic_rpm (3.6.4.122) lib/new_relic/rack/error_collector.rb:12:in `call'
  newrelic_rpm (3.6.4.122) lib/new_relic/rack/agent_hooks.rb:22:in `call'
  newrelic_rpm (3.6.4.122) lib/new_relic/rack/browser_monitoring.rb:16:in `call'
  newrelic_rpm (3.6.4.122) lib/new_relic/rack/developer_mode.rb:28:in `call'
  rack (1.5.2) lib/rack/etag.rb:23:in `call'
  rack (1.5.2) lib/rack/conditionalget.rb:25:in `call'
  rack (1.5.2) lib/rack/head.rb:11:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/flash.rb:241:in `call'
  rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
  rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/cookies.rb:486:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.0.0) lib/active_support/callbacks.rb:373:in `_run__4278100521352222029__call__callbacks'
  activesupport (4.0.0) lib/active_support/callbacks.rb:80:in `run_callbacks'
  actionpack (4.0.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/reloader.rb:64:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  rollbar (0.11.7) lib/rollbar/middleware/rails/show_exceptions.rb:19:in `call_with_rollbar'
  actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.0.0) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.0.0) lib/rails/rack/logger.rb:21:in `block in call'
  activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `block in tagged'
  activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in `tagged'
  activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `tagged'
  railties (4.0.0) lib/rails/rack/logger.rb:21:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  activesupport (4.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in `call'
  railties (4.0.0) lib/rails/engine.rb:511:in `call'
  railties (4.0.0) lib/rails/application.rb:97:in `call'
  rack (1.5.2) lib/rack/content_length.rb:14:in `call'
  puma (2.6.0) lib/puma/server.rb:486:in `handle_request'
  puma (2.6.0) lib/puma/server.rb:357:in `process_client'
  puma (2.6.0) lib/puma/server.rb:250:in `block in run'
  puma (2.6.0) lib/puma/thread_pool.rb:92:in `call'
  puma (2.6.0) lib/puma/thread_pool.rb:92:in `block in spawn_thread'

Best regards 最好的祝福

I had the exact same issue in a Rails 4.0.x app where it was polluting my New Relic error page. 我在Rails 4.0.x应用程序中遇到了完全相同的问题,它正在污染我的New Relic错误页面。

I got around this by writing a middleware that caches the ActionController::BadRequest error, Logs it and returns a 400 error page. 我通过编写一个缓存ActionController::BadRequest错误的中间件来解决这个问题,记录它并返回400错误页面。 (A 400 seemed more appropriate then a 404 error.) (400似乎比404错误更合适。)

app/middleware/catch_request_errors.rb 应用/中间件/ catch_request_errors.rb

class CatchRequestErrors
  def initialize(app)
    @app = app
  end

  def call(env)
    begin
      @app.call(env)
    rescue ActionController::BadRequest => error
      ::Rails.logger.warn("WARN: 400 ActionController::BadRequest: #{env['REQUEST_URI']}")
      @html_400_page ||= File.read(::Rails.root.join('public', '400.html'))
      [
          400, { "Content-Type" => "text/html" },
          [ @html_400_page ]
      ]
    end
  end
end

config/application.rb 配置/ application.rb中

config.middleware.insert_before ActionDispatch::ParamsParser, "CatchRequestErrors"

public/400.html 公共/ 400.html

<!DOCTYPE html>
<html>
<head>
  <title>Your request could not be handled (400)</title>
  <style type="text/css">
    body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
    div.dialog {
      width: 25em;
      padding: 0 4em;
      margin: 4em auto 0 auto;
      border: 1px solid #ccc;
      border-right-color: #999;
      border-bottom-color: #999;
    }
    h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
  </style>
</head>

<body>
  <!-- This file lives in public/400.html -->
  <div class="dialog">
    <h1>Your request could not be handled.</h1>
    <p>Please check the url and post data for syntax errors.</p>
  </div>
</body>
</html>

This stops processing the rails stack, logs the error and returns the 400.html page to the user freeing the app to process a more valid request. 这将停止处理rails堆栈,记录错误并将400.html页面返回给用户,从而释放应用程序以处理更有效的请求。

I'm also caching the 400 page as a instance variable to save on GC and Disc IO. 我也将400页缓存为实例变量以保存在GC和Disc IO上。

There is a hack. 有一个黑客。 Put this code inside initializers 将此代码放在initializers

module Rack
  module Utils
    alias_method :original_normalize_params, :normalize_params
    module_function :original_normalize_params

    def normalize_params(params, name, v = nil)
      begin
        original_normalize_params(params, name, v)
      rescue => e
        raise ActionController::BadRequest.new("Incorrect URL")
      end
    end

    module_function :normalize_params
  end
end

It will respond with 400 for requests like http://127.0.0.1:3000/?foo[]=array&foo[hash]=hash 对于http://127.0.0.1:3000/?foo[]=array&foo[hash]=hash请求,它会响应400

EDIT: 编辑:

Also, it is possible to implement middleware which checks for correctness of parameters. 而且,可以实现检查参数正确性的中间件。

Catching bad queries by middleware 通过中间件捕获错误的查询

# config/application.rb

require File.expand_path('../../lib/query_validator', __FILE__)

module MyApp
  class Application < Rails::Application
    # configurations

    config.middleware.insert_before('ActionDispatch::ShowExceptions', QueryValidator)
  end
end

# lib/query_validator.rb

class QueryValidator
  def initialize(app)
    @app = app
  end

  def call(env)
    begin
      Rack::Utils.parse_nested_query(env['QUERY_STRING'])

      env['QUERY_STRING'].valid_encoding? or
        raise ActionController::BadRequest, "Invalid parameter: #{env['QUERY_STRING']}"
    rescue => e
      env['QUERY_STRING'] = ''
      env['my_app.query_errors'] = 'Invalid query.'
    end

    @app.call(env)
  end
end


# application_controller.rb
class ApplicationController < ActionController::Base
  before_filter do
    if env['my_app.query_errors']
      flash[:alert] = env['my_app.query_errors']
      redirect_to root_path
    end
  end

end

I suppose that one of your routes or Rack middleware is invalid and causes the 500 error. 我想你的一个路由或Rack中间件无效并导致500错误。 In new Rails app there is no incorrect behaviour with url http://localhost:3000/Di%c5%ef%bf%bd-f%c4%b1r%c3%a7as%c4%b1 - app returns a 404 error as expected. 在新的Rails应用程序中,url没有不正确的行为http://localhost:3000/Di%c5%ef%bf%bd-f%c4%b1r%c3%a7as%c4%b1 - app按预期返回404错误。 Also request to http://127.0.0.1:3000/?foo[]=array&foo[hash]=hash return absolutely correct response with 400 error (BadRequest). 还请求http://127.0.0.1:3000/?foo[]=array&foo[hash]=hash返回绝对正确的响应400错误(BadRequest)。

Try to rewrite routes like this: 尝试重写这样的路线:

Rails.application.routes.draw do
  get '/*path', :to => lambda { |env| [200, {}, [env.to_s]]}
  # omited
end

and take a look to response status - if it 200, than problem in your Rails application, otherwise anywhere else in the rack middleware. 并查看响应状态 - 如果它是200,而不是Rails应用程序中的问题,否则机架中间件中的任何其他位置。

rails -v #=> Rails 4.1.1

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

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