curl 301 moved permanently if I don't use slash at the end of the url

Francis Daly francis at daoine.org
Sat Apr 1 07:32:58 UTC 2017


On Tue, Mar 28, 2017 at 12:56:13PM -0400, c4rl wrote:

Hi there,

> I need to list the content of some directories with curl without to use a
> '/' at the end of the url.

There's a relative-url reason why that is usually not a good idea,
which is probably why the defaults for many web servers' "auto-index"
feature do not do it, and instead issue the http 301.

It is reasonable and possible to do the directory listing the way you
want, but it is likely to include some extra coding (rather than just
configuration) on your part.

> If I do not use the slash then I receive the message below, otherwise the
> content is showed. I tried many rewrite rules without success.
> 
> [user at localhost ~]$ curl http://mydomain.example.com/data/foo

The important part of that response is in the http headers, that you
can see by (for example) adding "-i" after "curl". It will show the 301
response code, and the Location: header that includes the trailing /.

> [user at localhost ~]$ curl http://mydomain.example.com/data/foo/
> 
> <html>
> <head><title>Index of /data/foo/</title></head>
> <body bgcolor="white">
> <h1>Index of /data/foo/</h1><hr><pre><a href="../">../</a>
> <a href="57581/">57581/</a>                                            
> 12-Jul-2016 01:56                   -
> <a href="57582/">57582/</a>                                            
> 13-Jul-2016 01:55                   -
> <a href="57583/">57583/</a>                                            
> 14-Jul-2016 00:34                   -
> </pre><hr></body>
> </html>

You will have to write something that happens before nginx's in-built
default for a request with no trailing / that corresponds to a directory
name on the filesystem.

If you want the response to be html usable by a client, then the output
will not be identical to the above; instead the entries will have to be
of the form

 <a href="foo/57583/">57583/</a>
 14-Jul-2016 00:34                   -

(and you may or may not want one or both of the trailing / characters
there.)

> This is my vhost configuration:

>      location /data/foo {
>              alias /data/foo;
>              autoindex on;
>      }

I suspect that you would need something like

  location = /data/foo {
    my_special_autoindex on;
  }

where you must write the code behind "my_special_autoindex" yourself.

Or, more likely, use something like "try_files" (possibly in a chain)
to handle a generic url; if it does not end in / and does correspond
to a directory, then invoke your extra code -- which might be handled
by a fastcgi script you write, or by something you write in one of the
embedded languages in nginx.

It is possible that someone has already wanted the same thing as you,
and written a module to do it.

I'm not aware of that module.

Good luck with it,

	f
-- 
Francis Daly        francis at daoine.org


More information about the nginx mailing list