Nginx 301重定向自定义页面不起作用,仍然返回其中包含 "nginx"的页面

标签 nginx nginx-config

因此,我们需要隐藏 nginx 是我们的服务器,而它暴露自己的方式之一就是通过它的错误页面。为此,我们有一个片段:

error_page 404 /404.html;
error_page 403 /403.html;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /500.html;

# Non-rails error pages
error_page 301 /error/301.html;
error_page 307 /error/307.html;
error_page 308 /error/308.html;
error_page 400 /error/400.html;
error_page 401 /error/401.html;
error_page 405 /error/405.html;
error_page 406 /error/406.html;
error_page 408 /error/408.html;
error_page 413 /error/413.html;
error_page 415 /error/415.html;
error_page 429 /error/429.html;
error_page 431 /error/431.html;

location ^~ /error/ {
  internal;
  root /usr/share/nginx/html;
}

包含在 server {} block 中:

server {
  listen 80;
  server_name ourhost.com
  include /etc/nginx/snippets/error_pages.conf;
  return 301 https://$server_name$request_uri;
}

所有页面仅包含错误号和消息,没有任何泄露信息。但是,301页面仍然返回:

<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>

这不是我们想要的。如何加载自定义 301 页面而不是默认的 nginx 页面?

最佳答案

这里的答案有帮助:https://serverfault.com/questions/326877/nginx-error-page-directive-is-silently-ignored

location/{} 中关闭 return 可解决该问题。

关于Nginx 301重定向自定义页面不起作用,仍然返回其中包含 "nginx"的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50550755/

相关文章:

redirect - 域的 SSL,包括子域

kubernetes-ingress - SPA 应用程序(Vue、React、Angular)在 Kubernetes 上的 Nginx 入口 Controller 后面无法正常工作

java - nginx.conf中的 "location/logs"和 "location/logs/"有什么区别

ubuntu - 恢复完整的nginx配置

regex - Nginx 位置正则表达式,是否需要转义正斜杠?

nginx gzip_static 不会自动创建不存在的gz 文件?

django - nginx 权限在 Ubuntu 上被拒绝

php - 在几秒钟内处理数千个请求的最佳 Nginx 配置

ssl - http -> https 后 nginx 没有找到静态内容

nginx - Certbot,指定重定向而不回答问题