1

上限部署的维护页面不起作用。我尝试调试但没有看到我的错误有人看到我在这里想念的东西为什么它不起作用?谢谢

当我做 cap: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;
}
4

3 回答 3

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 { }

那么您的解决方案可能会起作用,但我仍然建议我的解决方案更简单,更高效。

于 2012-06-27T19:43:40.683 回答
1

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

于 2012-09-11T09:42:31.063 回答
0

如果您正在使用带有独角兽的 nginx,您可以使用下面的示例,因为它只是替换一些路径 -

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

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

干杯!

于 2014-11-11T12:37:39.533 回答