create new nginx template just copy the one you want to use for wp, and for other sites just copy the template for that site so they don’t get overwritten in updates. add:
I’ve made a duplicate template from wordpress tpl and stpl, with a new name, using command
cp wordpress.tpl wordpresswebp.tpl and cp wordpress.stpl wordpresswebp.stpl
which can be found in /usr/local/hestia/data/templates/web/nginx/php-fpm
Edit newly made templates, by inserting their code under the line include %home%/%user%/conf/web/%domain%/nginx.hsts.conf*;
# BEGIN WebP Converter
set $ext_avif ".avif";
if ($http_accept !~* "image/avif") {
set $ext_avif "";
}
set $ext_webp ".webp";
if ($http_accept !~* "image/webp") {
set $ext_webp "";
}
location ~ /wp-content/(?<path>.+)\.(?<ext>jpe?g|png|gif)$ {
add_header Vary Accept;
add_header Cache-Control "private" always;
expires 365d;
try_files
/wp-content/uploads-webpc/$path.$ext$ext_avif
/wp-content/uploads-webpc/$path.$ext$ext_webp
$uri =404;
}
# END WebP Converter
The last step: Add the code below into the config file of your domain at /home/yourusername/conf/web/yourdomain.com/nginx.ssl.conf
location ~* ^(/wp-content/.+)\.(png|jpe?g)$ {
set $base $1;
set $webp_uri $base$webp_suffix;
set $webp_old_uri $base.$2$webp_suffix;
set $root "<<FULL PATH OF WP-CONTENT PARENT>>";
root $root;
add_header Vary Accept;
if ( !-f $root$webp_uri ) {
add_header X_WebP_SP_Miss $root$webp_uri;
}
try_files $webp_uri $webp_old_uri $uri =404;
}
Remember change the path into code above <<FULL PATH OF WP-CONTENT PARENT>> to your website root path, ex: /home/yourusername/web/yourdomain.com/public_html/
After everything is configured, your WebP or AVIF files will show up with the same URL and extension. Your browser will display image.jpg, but the image will actually be a WebP/AVIF file.
I noticed the speed improvement even though the browser still shows me the PNG or JPG files. But in reality, is actually using the WebP file.