Skip to content

Instantly share code, notes, and snippets.

@igrigorik
Created August 29, 2013 00:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save igrigorik/6372960 to your computer and use it in GitHub Desktop.
Save igrigorik/6372960 to your computer and use it in GitHub Desktop.
CH + select best candidate based on defined buckets.
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
rewrite_log on;
error_log logs/errog_log debug;
# - capture the DPR resolution from CH header
# - if no CH header is present, default to 1.0
map $http_ch $dpr {
~*dpr=(?P<val>.*) $val;
default "1.0";
}
server {
listen *:8080;
# there is probably a cleaner way (TODO).. but for now, it works:
# check pre-defined buckets to provide "best match" for any given resolution
if ($dpr = 3.0) { set $c0 "3.0"; set $c1 "2.5"; set $c2 "2.0"; set $c3 "1.5"; set $c4 "1.0"; }
if ($dpr = 2.5) { set $c0 "2.5"; set $c1 "2.0"; set $c2 "1.5"; set $c3 "1.0"; }
if ($dpr = 2.0) { set $c0 "2.0"; set $c1 "1.5"; set $c2 "1.0"; }
if ($dpr = 1.5) { set $c0 "1.5"; set $c1 "1.0"; }
if ($dpr = 1.0) { set $c0 "1.0"; }
# - check for appropriate DPR asset
# - if appropriate DPR is not available, fallback to original
location ~* /photos/(?<name>.*)\.(?<ext>.*)$ {
set $dir "/photos/";
try_files $dir$name-$c0.$ext $dir$name-$c1.$ext $dir$name-$c2.$ext
$dir$name-$c3.$ext $dir$name-$c4.$ext $uri =500;
}
location / {
root html;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment