How to extract Only the file name from the request uri

Solution 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.