Skip to content

Instantly share code, notes, and snippets.

@sergejmueller
Created November 27, 2013 09:00
Show Gist options
  • Save sergejmueller/7672727 to your computer and use it in GitHub Desktop.
Save sergejmueller/7672727 to your computer and use it in GitHub Desktop.
Nginx: Detecting WebP support with Header Vary Accept. Filename format: image.jpeg / image.webp
...
image/webp webp;
...
location ~ (.+)\.(png|jpe?g)$ {
if ( $http_accept ~* webp ) {
set $webp "A";
}
if ( $request_filename ~ (.+)\.(png|jpe?g)$ ) {
set $file_without_ext $1;
}
if ( -f $file_without_ext.webp ) {
set $webp "${webp}E";
}
if ( $webp = AE ) {
add_header Vary Accept;
rewrite (.+)\.(png|jpe?g)$ $1.webp break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment