PSA: Nginx config without “map”
-
If you’re using nginx but can’t use the “map” directive from the FAQ (like this person couldn’t), here’s some alternative code:
if ( $http_accept ~ "image/webp" ) {
set $webp .webp ;
}
location ~* (.*)\.(?:png|jpe?g|gif)$ {
if ( -f $document_root$1.webp ) {
set $webp serve$webp ;
}
if ( $webp = "serve.webp" ) {
return 301 '$1.webp';
}
}I had also tried this code, which is more efficient, but unfortunately it didn’t work for me:
if ( $http_accept ~ "image/webp" ) {
set $webp '.webp' ;
set $type 'image/webp';
}
location ~* \.(png|jpe?g|gif)$ {
add_header Vary Accept;
add_header X-Type "static/known";
add_header Cache-Control "public, max-age=2592000";
add_header Vary "Accept-Encoding";
add_header Access-Control-Allow-Origin *;
add_header content-type $type;
default_type '$type';
try_files $uri$webp $uri =404;
}
- You must be logged in to reply to this topic.