【问题标题】:rails app on nginx+passenger not showing custom error pagesnginx+passenger 上的 rails 应用程序不显示自定义错误页面
【发布时间】:2013-05-12 16:12:18
【问题描述】:

我有一个 Rails 应用程序在 nginx 1.2.0 和乘客 3.0.7 上运行。我希望在应用程序中发生相应的 http 错误时显示 rails 应用程序中的自定义错误页面(例如 /rail_app/public/500.html)。

这是我当前的 nginx 配置文件:

http {
    passenger_root /usr/lib/ruby/gems/1.8/gems/passenger-3.0.7;
    passenger_ruby /usr/bin/ruby;

    include       mime.types;
    default_type  application/octet-stream;

    #access_log  /opt/nginx/logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    server {
        listen 80;
        server_name localhost;
        root /var/www/dashboard/current/public;
        passenger_enabled on;
        passenger_min_instances 1;
#       listen 443;
#       ssl on;
#       ssl_certificate /opt/nginx/conf/server.crt;
#       ssl_certificate_key /opt/nginx/conf/server.key;
        error_page 500 502 503 504 /500.html;
        location = /500.html {
            root /var/www/dashboard/current/public/;
        }
    }
}

此配置不显示 rails 应用程序客户错误页面,而只是将 http 错误状态代码发送到客户端。

有谁知道让 nginx/passenger 向客户端发送带有 http 错误状态代码的 rails 应用程序自定义错误页面需要什么?

【问题讨论】:

    标签: ruby-on-rails nginx passenger


    【解决方案1】:

    请尝试以下方法:

    # We use the x just because it is for all 5xx errors.
    error_page 500 502 503 504 /5xx.html;
    location = /5xx.html {
        alias /var/www/dashboard/current/public/;
    }
    

    重新配置root 指令没有意义,因为它已经设置为您之前指定的路径。 alias 确保特定位置在内部匹配到文件系统上的不同位置。所有传入的请求参数都应该被传递,如果你的 Rails 应用程序正在处理这些事情,它应该回答。只需确保您的 Rails 应用不再以 500 状态响应(我不知道那时会发生什么)。

    相关链接

    【讨论】:

      【解决方案2】:

      您的 nginx 配置中可能缺少 passenger_intercept_errors on;

      请参阅passenger docs 了解更多信息

      【讨论】:

        【解决方案3】:

        我使用的配置:

        error_page   500 502 503 504  /50x.html;
        
        location = /50x.html {
           root   html;
        }
        

        【讨论】:

        • 本杰明,感谢您的回复。我试过了,但没有用。这是关于我要完成的工作的更多详细信息:当数据库关闭时...我希望 rails 应用程序显示自定义 500 错误页面,而不是仅将 http 500 发送到客户端。也许我应该捕捉到 activerecord 异常?只是不确定如何。
        • 产品日志中的错误:/!\ FAILSAFE /!\ Fri Jun 14 10:29:18 -0400 2013 Status: 500 Internal Server Error Can't connect to MySQL server on '169.254.1.51' (113) /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.17/lib/active_record/connection_adapters/mysql_adapter.rb:620:in real_connect'`
        猜你喜欢
        • 1970-01-01
        • 2016-06-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多