Try_files in 2 different locations

Solution 1:

The try_files directive accepts file parameters which look like URIs. The pathname of the file is constructed by concatenating the value of the root directive with the value of the file parameter.

With the two pathnames you are using (/var/www/site.com/html/images/img.jpg and /var/www/dev.site.com/html/images/img.jpg), the common part is /var/www.

The variable $uri will be set to /images/img.jpg, so the file parameter just needs to provide the missing pieces.

For example:

location ^~ /images/ {
    root /var/www;
    try_files /site.com/html$uri /dev.site.com/html$uri =404;
}

See this document for details.