Custom NGINX configuration
Platform.sh DevRel
I’m trying to use a Wordpress plugin that converts existing image files into webp and then uses an nginx rule to try and get the webp version. If the webp version fails it reverts to default. Is it possible to do this with platform?
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_webp
$uri =404;
}
I’ve tried several variations using locations in app.yaml but the closest I get is this in the nginx.conf on pkatform
location "/wp/wp-content/uploads/" {
alias /app/web/wp/wp-content/uploads/;
location "/wp/wp-content/uploads/" {
try_files $uri =404;
expires -1s;
}
location ~ "(?<path>.+)\.(?<ext>jpe?g|png|gif|webp)$" {
location ~ "(?<path>.+)\.(?<ext>jpe?g|png|gif|webp)$" {
set $_rewrite_path "/wp/wp-content/uploads-webpc/uploads/$path.$ext";
try_files $uri @rewrite;
expires -1s;
}
}
}
Thanks Dan
0
Comments
Hi Dan, take a look at Rewrite Requests without Redirect documentation.
You should end up with something like this in your application yaml:
Hope this helps. Good luck!
I’ve tried for a fair few hours to get this working but can’t, I even tried just re-writing a single file I know exits in both formats but can’t get that to work either.
for example:
I’m using the Wordpress Bedrock template which may be causing some confusion. My whole locations config looks like this
Cheers Dan
Sorry, my original suggestion was not correct. The root of the problem that I suggested is that
passthrumust be a web URL path that your app is ready to handle—not a local file-path route.I suggest starting fresh from the original
.platform.app.yamlprovided by the WordPress Bedrock template and then following the steps below.Let’s first ensure that web requests for your
uploads-webpccan be answered. This location will be referenced by your/wp/wp-contentrule.Add this to your
web.locationssection:This is essentially the same setup as the
wp/wp-contentrule. The only difference is therootlocation andwebphas been added to therules.Now, let’s ensure
/wp/wp-content/uploadschecks for webp filesModify the existing
/wp/wp-content/uploadslocation to include a second rule. It passes request through the location we defined above—looking for the file name requested, ending with.webpFinally, if you are using the
webp-converter-for-mediaplugin to do this, ensure that it can create files as needed by adding this to yourmounts:I tested this setup using the
webp-converter-for-mediaviacomposer require wpackagist-plugin/webp-converter-for-media. I’m unfamiliar with this package, so I may have missed something. Hopefully, this gives you an idea of how to iterate through testing these route rewrites.Good luck!
Hi Tyler,
Thanks for the pointers, I re-configured the locations and mounts correctly as I had got them mixed up from when I first setup the site a while back.
I am using the
plugin/webp-converter-for-mdeiaplugin and can confirm that my webp images are being served on the alternative path they’re just not being re-written / redirected.For example:
“https://feature-fix-webp-gj4zkma-qn5amk3ilbjc2.uk-1.platformsh.site//app/uploads/2022/06/Product-Footer-2.jpg”
“https://feature-fix-webp-gj4zkma-qn5amk3ilbjc2.uk-1.platformsh.site//app/uploads-webpc/uploads/2022/06/Product-Footer-2.jpg.webp”
But on the landing page the images in the source are still the originals.
https://feature-fix-webp-gj4zkma-qn5amk3ilbjc2.uk-1.platformsh.site
My locations are setup as follows.
I think I’m close but I just can’t get the pattern working.
Thanks Dan
TL;DR: Jump to the “Now, let’s approach this logically.” summary at the end of this post
I’m familiarizing myself with WordPress Bedrock as we talk through this, so sorry for anything I miss along the way.
It looks like Bedrock doesn’t actually use
/wp/wp-content/uploads- it will always be referred to as/app/uploads. So we need to tell platform.sh how to hand requests to/app/uploadsURLs when they include .webp extensions. Once you find success there, you can repeat the process for/app/themesand/app/pluginsrequests if you choose to take advantage of those plugin features./app/uploads-webpcand/app/uploadswork out-of-the-box with this template. For example, if I uploadtest_file.jpgand request the URL /app/uploads/2022/11/test_image-150x150.jpg.webp, it loads without issue. If I generate the webpc version of the file, I can access that URL as well.That means we don’t need the special rule we added for
uploads-webpcto answer requests. Let’s delete this from the yaml:uploadsvia this route:/app/uploads/..../wp/wp-content/uploadsrule:Now, let’s approach this logically.
/app/uploadsreceives a.webpfile request?web/app/uploadsdirectory`web/app/uploads-webpc/uploadsdirectory/app/uploads-webpcreceives a request?web/app/uploads-webpc.webpfile and the file was not found, look for it inweb/app/uploads.Using your
.platform.app.yamlfile, the result is this. Note the change to theapp/uploadschanges and the cleanup of thewp/locations that we previously created.Edit
Just to be clear, this will look for the webp version first, and fall back to the original version only when the
.webpextension is included in the URL. If file.jpg is requested, it will not look for the file.jpg.webp version. If that is what is needed, it shouldn’t be too tricky to modify the existing logic to do so.I’m trying to get the same plugin working. I’ve followed the suggestions here, but this plugin is still returning the error:
It then refers me to this url.
The webp file type is listed in rules section per your example, but I still cannot seem to resolve this error. Any advice?
Please sign in to leave a comment.