Why 301 permanent redirect with appended slash?

J. Lewis Muir jlmuir at imca-cat.org
Tue Jul 30 22:12:01 UTC 2019


Hello, all!

I have a minimal nginx.conf with one server block that sets the root
directory and one location with a prefix string of "/foo/", and for a
request of "/foo", it returns a 301 permanent redirect to "/foo/".  Why?
I expected it to return 404 or similar.  I also tried a prefix string of
"/foo", but that also results in the same 301.

Here's the server block (entire nginx.conf at end of message):

----
server {
    listen      127.0.0.1:80;
    listen      [::1]:80;
    server_name localhost "" 127.0.0.1 [::1];
    root        /srv/www/localhost;

    location /foo/ {
    }
}
----

And here's the curl invocation:

----
$ curl -I 'http://localhost/foo'
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.2
Date: Tue, 30 Jul 2019 21:54:44 GMT
Content-Type: text/html
Content-Length: 185
Location: http://localhost/foo/
Connection: keep-alive

----

I've read in

  https://nginx.org/en/docs/http/ngx_http_core_module.html#location

where it says

  If a location is defined by a prefix string that ends with the
  slash character, and requests are processed by one of proxy_pass,
  fastcgi_pass, uwsgi_pass, scgi_pass, memcached_pass, or grpc_pass,
  then the special processing is performed. In response to a request
  with URI equal to this string, but without the trailing slash, a
  permanent redirect with the code 301 will be returned to the requested
  URI with the slash appended.

But in my case, I don't believe the request is being processed by any of
those *_pass directives.

Thank you!

Lewis

---- Complete nginx.conf ----
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';

    access_log /var/log/nginx/access.log main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include      /etc/nginx/mime.types;
    default_type application/octet-stream;

    server {
        listen      127.0.0.1:80;
        listen      [::1]:80;
        server_name localhost "" 127.0.0.1 [::1];
        root        /srv/www/localhost;

        location /foo/ {
        }
    }
}
----


More information about the nginx mailing list