Nginx 不从 if 表达式返回自定义错误页面

Nginx not returning custom error pages from if expression

这是我的 CDN 和 conf 文件包含 "few lines"。

情况真的很奇怪:

在配置中我有

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 页面。

问题是:为什么?

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

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

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