visit
At the top level, we use a CDN (Content Delivery Network) - a geographically distributed network infrastructure that allows optimizing the delivery and distribution of content to end users on the Internet. In the initial stage, the user requests an image from the domain cdn1.yourhost.com
, and the request goes to the CDN. If this request is already stored in the cache, it is immediately served to the client; if not, the request goes to our servers.
The request that comes to our servers (MediaPRX) is also checked for the presence of a ready cache; if it exists, it is served immediately; if not, the request is passed to the next level (MediaResize) and the response is cached.
At the resizer, we transform the request by determining the required format and size, and send it to the locally deployed imgproxy, adding the original image URL to the request. Imgproxy, upon receiving the request, fetches the original image from the same server where the request came from.
All images served through media.yourhost.com
and cdn1.yourhost.com
support dynamic resizing by specifying the necessary parameters in the request URL. We use imgproxy as the resizer, and the nginx
in front of it converts the requested URL into a format understood by imgproxy.
~^/s3/(?<a>[^/]+/(.?/)?)(a|w)?(c|b|d|s|w|h|f)(?<size>[0-9]{2,4})(_q[0-9]{2})?/(?<b>[^/]+.(jpg|jpeg|png|gif|webp))$
/size1 x size2/
or /size1 / size2/
) - fit within a rectangle while maintaining proportions.(w|a)?(w|h)
(w|a)?(c|b|d|s|w|h|f)
:The first optional modifier:
w
- indicates that the requested image needs to be converted to webp
. If this modifier is absent, the image will be served without format conversion.
a
- on stg/prod
, there is already a modifier - avif
, but it is not recommended to use it, as the compression operation takes a long time online.
The second mandatory modifier:
c
- fit within a square while maintaining proportions without image extension.
b
- fit within a square while maintaining proportions, filling the empty areas with white color, and positioning the image at the bottom.
d
- fit within a square while maintaining proportions, overlaying a watermark with filling over the entire image.
s
- fit within a square while maintaining proportions, filling the empty areas with white color, and positioning the image at the center.
w
- resize by width, maintaining proportions.
h
- resize by height, maintaining proportions.
f
- resize and crop according to the specified "square" dimensions.
".*" is 95
, ".webp" is 80
, ".avif" is 50
.
After the resize modifiers, you can redefine the desired quality level (w|a)?(c|b|d|s|w|h|f){SIZE}_q({quality})
.
{quality}
- numbers from 01 to 99
.
Upon receiving a request from imgproxy, nginx
checks if the image is in its local cache (this is done to avoid fetching the original image from S3/MediaOrigin
multiple times when requesting different sizes of the same image). If the image is not in the cache, nginx
fetches it from S3/MediaOrigin
, caching the response.
The result obtained from imgproxy is then passed back through the chain to the user and cached at each level to speed up subsequent requests to the same URL.