Welcome! Log In Create A New Profile


Re: nginx virtual directory redirect to a static file

Francis Daly
February 04, 2021 04:54AM
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@daoine.org
nginx mailing list
Subject Author Posted

nginx virtual directory redirect to a static file

proj964 February 04, 2021 03:07AM

Re: nginx virtual directory redirect to a static file

Francis Daly February 04, 2021 04:54AM

Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 82
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready