nginx 维护页面,最佳实践

NeD*_*ark 20 maintenance nginx best-practices

我想配置服务器以在它存在时显示维护页面。我试过这个代码并且工作:

location / {
    try_files /maintenance.html $uri $uri/ @codeigniter;
}
Run Code Online (Sandbox Code Playgroud)

但我注意到它会带有 200 状态代码,这可能会给搜索引擎造成混乱。我认为最好的做法是返回 503 状态代码。在谷歌上,我找到了几个关于它的相关页面,就像这样。但是,他们使用 if 进行重定向,并且根据 nginx 文档,使用 ifs 是不安全的。

有没有办法不使用if来做到这一点?在这种情况下使用是否安全?

谢谢。

Mik*_*ike 27

这就是我所做的。

            if (-f $document_root/maintenance.html) {
                    return 503;
            }
            error_page 503 @maintenance;
            location @maintenance {
                    rewrite ^(.*)$ /maintenance.html break;
            }
Run Code Online (Sandbox Code Playgroud)

如果文件在那里,它将显示维护页面。删除文件后,您将恢复正常。

  • 听起来这会影响性能:NGINX 需要为每个请求检查文件是否存在...... (5认同)
  • 是的,这与问题链接上的代码相同。我实际上是在问在这种情况下使用 `if` 是否安全,因为根据 [documentation](http://wiki.nginx.org/IfIsEvil) 不应该使用它。 (2认同)
  • 马克,不是因为经常访问的文件存储在内存中的文件系统缓存中。 (2认同)

qua*_*nta 8

我认为最好的做法是返回 500 状态代码。

我想你的意思是 503 而不是 500。

它们用于if进行重定向,根据 nginx 文档,使用 ifs 是不安全的。

不,只有return100%安全的内部iflocation环境。

根据nginx 文档,您可以指定 HTTP 状态代码作为try_files. 我试过这个,但没有用。


小智 5

是的,将 HTTP 503 用于临时非常重要。重定向。我是这样解决的:

server {
        listen      80;
        server_name joergfelser.at;
        root    /var/www/joergfelser.at/;

        location / {
            if (-f $document_root/maintenance.html) {
                return 503;
           }
            ... # rest of your config, it's important to have 
            ... # the maintenance case at the very top
         }

        error_page 503 @maintenance;
        location @maintenance {
                rewrite ^(.*)$ /maintenance.html break;
        }
}
Run Code Online (Sandbox Code Playgroud)

我还写了一篇关于该主题的博客文章:https :
//www.joergfelser.at/redirecting-to-a-custom-nginx-maintenance-page/

快乐维护;)