【问题标题】:maintenance page for cap deploy does not work ( it is not shown )上限部署的维护页面不起作用(未显示)
【发布时间】:2012-06-27 15:48:06
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案1】:

    我只能帮助 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 { }
    

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

    【讨论】:

    • 感谢 Cliff 的清晰描述,这对我来说很有意义 - 所以我尝试了但问题仍然存在,我已经重新启动应用程序 + nginx 似乎 cap:deploy:web:disable 和服务器重新启动仍然没有显示维护页面。虽然它在那里
    • 如果你直接请求 /system/maintenance.html(使用 curl 或你的浏览器),你会在 Nginx 日志中得到什么?
    • 我现在正在看,是的。由于该路径不存在。问题是它的 /srv/books/current/.. 应用程序和 /srv/books/shared/system/maintance 所以我猜这就是它出错的地方。
    • 我可以将maintenance.html添加到/public,但是你可以做site.com/maintenance.html不好也不是因为索引搜索引擎,也许simlinking系统文件夹是这样去。我认为你的 nginx 配置很好很短我替换了我的!
    • 你也可以尝试在 try_files 之前使用别名
    【解决方案2】:

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

    【讨论】:

    • 感谢发帖,您的帖子是为乘客量身定制的。我已经有了 try_files 解决方案。可能对乘客有帮助
    【解决方案3】:

    如果您正在使用带有 unicorn 的 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
    

    这绝对会像魅力一样起作用!

    干杯!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-07
      • 2021-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多