Configure nginx to statically cache specific URLs

I want to cache specific URLs in my application by directly looking at static files. However, statically cached files may not exist, so I only want to redirect the request if the files exist. I suppose I can use try_files somehow, but so far I have not been able to do this correctly.

Here is what I tried:

location ^/(.*)/(.*)/other/stuff/(.*)/this-is-static { try_files /static-cache/$1/$2/$3/this-is-static @app; } 

static-cache is an internal location configured in the same file. This does not seem to work as intended. What's wrong?

The closest I got is that it overwrites each request (and thus it does not work when the file is not cached):

 location ^/(.*)/(.*)/other/stuff/(.*)/this-is-static { rewrite ^ /static-cache/$1/$2/$3/this-is-static; } 
+4
source share
1 answer

You tried to use ~ ?, I also used [^/] to avoid the greedy regular expression

 location ~ /([^/]*)/([^/]*)/other/stuff/([^/]*)/this-is-static { try_files /static-cache/$1/$2/$3/this-is-static @app; } location @app { #handle non cached files } 
0
source

Source: https://habr.com/ru/post/1482950/


All Articles