Last active
May 17, 2024 15:50
-
-
Save weirongxu/b55390a404fa34f0d14b68b0dfa13c53 to your computer and use it in GitHub Desktop.
nginx image filter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
server { | |
# Internal image resizing server. | |
server_name localhost; | |
listen 8888; | |
location /resize { | |
alias /var/www/app/current/public/imgs; | |
image_filter resize $arg_width $arg_height; | |
image_filter_jpeg_quality 75; | |
allow 127.0.0.0/8; | |
deny all; | |
} | |
} | |
server { | |
listen 80; | |
root /var/www/app/current/public; | |
location ^~ /imgs/ { | |
gzip_static on; | |
expires max; | |
add_header Cache-Control public; | |
if ($arg_width) { | |
set $resize width; | |
} | |
if ($arg_height) { | |
set $resize "${resize}height"; | |
} | |
if ($uri ~* "^/imgs/(.*)$" ) { | |
set $basename $1; | |
} | |
if ($resize = widthheight) { | |
rewrite .* /imgs-resize/$arg_width/$arg_height/$basename last; | |
} | |
if ($resize = width) { | |
rewrite .* /imgs-resize/$arg_width/-/$basename last; | |
} | |
if ($resize = height) { | |
rewrite .* /imgs-resize/-/$arg_height/$basename last; | |
} | |
} | |
location ~* /imgs-resize/(?<width>.+)/(?<height>.+)/(?<image_name>.+) { | |
set $image_root /var/www/app/current/public/imgs; | |
set $image_path "/resize/${width}/${height}/${image_name}"; | |
alias $image_root/resize; | |
if (!-f $image_root$image_path) { | |
proxy_pass http://127.0.0.1:8888/resize/$image_name?width=$width&height=$height; | |
break; | |
} | |
proxy_store $image_root$image_path; | |
proxy_store_access user:rw group:rw all:r; | |
proxy_temp_path /tmp/images; | |
proxy_set_header Host $host; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment