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

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

S.A.N
Hi,

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

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@localhost ~]$ curl http://mydomain.example.com/data/foo

<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>


[user@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>


This is my vhost configuration:

server {
     listen 80;
     server_name mydomain.example.com;
     access_log /var/log/nginx/mydomain.example.com-access.log;
     error_log /var/log/nginx/mydomain.example.com-error.log error;

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

     location /data/bit {
             alias /data/bit;
             autoindex on;
}


Thanks.

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,273267,273267#msg-273267

_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

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

Francis Daly
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@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@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        [hidden email]
_______________________________________________
nginx mailing list
[hidden email]
http://mailman.nginx.org/mailman/listinfo/nginx
Loading...