# seraphinite-accelerator - Automatic redirection to Avif and WebP versions if they exist
location ~ .*\.(jpe|jpg|jpeg|png|gif|bmp|webp|avif)$
{
	types
	{
		image/jpeg	jpe;
		image/jpeg	jpg;
		image/jpeg	jpeg;
		image/png	png;
		image/gif	gif;
		image/bmp	bmp;
	}

	set $any_redir "";

	# webp
	types { image/webp webp; }
	set $webp_redir "";
	if ($http_accept ~* "image\/webp") { set $webp_redir "${webp_redir}A"; }
	if (-f $request_filename.webp) { set $webp_redir "${webp_redir}F"; set $any_redir "R"; }
	if ($webp_redir = "AF") { add_header Vary Accept; rewrite (.*) $1.webp break; }

	if ($any_redir = "") { add_header Vary Accept; }
}
