【问题标题】:NGINX custom 404 page without redirectNGINX 自定义 404 页面没有重定向
【发布时间】:2016-10-06 00:38:47
【问题描述】:

默认情况下,当 NGINX 返回 404 时,它会呈现一些默认的 HTML。例如,类似:

<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.10.0 (Ubuntu)</center>
</body>
</html>

我想自定义此 HTML,但保持 URL 不变。我已经尝试为 404s 设置错误页面:

error_page 404 /404.html

但这首先将浏览器重定向到/404.html

有没有办法在不重定向的情况下自定义 404 HTML?

【问题讨论】:

  • error_page 404 /404.html; 不应生成重定向。您可以通过指定完整的 URL 来强制重定向,但在您的问题中,它不应该生成重定向。详情请见this document
  • 哦,你是对的。我又试了一次,它按预期工作。不知道我有什么配置设置让它重定向到/404.html。谢谢!

标签: nginx


【解决方案1】:

也许你可以通过使用 nginx 的 error_pageproxy_intercept_errors 指令来做到这一点

详细配置

server {
    ## other config
    ## here the proxy_intercept_errors directive is the key
    proxy_intercept_errors on;
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        ## your 50x.html file parent directory
        root   /var/www; 
    }
    error_page   404  /404.html;
    location = /404.html {
        ## your 404.html file parent directory
        root   /var/www; 
    }
}

nginx官网文档:

proxy_intercept_errorserror_page

【讨论】:

    【解决方案2】:

    这个配置对我有用

    error_page 404 /404.html;
    location = /404.html {
        internal;
    }
    

    http://nginx.org/en/docs/http/ngx_http_core_module.html#internal

    【讨论】:

      猜你喜欢
      • 2013-06-18
      • 2019-02-03
      • 2010-11-04
      • 2012-10-01
      • 1970-01-01
      • 2019-09-14
      • 2015-08-01
      • 1970-01-01
      • 2016-11-21
      相关资源
      最近更新 更多