Hi,

Today I'm in need of some help.

I have the following scenario.

One of my sites is quite old (10 years or more), and has more and less 50GB of uploads. In order to save disk space, I move the old uploads (2020 and before) to a Backblaze B2 account, and redirect the traffic to these uploads with:

rewrite /wp-content/uploads/2020/(.*) https://b2.full.url/2020/$1 permanent;

It works well enough, but it's not easy to maintain, and if I happen to upload something new in an older post (quite common), I have to remember to upload it to B2. And the recent months also become a nightmare, because every month I have to add several new rewrite rules before I can rewrite the whole year.

How could I rewrite (or ideally proxy_pass) only the request to non existent files in local filesystem?

I tried the code below, with no success:

location ^/wp-content/uploads/(.*) {
  try_files $uri @b2proxy;
}

location @b2proxy {
  proxy_pass https://b2.full.url/;
}

Thanks!

Haven't had time to test, but I think you need:-
location ^~ /wp-content/uploads/ {

    marty, thanks for the suggestion, but it did not work.

    Actually, I can't do what I want without messing with PHP configuration rules. There is a try_files block in /etc/nginx/common/wpcommon-php*.conf which tries the AVIF and WEBP variations before the actual image file, and finally gives up with a 404.

    Nginx will never reach my location block.

    I have to think of something more elegant, but I'm not sure it will be possible without messing (excessively) deep in WO default conf files. 😰

    Solved!

    It's just:

    error_page 404 = @notfound;
    location @notfound {
    	rewrite ^/wp-content/uploads/(.*) https://b2.root.url/$1 permanent;
    }

    That's it.

    Ah yeah, darn. The ^~ will take priority over regex matches but not exact matches, so location /wp-content/uploads wins.
    A regex should override that though, so location ~* ^/wp-content/uploads/.* {}

    Hosted by VirtuBox