Need to find something?

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;Code language: PHP (php)

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 MediaCode language: PHP (php)

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.

Cloudflare Purge Everything

Last Updated: September 2, 2023

Al-Mamun Talukder

About Al-Mamun Talukder

WordPress Developer. Minimalist Designer. Tech Enthusiast. Loves Cars. Founder of Omnixima.

Connect with me: Upwork, GitHub, Facebook, Twitter

4 comments

  • Thank you.
    But it was how to disable the cache before purge I didn’t quite understand, I don’t know how to disable.

    • A

      On your Cloudflare Dashboard, go to Caching > Cache Rules. Add new rule. Field = URI, Operator = Equals, Value = /*. Expression preview should look like this (http.request.uri eq “/*”). Then select Bypass Cache on Cache status and save changes. You may also try activating development mode to see the issue resolves (if the bypass cache rule doesn’t work for you).

  • Jason Wells

    Hi Man thank you for this, I have gotten as far as to write the code, and checked out ok, restarting does indeed give me the “rewrites_cached” issue but i a cant seem to figure out what you mean by “go ahead and try disabling the Cloudflare cache and purge everything” Do you mean go to the cloudflare website and change some setting there? Or is there a setting i am purely missing thats right in front of me lol

    • A

      I have added a screenshot of the Cloudflare page that has the “Purge Everything” button. Hopefully it will resolve your issue. Thanks

Leave your comment