Skip to content

1gor/varnish-imgproxy

Repository files navigation

Imgproxy is a “fast and secure standalone server for resizing and converting remote images”. It should normally be deployed behind a caching proxy or a CDN, and docker-compose.yml provides an example of such a deployment behind a Varnish cache server.

Here is a simple workflow of using signed urls with imgproxy.

For this demo you need to create an .env file containing server key and salt. These have to be random hex-encoded strings. You can easily generate such strings in ruby:

require 'securerandom'
SecureRandom.hex(13)
# => 4a4c608197650d45d3b7b15c4f

Now you can run docker-compose up and access your server at: http://0.0.0.0:8080. Encoded urls can be generated in ruby using the same key and salt with the help of imgproxy gem. For this example we’ll resize codercat GitHub image and convert it to webp format.

require 'imgproxy'
require 'dotenv'
Dotenv.load(".env")

Imgproxy.configure do |conf|
  conf.key = ENV['IMGPROXY_KEY']
  conf.salt = ENV['IMGPROXY_SALT']
end
b = Imgproxy::Builder.new(
  width: 200,
  height: 200,
  resizing_type: :auto,
  base64_encode_url: true,
  format: 'jpg'
)
# url = "https://octodex.github.com/images/codercat.jpg"
url = "https://102922.selcdn.ru/nomenclature_images_test/1841598/d378c003-304e-490c-b861-3eb7c387d072.png"
b.url_for(url)

You can test resulting url on your local server: http://0.0.0.0:8080/6CwHaj-VyyloNQfcjjXnQURrh5_Fcg6bfmOHrd_vQIE/rs:auto:100:100/aHR0cHM6Ly9vY3Rv/ZGV4LmdpdGh1Yi5j/b20vaW1hZ2VzL2Nv/ZGVyY2F0LmpwZw.webp.

About

Varnish in front of Imageproxy demo with signed urls

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published