Nginx Configuration for the “Converter for Media Plugin” on GridPane Server

Image optimization is one of the most important subject when it comes to website development. I recently came across a very nice plugin called “Converter for Media”, that you can use to convert your uploaded images to WebP for free. It also has option for a better format Avif, only if you get the premium subscription.

Everything was working great on my Cloudways hosted sites. All I had to do is enable WebP Redirection on the application settings page.

Things began more complicated when I was trying GridPane for a new server. I was getting error “rewrites_not_executed”. The plugin author has done a great job of properly adding necessary instructions to debug the errors.

Looking at the plugin page at WordPress repository, I found the code need to be added to fix the issue. However, on GridPane, it is recommended to not edit the .conf files directly.

gridpane-message

Instead, we need to use a location specific include.

We can use

include /etc/nginx/extra.d/*main-context.conf;

So, if we create a file like {%NAME%}main-context.conf inside /etc/nginx/extra.d/ folder, we can use our code in that file.

To create the file, type the following command. We are naming our file “webpc-main-context.conf”

nano /etc/nginx/extra.d/webpc-main-context.conf

Once you are in the editor, copy the code below and paste it.

# BEGIN Converter for Media
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|webp)$ {
    add_header Vary Accept;
    expires 365d;
    try_files
        /wp-content/uploads-webpc/$path.$ext$ext_avif
        /wp-content/uploads-webpc/$path.$ext$ext_webp
        $uri =404;
}
# END Converter for Media

Now press CTRL + O and then press Return/Enter to save the file. After that press CTRL + X to exit.

Next thing we need to do is to verify we did not make any errors before we do a restart.

Type the following command and see if there is any errors

nginx -t

If everything works ok, type the next command to restart nginx

gp ngx -reload

That’s it. Go ahead and reload the “Converter for Media” plugins page to see if the error is still there. You should use the “via .htaccess” mode, not the one called “Bypassing Nginx”.

Error Code: rewrites_cached

If you see this error, and you use Cloudflare, go ahead and try disabling the Cloudflare cache and purge everything. That will fix the rewrites_cached issue.

Last Updated: March 29, 2023

Al-Mamun Talukder

About Al-Mamun Talukder

WordPress Developer. Minimalist Designer. Tech Enthusiast. Music & Movie Freak. Loves Cars. Founder of WolfDevs.

Connect with me: Upwork, GitHub, Facebook, Twitter

Leave a Reply

Your email address will not be published. Required fields are marked *