Useful Snippets

Welcome!


This blog is used to collect useful snippets related to Linux, PHP, MySQL and more. Feel free to post comments with improvements or questions!

Are your smart devices spying on you? Make better purchasing choices and find products that respect your privacy at Unwanted.cloud

RSS Latest posts from my personal blog


Subscribe to RSS feed


Nginx configuration for the WebP Converter for Media WordPress plugin

Stanislav KhromovStanislav Khromov

To make Nginx work with the WebP Converter for Media you can use the configuration below. It checks if the the browser Accept header allows WebP images and serves the WebP image if that is the case. If the browser does not support it, the original jpg, png or gif is served.

You will need to place the configuration in the server block of your existing configuration and then configure the plugin to use the “via .htaccess (recommended)” method. Even though we don’t use Apache our method works the same as the plugins .htaccess method.

server {
    // ... 

    # Add support for "WebP Converter for Media" WordPress plugin
    # https://wordpress.org/plugins/webp-converter-for-media/
    location ~ ^/wp-content/(?<path>.+)\.(?<ext>jpe?g|png|gif)$ {
        if ($http_accept !~* "image/webp") {
            break;
        }

        expires 180d;
        add_header Vary Accept;
        try_files /wp-content/uploads-webpc/$path.$ext.webp $uri =404;
    }
    // ...
}

If you want to see a more complete example, please see here.

Full-stack impostor syndrome sufferer & Software Engineer at Schibsted Media Group

Comments 2
  • Robin
    Posted on

    Robin Robin

    Reply Author

    Thanks for this!

    one question:

    i get a ‘ server configuration error ‘ that warns me we are bypassing apache, but this is the goal right? i should use the recommended method .htaccess and ignore this message right? the server snippet does its work regardless of the error message?