Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?
share|improve this question
up vote 11 down vote accepted

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.

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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.