I solved this problem as follows:
- Check if client declares "image / webp" in Accept header
- If WebP is supported, check if the local WebP file is on disk and serve it
- If the server is configured as a proxy server, add the header "WebP: true" and go to the backend
- Add "Vary: Accept" if the WebP service resource
in nginx:
location / { if ($http_accept ~* "webp") { set $webp "true"; }
In my case, I use thumbor software to convert images. https://github.com/globocom/thumbor
pip install thumbor
My conf:
upstream thumbor { server 127.0.0.1:9990; server 127.0.0.1:9991; server 127.0.0.1:9992; server 127.0.0.1:9993; server 127.0.0.1:9994; } location / { if ($http_accept ~* "webp") { set $webp "T"; } if ($uri ~* "(jpg|jpeg)$") { set $webp "${webp}T"; } proxy_cache_key $host$request_uri$webp; if ($webp = "TT") { rewrite ^(.*)$ "/unsafe/smart/filters:format(webp)/exemple.com$uri" break; proxy_pass http://thumbor; add_header Content-Disposition "inline; filename=image.webp"; } if ($webp != "TT") { proxy_pass http://exemple.com; } }
source share