【问题标题】:NGINX and Gatsby: Setting up nginx to route to Gatsby 404 pageNGINX 和 Gatsby:设置 nginx 以路由到 Gatsby 404 页面
【发布时间】:2021-04-13 12:26:34
【问题描述】:

我有一个使用 nginx 设置的 Gatsby 站点。我知道 Gatsby 使用 pages/404.js 中存在的任何组件作为 404 页面。如果我导航到www.mysite.com/404,我可以看到我创建的自定义 Gatsby 404 页面,但这是我唯一一次看到 Gatsby 404 组件。如果我导航到 www.mysite.com/blahblahblah 或任何其他不存在的页面,我会看到默认的白色 nginx 404 页面。

我对 Gatsby 很熟悉,但对 nginx 却是个菜鸟。当用户导航到不存在的页面而不是显示默认的 nginx 404 页面时,如何设置我的 nginx 配置以将我的用户重定向到我的自定义 Gatsby 404 组件?

这是我的 nginx 配置:

    server {
    listen 80;
    sendfile on;
    
    default_type application/octet-stream;

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

【问题讨论】:

    标签: nginx http-status-code-404 config


    【解决方案1】:

    我也遇到了同样的情况。我将分享(似乎)对我有用的东西。和你一样,我正在使用 Gatsby,并且我在 pages/404.js 有一个文件,我想将其用作丢失路线的全部信息。我发现@ivo-gelov 给出的答案在这里很有帮助:How nginx process =404 fallback in try_files

    具体来说,我只是添加了一个error_page定义来匹配=404错误代码:

      error_page 404 /404.html;
    
      location / {
          try_files $uri $uri/ /index.html =404;
      }
    

    【讨论】:

      猜你喜欢
      • 2020-07-21
      • 2021-12-03
      • 2020-03-06
      • 2020-01-04
      • 2016-04-16
      • 2018-05-10
      • 2021-05-20
      • 1970-01-01
      • 2021-08-07
      相关资源
      最近更新 更多