gpt4 book ai didi

ruby-on-rails - Rails 3 - 生产模式下的开发错误

转载 作者:行者123 更新时间:2023-12-04 07:33:35 26 4
gpt4 key购买 nike

我在我的生产服务器上使用 Rails、Passenger(都是 3.0.5)和 Nginx。据我所知,Rails 应该显示 public/404.htmlpublic/500.html而不是像 ActiveRecord::RecordNotFound 这样的开发错误或 Unknown action但这不会发生。我试图删除 config.ru 文件并在 nginx.conf 中设置 rack_env 或 rails_env 但没有任何帮助。

这是我的 nginx.conf:

worker_processes  1;

events {
worker_connections 1024;
}

http {
passenger_root /home/makk/.rvm/gems/ruby-1.9.2-p0/gems/passenger-3.0.5;
passenger_ruby /home/makk/.rvm/bin/passenger_ruby;
#passenger_ruby /home/makk/.rvm/wrappers/ruby-1.9.2-p0/ruby;

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

sendfile on;
keepalive_timeout 65;

server {
listen 80;
server_name localhost;

location / {
root /home/makk/projects/1server/deploy/current/public;
index index.html index.htm;
passenger_enabled on;
rack_env production;

recursive_error_pages on;

if (-f /home/makk/projects/1server/maintenance.html) {
return 503;
}

error_page 404 /404.html;
error_page 500 502 504 /500.html;
error_page 503 @503;
}

location @503 {
error_page 405 = /maintenance.html;

# Serve static assets if found.
if (-f $request_filename) {
break;
}
rewrite ^(.*)$ /maintenance.html break;
}

location ~ ^(\/phpmyadmin\/)(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(\/phpmyadmin\/)(.*)$;
fastcgi_param SCRIPT_FILENAME /usr/share/phpmyadmin/$fastcgi_path_info;
include fastcgi_params;
}
}
}

似乎这个问题重复 this one但没有工作建议。

UPD :我在同一台 PC 上同时拥有开发和生产应用程序。在生产中 Rails 忽略 config.consider_all_requests_local = false (在/config/environments/production.rb 中)由于 local_request?方法。因此,下面列出了一种可能的解决方案(取自 here ):
# config/initializers/local_request_override.rb
module CustomRescue
def local_request?
return false if Rails.env.production? || Rails.env.staging?
super
end
end

ActionController::Base.class_eval do
include CustomRescue
end

或者对于 Rails 3:
class ActionDispatch::Request
def local?
false
end
end

最佳答案

要在 Rails 3 中使用它,您必须执行以下操作:

首先,创建您的 404 和 500 错误页面。我把我的放在 app/views/errors/404.html.erbapp/views/errors/500.html.erb .

其次,将以下内容添加到 application_controller.rb:

unless Rails.application.config.consider_all_requests_local
rescue_from Exception, :with => :render_error
rescue_from ActiveRecord::RecordNotFound, :with => :render_not_found
rescue_from AbstractController::ActionNotFound, :with => :render_not_found
rescue_from ActionController::RoutingError, :with => :render_not_found
rescue_from ActionController::UnknownController, :with => :render_not_found
rescue_from ActionController::UnknownAction, :with => :render_not_found
end

def render_error exception
Rails.logger.error(exception)
render :template => "/errors/500.haml", :status => 500
end

def render_not_found exception
Rails.logger.error(exception)
render :template => "/errors/404.haml", :status => 404
end

最后,让你的 production.rb 不考虑所有本地请求:
config.consider_all_requests_local = false

P.S:请记住,当发生完整的路由错误时 - 即当绝对没有路由匹配时,而不仅仅是 ActiveRecord NotFound 错误时,public/404.html 将被显示,因此最好将其设置到位。我通常只是将它重定向到我的 errors_controller,这确保了后面提到的异常没有捕获的任何 404 错误仍然正确地重定向到 ErrorsController。
<script type="text/javascript">
<!--
window.location = "<%= request.host_with_port %>/errors/404"
//-->
</script>

关于ruby-on-rails - Rails 3 - 生产模式下的开发错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5330290/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com