11

I am trying to rewrite the request uri depending on the extension of the file, then extract only the file name from the uri and store it in another folder. The problem here is no predefined variable for file name and the available variables uri, request_uri and request_filename will give the full uri .

server{

        set $file_folder D:/nginx-1.0.15/imageAll/;  

        location ~*+.(gif|jpg)$ { 
            try_files $uri @imgstore;
        }

        location @imgstore { 
            proxy_pass $file_folder$request_filename;
            proxy_store on;
            proxy_temp_path /nginx-1.0.15/images/;
            proxy_store_access  user:rw  group:rw  all:r;
       }
}

the best I can do is getting the extension .jpg or .gif, that when I put $1 in place of $request_filename like this:

location @imgstore { 
    proxy_pass $file_folder$1;
}

So, I want to know :

  1. How to get the file name from the request?
  2. Is it the right way to store images from folder to another?
webtoe
  • 1,946
  • 11
  • 12
Johnta
  • 113
  • 1
  • 1
  • 4

1 Answers1

18

Do you want the filename from the original request, or from the current uri (after any internal redirection)? They're both possible using the map module:

# Gets the basename of the original request
map $request_uri $request_basename {
    ~/(?<captured_request_basename>[^/?]*)(?:\?|$) $captured_request_basename;
}

# Gets the basename of the current uri
map $uri $basename {
    ~/(?<captured_basename>[^/]*)$ $captured_basename;
}

Then just use $request_basename or $basename wherever you need them. Note that maps must be defined in the http{} context, making them siblings of server{}s.

kolbyjack
  • 7,854
  • 2
  • 34
  • 29