上限部署的维护页面不起作用(未显示)

时间:2012-06-27 15:48:07

标签: ruby-on-rails-3 deployment nginx capistrano setup-deployment

上限部署的维护页面不起作用。香港专业教育学院试图调试,但没有看到我的错误任何人看到我错过了什么,为什么它不工作? THX

当我确定上限时:web:disabled它没有显示维护页面而只显示应用程序!

在deploy.rb中我有:

  namespace :deploy do
  namespace :web do
     task :disable, :roles => :web do
       require 'erb'
       on_rollback { run "rm #{shared_path}/system/maintenance.html" }

       reason = ENV['REASON']
       deadline = ENV['UNTIL']
       template = File.read('app/views/layouts/maintenance.html.erb')
       page = ERB.new(template).result(binding)

       put page, "#{shared_path}/system/maintenance.html", :mode => 0644
     end
   end

我的应用程序的Nginx配置:

  upstream unicorn {
  server unix:/srv/books/shared/tmp/unicorn.sock fail_timeout=0;
}

server {
  listen 80 deferred;
  server_name books.ltd;

  root /srv/books/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;
  }

  if (-f $document_root/system/maintenance.html) {
        return 503;
  }

  error_page 503 @maintenance;
  location @maintenance {
      rewrite  ^(.*)$  /system/maintenance.html last;
      break;
  }

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

3 个答案:

答案 0 :(得分:3)

我只能帮助Nginx,但是我会这样做:

  root /srv/books/public;

  location / {
      try_files /system/maintenance.html $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;
  }

两者之间唯一的功能区别是用户将获得200而不是503,但我认为您正在使用该代码来利用错误处理来进行重写。

顺便提一下,您的解决方案不起作用的原因是您重写到/system/maintenance.html,但您没有可以提供该文件的位置。因此,Nginx再次搜索新位置,遇到您的try_files,并将请求传递给@unicorn,因为没有其他匹配位置。如果你添加了

location = /system/maintenance.html { }

那么你的解决方案可能会有效,但我仍然建议我的解决方案更简单,效率更高。

答案 1 :(得分:1)

经过一番研究后我找到了这个页面。它可能对有类似问题的人有所帮助。 http://blog.oncompare.com/2011/01/25/setting-up-a-maintenance-page-with-passenger-nginx-and-rails/

答案 2 :(得分:0)

你可以使用下面的例子,如果你正在使用nginx与unicorn一起替换一些路径 -

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

server {
  listen             80;
  server_name       example.com;

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

    location ~* ^/assets/ {
        root /home/deployer/apps/test/current/public;
        gzip_static on;
        expires max;
        add_header Cache-Control public;
        add_header Last-Modified "";
        add_header ETag "";
        break;
    }

  try_files $uri/index.html $uri @unicorn;
  location @unicorn {
    proxy_set_header  X-Real-IP       $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header  Host $http_host;
    proxy_redirect    off;
    proxy_pass        http://unicorn;
  }
  error_page 503 @503;
    if (-f $document_root/system/maintenance.html) {
     return 503;
    }
  error_page 500 502 503 504 /500.html;
    location @503 {
      rewrite ^(.*)$ /system/maintenance.html break;
    }
  client_max_body_size 4G;
  keepalive_timeout 30;
    proxy_connect_timeout 300;
    proxy_send_timeout    300;
    proxy_read_timeout    300;
}

这是我可行的nginx配置,您只需添加下面的代码即可。

  error_page 503 @503;
    if (-f $document_root/system/maintenance.html) {
     return 503;
    }
  error_page 500 502 503 504 /500.html;
    location @503 {
      rewrite ^(.*)$ /system/maintenance.html break;
    }

然后在deploy.rb或production.rb capistrano文件中添加以下代码

namespace :deploy do
  namespace :web do
    desc "Enable maintenance mode for apache"
    task :disable, :roles => :web do
      on_rollback { run "rm -f #{shared_path}/system/maintenance.html" }
      page = File.read('public/maintenance.html')
      put page, "#{shared_path}/system/maintenance.html", :mode => 0644
    end

    desc "Disable maintenance mode for apache"
    task :enable, :roles => :web do
      run "rm -f #{shared_path}/system/maintenance.html"
    end
  end 
end

在公共文件夹中添加具有以下内容的maintenance.html文件

<!DOCTYPE html>
<html>
<head>
  <title>The page you were looking for doesn't exist (404)</title>
  <style type="text/css">
    body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
    div.dialog {
      width: 48em;
      padding: 0 4em;
      margin: 4em auto 0 auto;
      border: 1px solid #ccc;
      border-right-color: #999;
      border-bottom-color: #999;
    }
    h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
  </style>
</head>

<body>
  <div class="dialog">
    <h1>Maintenance Mode</h1>
    <p>"We are currently carrying out essential scheduled maintenance.
        Normal service will resume shortly.  Apologies for any inconvenience." </p>

    <p>Sorry for the inconvenience!</p>
  </div>
</body>
</html>

以下是可以使用的rake任务 -

cap deploy:web:enable
cap deploy:web:disable

cap production deploy:web:enable
cap production deploy:web:disable

cap staging deploy:web:enable
cap staging deploy:web:disable

这肯定会像魅力一样发挥作用!

干杯!