简体   繁体   English

Rails ActiveStorage / Cloudinary 未将图像请求重定向到 HTTPS

[英]Rails ActiveStorage / Cloudinary Not Redirecting Image Request to HTTPS

The ActiveStorage image_url helper generates a URL to the image on the Rails web server. ActiveStorage image_url helper 为 Rails web 服务器上的图像生成 URL。 When that request is received on the web server it is redirected to a URL on Cloudinary to request the image.当在 web 服务器上收到该请求时,它将被重定向到 Cloudinary 上的 URL 以请求图像。 ActiveStorage is generating an image URL with the https protocol but the Rails web server is generating an image URL to Cloudinary with the http (no ssl) protocol. ActiveStorage is generating an image URL with the https protocol but the Rails web server is generating an image URL to Cloudinary with the http (no ssl) protocol. I have not been able to determine why.我无法确定原因。

The Log of the request:请求的日志:

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 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

I just updated the cloudinary gem to 1.13.0 and didn't see a change.我刚刚将 cloudinary gem 更新到 1.13.0 并没有看到任何变化。 If you need anything else let me know.如果您还需要什么,请告诉我。

Cloudinary gem provides a cl_image_tag helper which generates a HTML img tag that links directly to the image on the Cloudinary server Cloudinary gem 提供了一个cl_image_tag帮助器,它生成一个 HTML img标签,直接链接到 Cloudinary 服务器上的图像

You should prefer to use this helper as opposed to ActiveStorage url helpers as this helper tag generates a direct link to the image and not a link to your server.您应该更喜欢使用此帮助程序而不是 ActiveStorage url 帮助程序,因为此帮助程序标记生成到图像的直接链接,而不是到您的服务器的链接。 You get the full benefits of using a CDN by hitting the image directly from Cloudinary.通过直接从 Cloudinary 访问图像,您可以获得使用 CDN 的全部好处。 To have https , just specify secure: true in the tag.要拥有https ,只需在标签中指定secure: true

Eg: If you have a User record that has_one_attached:file .例如:如果您有一条User记录has_one_attached:file You can use the cl_image_tag like this:您可以像这样使用cl_image_tag

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM