【问题标题】:nginx doesn't honor subfolder error_page directivesnginx 不遵守子文件夹 error_page 指令
【发布时间】:2020-12-22 13:50:45
【问题描述】:

我正在尝试让我的非常简单的 nginx 提供静态文件以回退或以其他方式显示错误页面,如果它是这样的 404 或 50x 错误:

server {
  listen 80;
  server_name frontend;
  add_header X-Frame-Options "SAMEORIGIN";

  error_page 404 = @fallback404;
  location @fallback404 {
    index index.html;
    root /usr/share/nginx/html/404-error;
    internal;
  }

  error_page 500 502 503 504 @fallback5xx;
  location @fallback5xx {
    index index.html;
    root /usr/share/nginx/html/5xx-errors;
  }

  location /testing500 {
    fastcgi_pass unix:/does/not/exist;
  }

  location / {
    autoindex off;
    index index.html;
    root /usr/share/nginx/html;
    try_files $uri $uri/ =404;
  }
}

/testing/500返回计划nginx的404页面:

我还尝试将 error_page 指令声明为它们各自子文件夹的位置,在我的情况下,错误的实际 index.html 所在的子文件夹是:/404-error/index.html/5xx-errors/index.html,而不使用 @fallbackXXX 定义,效果是一样的。

我错过了什么? nginx 是否不尊重错误页面位于子文件夹或其他内容中?

【问题讨论】:

  • 我不认为location @fallback404 实际上做了很多。您应该将try_files /index.html =404; 添加到该位置以强制 Nginx 找到您的文件。
  • 这有资格作为答案,它有效,想发布它。所以我可以接受吗?

标签: nginx


【解决方案1】:

您需要在location @fallback404 块中添加try_files 语句以选择要返回的正确URI。

例如:

error_page 404 = @fallback404;
location @fallback404 {
    root /usr/share/nginx/html/404-error;
    try_files /index.html =404;
    internal;
}

但是,更简单的解决方案是在 error_page 语句本身中指定 URI。

例如:

error_page 404 /404-error/index.html;
location = /404-error/index.html {
    root /usr/share/nginx/html;
    internal;
}

【讨论】:

    猜你喜欢
    • 2020-08-31
    • 2015-03-04
    • 2015-03-11
    • 2021-03-08
    • 1970-01-01
    • 2014-07-20
    • 2014-10-08
    • 2018-09-13
    • 2016-01-21
    相关资源
    最近更新 更多