2011-06-10 26 views
7

我正在使用MaintenanceModeMiddleware将我的网站置于维护模式,但它要求您在远程服务器上的settings.py文件中进行更改。我想用fabric来远程将网站置于维护模式。有没有办法做到这一点?还是有更好的方法来做到这一点?谢谢。反正有django站点进入维护模式使用面料?

[更新]

感谢回馈大家在这个到底是什么,我没有和它为我的伟大工程,http://garthhumphreys.com/2011/06/11/painless-django-maintenance-mode-with-fabric/ - 我喜欢的取消注释行的想法,但我的设置,如果我是在生产服务器上执行该操作时,一旦我将新版本推出后,它将被覆盖,因此最终将该网站从服务器级别置于维护模式,而不是django级别运行得更好,而且更加简单和灵活我至少:)

+0

This [question](http://stackoverflow.com/questions/6237514/write-to-a-remote-file-with-fabric)可能有关。我对织物不太了解,我不是故意粗鲁,但是,没有使用SSH并且更改一个设置线相当容易?这是一个远程方法 – 2011-06-10 16:58:31

+0

由于浏览器缓存,我发现Garth Humphreys的方法并不好。它导致以下情况之一: - 在维护模式期间看到非维护模式页面 - 未处于维护模式时看到维护模式页面 我会将我的调整解决方案放在单独的答案中。 – seddonym 2013-05-24 10:51:06

回答

8

面料确实有命令来帮助您评论或取消注释给定文件中的行fabric.contrib.files。请参阅文档:http://docs.fabfile.org/en/1.0.1/api/contrib/files.html

个人而言,我更喜欢在前端代理而不是在Django中间件中处理此问题。我会看看Show a custom 503 page if upstream is down这个问题,它将Nginx配置为在上游关闭时使用自定义页面。

+0

但管理员/员工用户仍然可以进入没有503s?中间件看起来很酷,特别是对于那些倾向于在生产服务器上开发的人。当Django项目实际上停止时,Nginx 503会很好。 – 2011-06-10 21:26:15

+0

@j_syk没有理由不能同时使用两者。如果Django服务器出现故障,那么中间件不能工作,Nginx会启动。 – 2011-06-11 01:31:50

+0

感谢大家的反馈,最终我做到了这一点,它对我很好,http://garthhumphreys.com/2011/06/11/painless-django-maintenance-mode-with-fabric/- 我喜欢取消注释行的想法,但是使用我的设置,如果我要在生产服务器上执行该操作,只要推入新版本就会覆盖它所以最终把网站从服务器级别进入维护模式,而不是Django级别的工作好多了,真的更容易和灵活,对我来说至少:) – 2011-06-12 00:21:03

2

有一个fork of django-maintenancemode允许通过设置数据库中的值来打开/关闭维护模式。例如,通过这种方式,您可以创建一个简单的管理命令来切换维护模式并通过结构调用它。我认为这比使用mod_rewrite更灵活。

+0

这意味着每个通过django服务的页面都需要额外的命中数据库吗? – Matt 2015-09-14 02:10:28

4

我的解决办法:

  1. 通过URL配置创建一个维护模式模板,并链接到它,所以当访问/维修不足/显示维护页面。
  2. 然后,配置apache以测试是否存在'maintenance-mode-on'文件,如果存在,请将302重定向到维护模式页面的url。
  3. 如果存在“维护模式关闭”文件,则配置apache将重定向到主页的维护模式URL。
  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的处理维护模式期间的请求;这将是很好的服务一个静态文件。