【问题标题】:Nginx not returning custom error pages from if expressionNginx 不从 if 表达式返回自定义错误页面
【发布时间】:2015-11-28 00:04:40
【问题描述】:

这是我的 CDN 和 conf 文件包含“几行”。

情况真的很奇怪:

在配置中我有

error_page 403 = /e403;
error_page 404 = /e404;

location =/e403 {
    default_type text/html;
    return 403 "somehtml403";
}
location =/e404 {
    default_type text/html;
    return 404 "somehtml404";
}

同时我有 args fitler(禁止使用 args,这是一个 CDN):

if ($args !~ ^$){
    return 404;
}
if ($request ~* (^.*\?.*$)){
    return 404;
}

当我请求/cookies.txt 时,我有我的自定义 404 页面。当我请求 /cookies.txt?onemore/cookies.txt? 时,我有 nginx 的 404 页面。

问题是:为什么?

【问题讨论】:

    标签: nginx cdn custom-error-pages


    【解决方案1】:

    $if 语句直接位于server 块内时,我可以复制该问题,但当它们位于顶级位置块内时它可以正常工作。这对我有用:

    location / {
      if ($args !~ ^$){
        return 404;
      }
      if ($request ~* (^.*\?.*$)){
        return 404;
      }
    }
    

    当然,如果你有其他的位置块,你可能也需要include那里的规则。

    【讨论】:

    • 它很亲切,很棒!谢谢!!
    猜你喜欢
    • 2011-03-08
    • 2013-02-27
    • 2014-11-27
    • 1970-01-01
    • 1970-01-01
    • 2014-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多