How does nginx determine whether a file has different access paths according to its existence?

because the disk space is full and cannot be expanded directly, a disk has been added.
now intends to use nginx to determine whether the file exists, otherwise find another path.
can it be solved by using nginx, and how to configure the configuration file?
/ data1/upload/2018/08/07/1.jpg
/ data2/upload/2018/08/07/1.jpg

Apr.05,2021

location /upload/ {
    root /data1/upload/;
    try_files $uri $uri/ @data2;
}
location @data2 {
    root /data2/upload/;
    try_files $uri $uri/ =404;
}

// ...
root /data1;
location / {
    if (-e $request_filename) {
        root /data2;
        break;
    }
}
// ...

refer to

Menu