我在下面设置nginx服务器配置文件并且工作正常,但我想知道是否有一种方法可以优化它,所以没有那么多的代理设置重复。
我想限制可以对特定预设尺寸进行的调整,而不是例如允许/resized/200x100/image.jpg
。
我正在通过Ghost提供这些图片,因此我只能添加或附加网址。
upstream backend {
server domain.name;
}
proxy_cache_path /var/www/cache/resized levels=1:2 keys_zone=resizedimages:10m max_size=1G;
server {
listen 80;
root /var/www/domain/source;
server_name domain.name;
image_filter_buffer 10M;
image_filter_jpeg_quality 85;
proxy_store_access user:rw group:rw all:rw;
location / {
proxy_pass http://127.0.0.1:2368;
proxy_redirect off;
}
location ~ /resize/xs/(.*) {
proxy_pass http://backend/$1;
proxy_cache resizedimages;
proxy_cache_valid 200 1d;
proxy_cache_valid any 1m;
proxy_cache_use_stale error timeout invalid_header updating;
image_filter resize 320 -;
}
location ~ /resize/xs@2x/(.*) {
proxy_pass http://backend/$1;
proxy_cache resizedimages;
proxy_cache_valid 200 1d;
proxy_cache_valid any 1m;
proxy_cache_use_stale error timeout invalid_header updating;
image_filter resize 480 -;
image_filter_jpeg_quality 65;
}
location ~ /resize/sm/(.*) {
proxy_pass http://backend/$1;
proxy_cache resizedimages;
proxy_cache_valid 200 1d;
proxy_cache_valid any 1m;
proxy_cache_use_stale error timeout invalid_header updating;
image_filter resize 480 -;
}
location ~ /resize/sm@2x/(.*) {
proxy_pass http://backend/$1;
proxy_cache resizedimages;
proxy_cache_valid 200 1d;
proxy_cache_valid any 1m;
proxy_cache_use_stale error timeout invalid_header updating;
image_filter resize 720 -;
image_filter_jpeg_quality 65;
}
# md, m@2x, etc.
location ~ /resized/(sq/xs|sq/md|xs|xs@2x|sm|sm@2x|md|md@2x|lg|lg@2x|xl|xl@2x)/(.*) {
try_files /resize$1/$2 @img;
}
location @img {
proxy_pass http://backend/resize/$1/$2;
proxy_store /var/www/domain/cache/$1/$2;
}
}