【问题标题】:Rails ActiveStorage / Cloudinary Not Redirecting Image Request to HTTPSRails ActiveStorage / Cloudinary 未将图像请求重定向到 HTTPS
【发布时间】:2020-03-15 12:36:29
【问题描述】:

ActiveStorage image_url 帮助器生成一个指向 Rails Web 服务器上图像的 URL。当 Web 服务器上收到该请求时,它会被重定向到 Cloudinary 上的 URL 以请求图像。 ActiveStorage 使用 https 协议生成图像 URL,但 Rails Web 服务器使用 http(无 ssl)协议生成到 Cloudinary 的图像 URL。我无法确定原因。

请求的日志:

Started GET "/rails/active_storage/blobs/really_long_hash/user-2.png"
Processing by ActiveStorage::BlobsController#show as JPEG
Parameters: {"signed_id"=>"really_long_hash", "filename"=>"nsi-site-bg"}
ActiveStorage::Blob Load (0.8ms) SELECT "active_storage_blobs".* FROM "active_storage_blobs" WHERE "active_storage_blobs"."id" = $1 LIMIT $2 [["id", 48], ["LIMIT", 1]]
Cloudinary Storage (4.3ms) Generated URL for file at key: cloudinary_file_name (http://res-4.cloudinary.com/hcfhlrdjg/image/upload/cloudinary_file_name.jpg)
Redirected to http://res-4.cloudinary.com/hcfhlrdjg/image/upload/cloudinary_file_name.jpg
Completed 302 Found in 48ms (ActiveRecord: 13.8ms)

cloudinary.yml

development:
  cloud_name: <%= ENV['CLOUDINARY_CLOUD_NAME'] %>
  api_key: <%= ENV['CLOUDINARY_API_KEY'] %>
  api_secret: <%= ENV['CLOUDINARY_API_SECRET'] %>
  secure: true
  cdn_subdomain: true
production:
  cloud_name: <%= ENV['CLOUDINARY_CLOUD_NAME'] %>
  api_key: <%= ENV['CLOUDINARY_API_KEY'] %>
  api_secret: <%= ENV['CLOUDINARY_API_SECRET'] %>
  secure: true
  cdn_subdomain: true
test:
  cloud_name: <%= ENV['CLOUDINARY_CLOUD_NAME'] %>
  api_key: <%= ENV['CLOUDINARY_API_KEY'] %>
  api_secret: <%= ENV['CLOUDINARY_API_SECRET'] %>
  secure: true
  cdn_subdomain: true

我刚刚将 cloudinary gem 更新到 1.13.0 并没有看到任何变化。如果您还需要什么,请告诉我。

【问题讨论】:

    标签: ruby-on-rails https cloudinary imageurl


    【解决方案1】:

    Cloudinary gem 提供了一个 cl_image_tag 帮助器,它生成一个 HTML img 标记,直接链接到 Cloudinary 服务器上的图像

    您应该更喜欢使用此帮助程序而不是 ActiveStorage url 帮助程序,因为此帮助程序标记生成到图像的直接链接,而不是到您的服务器的链接。通过直接从 Cloudinary 访问图像,您可以获得使用 CDN 的全部好处。要拥有https,只需在标签中指定secure: true

    例如:如果您有 User 记录 has_one_attached :file。您可以像这样使用cl_image_tag

    cl_image_tag(user.file.key, secure: true)
    

    【讨论】:

    • 谢谢@nwocha-adim。那效果很好。我也在使用cl_image_path(user.file.key, secure: true) 在需要时获取网址。
    猜你喜欢
    • 2020-04-24
    • 2021-04-12
    • 2015-03-02
    • 1970-01-01
    • 2016-10-23
    • 1970-01-01
    • 2013-04-06
    • 2019-09-08
    • 1970-01-01
    相关资源
    最近更新 更多