Skip to content

Instantly share code, notes, and snippets.

@jnbdz
Created December 3, 2014 19:17
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 jnbdz/69ea50f8fa8a5a43af6e to your computer and use it in GitHub Desktop.
Save jnbdz/69ea50f8fa8a5a43af6e to your computer and use it in GitHub Desktop.
Detect image format webp support.
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#
# < regular Nginx configuration here >
#
# For a hands-on explanation of using Accept negotiation, see:
# http://www.igvita.com/2013/05/01/deploying-webp-via-accept-content-negotiation/
# if proxying to another backend and using nginx as cache
proxy_cache_path /tmp/cache levels=1:2 keys_zone=my-cache:8m max_size=1000m inactive=600m;
proxy_temp_path /tmp/cache/tmp;
server {
listen 8081;
server_name localhost;
# check Accept header for webp, check if .webp is on disk
if ($http_accept ~* "webp") { set $webp_accept "true"; }
if (-f $request_filename.webp) { set $webp_local "true"; }
location / {
# if WebP variant is available, serve Vary
if ($webp_local = "true") {
add_header Vary Accept;
}
# if WebP is supported by client, serve it
if ($webp_accept = "true") {
rewrite (.*) $1.webp break;
}
}
# if proxying to another backend and using nginx as cache
proxy_cache_key $scheme$proxy_host$request_uri$webp_local$webp_accept;
location /proxy {
# Pass WebP support header to backend
proxy_set_header WebP $webp;
proxy_pass http://127.0.0.1:8080;
proxy_cache my-cache;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment