Skip to content

Instantly share code, notes, and snippets.

@igrigorik
Last active December 21, 2015 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igrigorik/6372614 to your computer and use it in GitHub Desktop.
Save igrigorik/6372614 to your computer and use it in GitHub Desktop.
CH + DPR negotiation example for Nginx.
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
# - capture the DPR resolution from CH header
# - if no CH header is present, default to 1
map $http_ch $dpr {
~*dpr=(?<val>.*) $val;
default "1";
}
server {
listen *:8080;
# - check for appropriate DPR asset
# - if appropriate DPR is not available, fallback to original
location ~* /photos/(?<name>.*)\.(?<ext>.*)$ {
try_files /photos/$name-$dpr.$ext $uri =404;
}
location / {
root html;
}
}
}
$> curl -s http://localhost:8080/photos/awesome.jpg | wc -c
252680
$> curl -s -H"CH: dpr=1.5" http://localhost:8080/photos/awesome.jpg | wc -c
381135
$> curl -s -H"CH: dpr=2" http://localhost:8080/photos/awesome.jpg | wc -c
710175
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment