Mobile app version of vmapp.org
Login or Join
Si4351233

: Nginx uses the try_files directive to 'see if something exists and fallback to another option' instead of using if statements and rewrites. Within your server block, create a location block

@Si4351233

Nginx uses the try_files directive to 'see if something exists and fallback to another option' instead of using if statements and rewrites.

Within your server block, create a location block that will match your files (e.g. files ending in jpg, gif, png - add whatever extensions you want). Within that block, you will add your try_files directive. Nginx will try the paths in order - so, you start with the direct path to the image, and then list the path to your php file (but that needs to be proxied, so use a named location instead). Perhaps something like the following:

location ~ .(gif|jpg|png) {
try_files $uri @img_proxy ;
}


The above essentially amounts to the following:
For any files ending in .gif, .jpg, or .png first try the path ($uri), and if it doesn't exist goto @img_proxy .

Now, you need to create a second location @img_proxy that nginx will use if it can't find the actual file. In this you will specify your proxy settings, will also have a rewrite to change the path, and finally will pass the URI to the backend server.

Something like the following (the regex is NOT what you are looking for - it will pass the entire path, not just the file name - you can modify it to suit your needs):

location @img_proxy {
rewrite ^(.*)$ /image.php?id=;
proxy_pass 127.0.0.1:8080; }


[edited to fix some missing semicolons]

10% popularity Vote Up Vote Down


Login to follow query

More posts by @Si4351233

0 Comments

Sorted by latest first Latest Oldest Best

Back to top | Use Dark Theme