如何在NGINX中设置自定义的503错误页面?

我学会了如何让NGINX返回503个客户错误页面,
但是我找不到如何做到以下几点:

示例配置文件:

    location / {
        root   www;
        index  index.php;
        try_files /503.html =503;
    }

    error_page 503 /503.html;
    location = /503.html {
        root   www;
    }

您可以看到,根据上面的代码,如果在我的根目录中找到一个名为503.html的页面,站点将返回此页面给用户.

但似乎尽管上面的代码工作,当有人只是访问我的网站打字

> http://www.example.com

它不会陷阱请求像:

> http://www.example.com/profile.php

使用我的代码,用户仍然可以看到配置文件页面或除index.php之外的任何其他页面.

问题:

当我的根文件夹中存在503.html时,如何将请求收集到我的站点中的所有页面,并将其转发到503.html?

最佳答案
已更新:将“if -f”更改为“try_files”.

尝试这个:

server {
    listen      80;
    server_name mysite.com;
    root    /var/www/mysite.com/;

    location / {
        try_files /maintenance.html $uri $uri/ @maintenance;

        # When maintenance ends,just mv maintenance.html from $root
        ... # the rest of your config goes here
     }

    location @maintenance {
      return 503;
    }

}

更多信息:

https://serverfault.com/questions/18994/nginx-best-practices

http://wiki.nginx.org/HttpCoreModule#try_files

dawei

【声明】:唐山站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。