【问题标题】:Rails: image exists but is not rendered in production even though rendered in developmentRails:图像存在但即使在开发中渲染也不会在生产中渲染
【发布时间】:2020-03-02 01:25:57
【问题描述】:

类似的问题有thisthisthisthis,但都没有解决问题。

我们有一个本地存储的图像,在开发环境中呈现。

但是,在生产中,图像不会呈现。从浏览器访问图像 URL 会被重定向到 404 页面,因为由于某种原因找不到图像。同一目录中的其他图像渲染没有问题。

详情:-

  • 图片 1 网址:https://test.com/designs/thumbnails/foo/bar1.jpg

  • 图片 2 网址:https://test.com/designs/thumbnails/foo/bar2.jpg

  • 将两个图像 URL 直接输入浏览器会产生不同的结果。图像 1 将渲染,但图像 2 不会。图 2 重定向到 404 页面。

  • 这不是缓存问题,隐身渲染失败。

  • 两个图像都存在于目录中。

  • 这似乎发生在新图像上,尽管模式不清晰(即,一些新图像渲染但并非全部渲染)。

  • 图像渲染代码:<img class="thumbnail" src="<%= design["thumbnailURL"] %>"> 其中design["thumbnailURL"] 是图像的相对 URL(是的,确实存在)。

  • 我们使用 Rails 3.2 和 Cloudflare。

  • 服务器堆栈:

    • Passenger AppPreloader 版本:6.0.2
    • 实例:ElIQgS48 (nginx/1.15.8 Phusion_Passenger/6.0.2)
    • Ruby 版本:ruby 2.1.2p95
    • 操作系统:CentOS Linux 版本 7.6.1810(核心)
    • PHP 7.0.33
    • 服务器版本:5.5.60-MariaDB MariaDB 服务器
    • 后缀 mail_version = 2.10.1
    • Dovecot 版本:2.2.36 (1f10bfa63)

【问题讨论】:

  • 你能分享一下你的路线是什么样的吗?
  • 你在使用任何cdn吗?
  • 你能用图片显示线条吗?喜欢 image_tag
  • @AniketShivamTiwari 是的,使用 cloudflare。清除缓存并不能解决问题。
  • @kevinluo201 刚刚更新了问题。

标签: html ruby-on-rails image ruby-on-rails-3 browser


【解决方案1】:

如果您将文件托管在公用文件夹中并且不使用资产管道,则通常由 HTTP 网络服务器(apache、nginx ...)直接提供静态文件。他们做得又好又快。

请提供有关您的生产托管设置(网络服务器、配置等)的更多详细信息,以及 Rails 为您的图片生成的 url。我将使用相关的网络服务器配置相应地更新答案,以便直接提供您的静态文件。

【讨论】:

  • 获取网络服务器的详细信息。 rails 生成的 url 格式为 test.com/designs/thumbnails/foo/bar1.jpg.
  • 用堆栈详细信息更新了 q。感谢您的帮助!
  • 请提供NGINX配置
  • 是否有关于你想要的 nginx 配置的具体细节,或者只是 '/etc/nginx/nginx.conf` 中的整个文件?
【解决方案2】:

我刚刚查看了您的示例https://www.test.com/designs/thumbnails/foo/bar1.jpg,然后我检查了您的图像标签,我看到了<img src="img/test.com-logo.png" alt="Test.com create tests">,我能够将其更新为<img src="/img/test.com-logo.png" alt="Test.com create tests">,并且能够看到您的徽标。

您可以将您的 erb 从 <img class="thumbnail" src="<%= design["thumbnailURL"] %>"> 更新为 <img class="thumbnail" src="/<%= design["thumbnailURL"] %>"> 或者您可以确保在 design["thumbnailURL"] 的值中包含“/”我希望这会有所帮助

【讨论】:

  • 抱歉给您带来了困惑。 test.com 不是我们的实际站点。实际网站没有链接,因为害怕人们声称垃圾邮件。将在另一条评论中分享实际网站,然后在您查看后将其删除。
  • hotpot.ai/templates/app_store_panorama 这是损坏的缩略图(有时)出现的页面。阅读此评论后将删除。感谢您的帮助!
  • 刚刚在上一条评论中分享了网站
  • 很抱歉,但看起来没有损坏 :(
  • 是同样的问题吗?解决了吗?
【解决方案3】:

您使用资产管道吗? 如果是,请先进行预编译

RAILS_ENV=production rails assets:precompile

那就试试

# attempt 1
<img class="thumbnail" src="<%= 'assets/' + design["thumbnailURL"] %>">
# because the images would be compiled and under public/assets/
# I don't know exactly what's design["thumbnailURL"] contain, so I list this attempt

# attempt 2
<%= image_tag design["thumbnailURL"], alt: '...', class: 'thumbnail' %>
# image_tag will also append SHA256 suffix in the filename
# in production environment, all the assets will be appended a SHA256 to let the browser know its the latest assets, ex: aaa.jpg will be aaa-908e25f4bf641868d8683022a5b62f54.jpg, it may be the cause the browser cannot find the right one and return 404

# attempt 3
# if you don't want the images being precomiled, just put original images under public/ rather than app/assets/images/
# design["thumbnailURL"] should be the relative path to public/
# and you can remain your <img ...> same in view
# however, if you update the image, the users who already have been to your site would cache the old image and your new image will not be shown on their browser

部署新代码,看看它是否有效

【讨论】:

  • 您好,感谢您,但我们不使用资产管道。
  • 您的图片保存在哪里?公开场合?
  • @MZaragoza 是保存在公共文件夹中,所以我们只使用每个图像文件的直接链接
猜你喜欢
  • 2021-06-05
  • 1970-01-01
  • 1970-01-01
  • 2014-02-03
  • 2020-02-18
  • 1970-01-01
  • 2018-02-22
  • 2020-11-15
  • 2022-12-21
相关资源
最近更新 更多