Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(imgproxy): enable nginx to serve image filters #32

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions nginx/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ ENV NGINX_PUBLIC ''
ENV NGINX_TEMPLATE application.conf
ENV XDEBUG_CONNECT_BACK_HOST '""'

RUN sed -i '1s/^/load_module \/etc\/nginx\/modules\/ngx_http_image_filter_module.so;\n\n/' /etc/nginx/nginx.conf

COPY etc/nginx/fastcgi_params /etc/nginx/fastcgi_params.template
COPY etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.template
COPY etc/nginx/available.d/*.conf /etc/nginx/available.d/
Expand Down
6 changes: 6 additions & 0 deletions nginx/etc/nginx/available.d/magento2.conf
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ location /static/ {
}

location /media/ {
location ~* ^/media/catalog/.* {
proxy_pass http://$image_resize_stream;
proxy_cache catalog_media;
proxy_cache_valid 200 24h;
}

try_files $uri $uri/ /get.php$is_args$args;

location ~ ^/media/theme_customization/.*\.xml {
Expand Down
80 changes: 80 additions & 0 deletions nginx/etc/nginx/conf.d/default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,83 @@ server {
include /etc/nginx/available.d/${NGINX_TEMPLATE};
include /etc/nginx/default.d/*.conf;
}

# create cache for resized images
proxy_cache_path /tmp/nginx-catalog-media-cache/
levels=1:2
keys_zone=catalog_media:10m
inactive=24h
max_size=500m;

# declare resize streams for legacy and new browsers
map $http_accept $image_resize_stream {
default image_resize_legacy;
~image/webp image_resize_webp;
}

upstream image_resize_webp {
server localhost:20001;
}

upstream image_resize_legacy {
server localhost:20002;
}

# server for modern browsers
server {
server_name localhost;
listen 20001;

root ${NGINX_ROOT}${NGINX_PUBLIC};
set $MAGE_ROOT ${NGINX_ROOT};

location ~* ^/(?<path>media/catalog/.*)$ {
set $ext "";

# when request is made for image.jpg,
# check if image.jpg.webp is available.
if (-f $MAGE_ROOT/pub/$path.webp) {
set $ext .webp;
}

alias $MAGE_ROOT/pub/$path$ext;

set $width "-";
set $height "-";
if ($arg_width != '') {
set $width $arg_width;
}
if ($arg_height != '') {
set $height $arg_height;
}

image_filter resize $width $height;
image_filter_interlace on;
image_filter_jpeg_quality 75;
image_filter_webp_quality 75;
}
}

# server for legacy browsers
server {
server_name localhost;
listen 20002;

root ${NGINX_ROOT}${NGINX_PUBLIC};

location / {
set $width "-";
set $height "-";
if ($arg_width != '') {
set $width $arg_width;
}
if ($arg_height != '') {
set $height $arg_height;
}

image_filter resize $width $height;
image_filter_interlace on;
image_filter_jpeg_quality 75;
image_filter_webp_quality 75;
}
}