1

我有一个使用 Nginx 和 Unicorn 在生产中运行的 Rails 3.1 应用程序。由于某种原因,我的自定义 404 和 500 html 错误页面没有显示。相反,我收到了实际的错误消息(例如“路由错误”)。

在我的 production.rb 文件中,我有config.consider_all_requests_local = false

在具有几乎相同配置的同一台服务器上,我有一个运行良好的“登台”站点。据我所知,唯一的区别是生产版本有 SSL,而暂存版本没有。

这是生产应用程序的 Nginx 配置:

upstream unicorn_myapp_prod {
  server unix:/tmp/unicorn.myapp_prod.sock fail_timeout=0;
}

server {
  listen 80;

  server_name myapp.com;

  root /home/deployer/apps/myapp_prod/current/public;

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @unicorn;
  location @unicorn {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://unicorn_myapp_prod;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}


server {
  listen 443 default;
  ssl on;
  ssl_certificate /home/deployer/apps/myapp_prod/shared/ssl_certs/myapp_prod.crt;
  ssl_certificate_key /home/deployer/apps/myapp_prod/shared/ssl_certs/myapp_prod.key;


  server_name myapp.com;

  root /home/deployer/apps/myapp_prod/current/public;

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @unicorn;
  location @unicorn {
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://unicorn_myapp_prod;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}

有任何想法吗?谢谢!

4

3 回答 3

2

如果您将 haproxy 与 nginx 和 unicorn 一起使用(例如,您在 Engineyard 上),则此修复还不够。您需要使用以下内容覆盖Rails

class ActionDispatch::Request
  def local?
    Rails.env != 'production'
  end
end

祝你好运!

于 2012-11-07T18:29:49.250 回答
2

The https listener's location @unicorn block is missing the X-Forwarded-For directive.

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

It's in your http listener, but not the https listener.

Assuming that Rails' force_ssl is successfully redirecting all of the http requests and your only errors are happening on https requests, it seems that would explain it.

Also, to be very clear, there is a well known problem in Rack/Rails3 with respect to routing errors, which you specifically mention.

https://rails.lighthouseapp.com/projects/8994/tickets/4444-can-no-longer-rescue_from-actioncontrollerroutingerror

于 2012-05-08T03:41:54.140 回答
0

不确定这是否适用,但在处理 /500.html 页面位置的 error_page 行之后,我们的 nginx 配置中也有一个链接

位置 = /500.html { 根 /path/to/rails/app/public; }

显然用您的路径替换 rails app 部分的路径。

于 2012-05-10T15:39:26.503 回答