8

現在、MaintenanceModeMiddleware を使用してサイトをメンテナンス モードにしていますが、リモート サーバーの settings.py ファイルを変更する必要があります。ファブリックを使用して、リモートでサイトをメンテナンス モードにしたいと考えています。これを達成する方法はありますか?または、これを行うためのより良い方法はありますか? ありがとう。

[アップデート]

みんなのフィードバックに感謝行のコメントを外すというアイデアですが、私の設定では、本番サーバーでそれを行うと、新しいバージョンをプッシュすると上書きされるため、最終的にサイトを django レベルではなくサーバーレベルからメンテナンスモードにします少なくとも私にとっては、はるかにうまく機能し、本当に簡単で柔軟です:)

4

3 に答える 3

9

Fabric には、 の特定のファイルの行をコメント化またはコメント解除するのに役立つコマンドがありますfabric.contrib.files。ここのドキュメントを参照してください: http://docs.fabfile.org/en/1.0.1/api/contrib/files.html

個人的には、Django ミドルウェアではなく、フロントエンド プロキシでこれを処理することを好みます。アップストリームがダウンしている場合はカスタム 503 ページを表示するこの質問を見て、アップストリームがダウンしているときにカスタム ページを使用するように Nginx を構成します。

于 2011-06-10T19:13:19.170 に答える
4

私の解決策:

  1. メンテナンス モード テンプレートを作成し、urlconf を介してリンクします。これにより、/under-maintenance/ にアクセスするとメンテナンス ページが表示されます。
  2. 次に、「maintenance-mode-on」ファイルの存在をテストするように apache を構成し、存在する場合は、メンテナンス モード ページの URL への 302 リダイレクトを実行します。
  3. 「maintenance-mode-off」ファイルが存在する場合は、メンテナンス モードの URLからホームページにリダイレクトするように Apache を構成します。
  4. メンテナンス モード オンとメンテナンス モード オフの間のファイルの切り替えを容易にするファブリック スクリプト。

Apache 構成ファイルの関連セクションは次のとおりです。

RewriteEngine On
# If this file (toggle file) exists then put the site into maintenance mode
RewriteCond /path/to/toggle/file/maintenance-mode-on -f
RewriteCond %{REQUEST_URI} !^/static.* 
RewriteCond %{REQUEST_URI} !^/admin.* 
RewriteCond %{REQUEST_URI} !^/under-maintenance/ 
# redirect to the maintenance mode page
RewriteRule ^(.*) /under-maintenance/ [R,L]

#If not under maintenance mode, redirect away from the maintenance page
RewriteCond /path/to/toggle/file/maintenance-mode-off -f
RewriteCond %{REQUEST_URI} ^/under-maintenance/
RewriteRule ^(.*) / [R,L]

次に、ファブリック スクリプトの関連部分:

env.var_dir = '/path/to/toggle/file/'

def is_in_mm():
    "Returns whether the site is in maintenance mode"
    return files.exists(os.path.join(env.var_dir, 'maintenance-mode-on'))

@task
def mm_on():
    """Turns on maintenance mode"""
    if not is_in_mm():
        with cd(env.var_dir):
            run('mv maintenance-mode-off maintenance-mode-on')
            utils.fastprint('Turned on maintenance mode.')
    else:
        utils.error('The site is already in maintenance mode!')


@task
def mm_off():
    """Turns off maintenance mode"""
    if is_in_mm():
        with cd(env.var_dir):
            run('mv maintenance-mode-on maintenance-mode-off')
            utils.fastprint('Turned off maintenance mode.')
    else:
        utils.error('The site is not in maintenance mode!')

これはうまく機能しますが、メンテナンス モード中の Django 処理リクエストに依存します。静的ファイルを提供するだけでいいでしょう。

于 2013-05-24T11:03:31.497 に答える
2

データベースに値を設定することでメンテナンスモードのオン/オフを切り替えることができるdjango-maintenancemodeのフォークがあります。このようにして、たとえば、単純な管理コマンドを作成してメンテナンス モードを切り替え、ファブリック経由で呼び出すことができます。mod_rewriteを使うより柔軟だと思います。

于 2011-09-06T04:16:57.997 に答える