How to Overwrite ActionController::RoutingError on Rails application to catch "bad Ip" -
i have daily bad request generates lot of logs in production.log this:
f, [2016-02-08t15:39:17.698761 #2437] fatal -- : actioncontroller::routingerror (no route matches [get] "/sites/default/files/elsevier/eop/s0025-7753(12)00231-x.pdf"): actionpack (4.0.2) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call' actionpack (4.0.2) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call...'
in website there isn't route /sites/`: foreign webservice error, , want know ip solve problem.
i have learned this, doesn't work , think need else of code
(i have rails 4.0.2 , upgrade 4.2.5 soon.)
1- in routes.rb
, @ end:
match '*path', :to => 'application#routing_error', via: :all
2- in application_ontroller
: log ip , i need render 404.html if i'm on production or render rails error normaly if i'm on development
def routing_error(error = 'routing error', status = :not_found, exception=nil) logger.debug { "---> catch #{request.remote_ip}" } render status: 404, file: "404.html" end
solution:
in routes.rb
class routingerrorconstrain def initialize @prodenv=rails.env=="production" end def matches?(request) @prodenv end end myapp::application.routes.draw ... match '*path', :to => 'application#render_routing_error', via: :all, constraints: routingerrorconstrain.new ... end
in application_controller.rb:
def render_routing_error logger.warn { "bad ip:--------------#{request.ip}---remote_ip:---#{request.remote_ip}---" } respond_to |format| format.html { render file: "public/404.html", layout: false, status: status } format.all { render nothing: true, status: status } end end
Comments
Post a Comment