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 .


        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?

1 Answer 1


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.

  • I Want the file name from the current uri ,I don't Think map module could give that but I'll try it,
    – Johnta
    Commented Apr 20, 2012 at 18:44
  • I tested it locally before posting, and that second map works (as long as you're using 0.9.6+)
    – kolbyjack
    Commented Apr 20, 2012 at 19:25
  • Thank you Kolbyjack, great trick it did the job, I got the file name and the extension.
    – Johnta
    Commented Apr 21, 2012 at 14:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .