加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > Nginx > 正文

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

发布时间:2020-12-13 21:28:27 所属栏目:Nginx 来源:网络整理
导读:我学会了如何让NGINX返回503个客户错误页面, 但是我找不到如何做到以下几点: 示例配置文件: location / { root www; index index.php; try_files /503.html =503; } error_page 503 /503.html; location = /503.html { root www; } 您可以看到,根据上面的

我学会了如何让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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读