Last active
December 21, 2015 21:59
-
-
Save igrigorik/6372614 to your computer and use it in GitHub Desktop.
CH + DPR negotiation example for Nginx.
This file contains 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
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; | |
} | |
} | |
} |
This file contains 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
$> 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