zilla
zilla

Reputation: 18

Rails custom error pages won't render on Nginx

I am new to Rails and Nginx. I was asked to add custom error pages to our app. I found instructions to do so on this site and my custom error pages render just fine locally. When I go to the url /no_such_page, I see the appropriate page. When I deploy the code to our test server running nginx and try the same url, I see the following instead:

500 Internal Server Error If you are the administrator of this website, then please read this web application's log file and/or the web server's log file to find out what went wrong.`

On the nginx server, if I go to the url \404, then I do see my page, so I know it renders ok. Both my local machine and the server are running under the development environment.

I've added this to config\development.rb:

config.consider_all_requests_local = false
config.exceptions_app = self.routes

Here is what I've added to routes.rb:

%w(404 500).each do |code|
    get code, to: "errors#show", :code => code, :via => :all
end

and my errors_controller looks like this:

class ErrorsController < ApplicationController
    def show
        status_code = params[:code] || 500
        render status_code.to_s
    end
end

Does anyone know if there is something special that I need to do in my nginx config to make this work?

Upvotes: 0

Views: 776

Answers (1)

Matt Brictson
Matt Brictson

Reputation: 11082

500 Internal Server Error If you are the administrator of this website, then please read this web application's log file and/or the web server's log file to find out what went wrong.

This error message is from Rails itself, not from Nginx.

https://github.com/rails/rails/blob/v5.0.0.1/actionpack/lib/action_dispatch/middleware/show_exceptions.rb#L15-L22

It means that your custom error page is itself raising an exception, which would lead to an infinite loop of error->show error page->error->show error page->... Rails detects this and halts with the error message you are seeing. Check your logs for this string to see what the error is:

Error during failsafe response:

Upvotes: 2

Related Questions