我有以下配置,用于调整图像image_filter_module模块,其大小为c64x64
,c128x128
,oringinal
,如下所示:
# Resize image on a fly (crop 64x64)
location ~ ^/image/c64x64/(.*):(.*)\.(?:jpg)$ {
alias /var/www/upload/$1/$1:$2.jpg;
image_filter crop 64 64;
image_filter_jpeg_quality 85;
image_filter_buffer 3M;
}
# Resize image on a fly (crop 128x128)
location ~ ^/image/c128x128/(.*):(.*)\.(?:jpg)$ {
alias /var/www/upload/$1/$1:$2.jpg;
image_filter crop 128 128;
image_filter_jpeg_quality 85;
image_filter_buffer 3M;
}
# Resize image on a fly (crop 192x192)
location ~ ^/image/c192x192/(.*):(.*)\.(?:jpg)$ {
alias /var/www/upload/$1/$1:$2.jpg;
image_filter crop 192 192;
image_filter_jpeg_quality 85;
image_filter_buffer 3M;
}
# Resize image on a fly (orig)
location ~ ^/image/original/(.*):(.*)\.(?:jpg)$ {
alias /var/www/upload/$1/$1:$2.jpg;
image_filter_jpeg_quality 85;
image_filter_interlace on;
image_filter_buffer 3M;
}
请求的图片是以下http://example.org/image/c128×128/10:54ea44.jpg
这看起来很难看,因为有很多重复的问题。我怎么能压缩mynginx配置?
答案 0 :(得分:1)
如果您不需要验证收到的尺寸,则可以执行以下操作:
# Move this line inside the location if your image root folder
# differs from your main document root
root /var/www;
location ~ ^/image/c(?<width>\d+)(×|x)(?<height>\d+)/(?<name>(?<dir>.+?):.+\.jpg)$ {
image_filter crop $width $height;
image_filter_jpeg_quality 85;
image_filter_buffer 5M;
rewrite ^.*$ /upload/$dir/$name break;
}
如果您确实想验证参数,那么配置必须变得有些复杂:
map $request_uri $incorrect_sizes {
default 1;
~/image/c64(×|x)64/.*$ "";
~/image/c128(×|x)128/.*$ "";
~/image/c192(×|x)192/.*$ "";
# Define here as many sets of sizes as your like
}
server {
...
# Move this line inside the location if your image root folder
# differs from your main document root
root /var/www;
location ~ ^/image/c(?<w>\d+)(×|x)(?<h>\d+)/(?<name>(?<dir>.+?):.+\.jpg)$ {
image_filter crop $width $height;
image_filter_jpeg_quality 85;
image_filter_buffer 3M;
if ($incorrect_sizes) {
set $w 128; # default width
set $h 128; # default height
}
rewrite ^.*$ /upload/$dir/$name break;
}
...
}