Score:2

NGINX Redirect to Cached File in Subfolder

ke flag

I have a program that creates optimized versions of pictures that are uploaded to the /images/ folder on the web server. It traverses subfolders and in each one, creates a .optimized folder that holds the optimized version, if it is at least a certain size smaller than the original. My goal is to check if such an optimized version exists, serve it if it does, and serve the original otherwise (in some sense like how gzip_static serves a .gz version of a file if it exists).

I'm running NGINX as a proxy in front of Apache, so while I'm accustomed to handling issues like this using htaccess, I am trying to do it natively in NGINX to avoid the server having to hand the request over to Apache. In .htaccess, I could do something like this:

RewriteCond %{REQUEST_FILENAME} ^(/images/(?:.*/)?)(.*?)$ [OR]
RewriteCond $1.optimized/$2 -f
RewriteRule .* $1/.optimized/$2 [L]

Is there a good way to handle this directly in NGINX? Most similar use cases I've found kept all the cached/optimized files in a single cached folder, as opposed to the structure I'm describing.

djdomi avatar
za flag
`if ($request_filename ~ "^/(.*/)+(.*?)$"){ set $rule_0 1; } if (-f /${fref_0_0}/.optimized/${fref_0_1}){ set $rule_0 1; } if ($rule_0 = "1"){ rewrite /.* /$1/.optimized/$2 last; }` can you test this, if I read your request correctly it may work, else we have to use try files
Richard Smith avatar
jp flag
The files are below the `/images/` folder. Does the URI begin with `/images/` also?
ke flag
@djdomi Thank you! I'll have access to my server in just a bit and will give that a try.
ke flag
@RichardSmith Yes. I erroneously had left that out of the htaccess example. I corrected that now. Thanks!
Score:3
jp flag

You can use a regular expression location to extract the first and second parts of the URI. Use a try_files statement to search the file system for each file in order.

For example:

location ~ ^(/images/.*?)([^/]+)$ {
    try_files $1.optimized/$2 $1$2 =404;
}
us flag
I think the order should be `try_files $1.optimized/$2 $1$2 =404`, so that the optimized version is used if it exists.
Richard Smith avatar
jp flag
Oops. Fixed. Thanks @TeroKilkanen
ke flag
Thank you so much! There was a bug in my regex (it assumed there would be at least one subdirectory involved, which can't be assumed), although even when I switch to `^(/images/(?:.*/)?)(.*)$` it doesn't seem to be "activating." I'm trying to figure out why....
ke flag
Oh! I realized that my proxy_pass was getting in the way of serving static content... I'll have to work on that, but that at least explains why it didn't apply. Thanks again!
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.