nginx virtual directory redirect to a static file

Francis Daly francis at
Thu Feb 4 09:53:34 UTC 2021

On Thu, Feb 04, 2021 at 03:07:27AM -0500, proj964 wrote:

Hi there,

> Perhaps this is something that can't be done with nginx, but I would like to
> have a url, 
> like http://localhost:nnn/abc return a file from q:/d1/qrst.txt.

I'm not certain, from the words here and in the Subject: line, but I
think you are asking for something like:

* request /abc/ returns the content of /tmp/thisfile.txt
* request /abc/anything returns the content of /tmp/thisfile.txt
(for any value of "anything")
* request /abcd does not return the content of /tmp/thisfile.txt
* request /abc either returns the content of /tmp/thisfile.txt, or maybe
returns a redirect to /abc/ (which then gets to item #1 again)

The first three of those can be done (assuming no overriding location)

  location ~ ^/abc/.* { alias /tmp/thisfile.txt; try_files "" =404; }

where the "=404" is just in case your file does not exist.

The fourth might be done with

  location = /abc { return 301 /abc/; }

You may also want to set default_type if what is there already does not
do what you want.

> I have tried many combinations of root, alias, rewrite, try_files in
> location.
> For everything I try, nginx inserts "abc" into the final absolute path

That should probably not happen with "alias". If you show the config,
the request, and the response, maybe it can be explained.

As it happens, I first thought that just

  location ~ ^/abc/.* { alias /tmp/thisfile.txt; }

would work, but the ngx_http_index_module handles requests ending in
/ before this config takes hold, so the extra "try_files" is used to
avoid that.

There may well be a better way.


Francis Daly        francis at

More information about the nginx mailing list