【问题标题】:NGINX Image not found未找到 NGINX 图像
【发布时间】:2020-12-09 08:25:03
【问题描述】:

我正在尝试制作自定义 404 页面。

这是我的 404.html:

<html>
<head>
    <meta charset="UTF-8">
    <title>Not Found</title>
</head>
<body>
    <div style="...">
      <div style="...">404</div>
      <div style="...">Page not found</div>
      <img src="404.gif" alt="not found" />
    </div>
</body>
</html>

它工作(几乎),但我总是在尝试加载 404.gif 时收到“未找到”错误

这是我的 nginx 配置(只想让 localhost/main/datalocalhost/main/test 路径可用):

http {
  server {
    listen 80;
    server_name localhost;
    error_page 404 /404.html;

    location = /404.html {
      root /var/www/error/;
    }

    location /404.gif {
      root /var/www/error/;
    }

    location ~ ^/main/(data|test)$ {
    }

当我尝试访问像 http://localhost/main/error_page 这样的任何页面时,服务器返回没​​有 gif 的自定义 404 页面和错误 GET http://localhost/main/404.gif 404 (Not Found)

如何加载我的 gif,我需要在 html 中提供什么路径或如何更改配置?

【问题讨论】:

  • 检查/var/log/nginx/errors.log中的错误,它会告诉你它试图从哪里获取图像,从那里开始调试

标签: html nginx nginx-config


【解决方案1】:

在图片标签src中使用正确的图片路径

<img src="../somefolder/somefoler/404.gif" alt="not found" />

【讨论】:

    【解决方案2】:

    需要像这样重定向这个gif的任何请求

    location ~* /(.*)404.gif$ {
        try_files $uri $uri/ /404.gif;
    }
    

    【讨论】:

      猜你喜欢
      • 2019-07-20
      • 2015-01-04
      • 1970-01-01
      • 1970-01-01
      • 2017-05-18
      • 2013-04-30
      • 2020-11-09
      • 2021-07-12
      • 1970-01-01
      相关资源
      最近更新 更多