10

I have a location

location /pass/ {
    proxy_pass http://localhost:12345/;
}

So it is supposed to proxify urls http://example.com/pass/whatever to http://localhost:12345/whatever

The thing I don't like about it is that nginx is automatically adding slash to

http://example.com/pass

and makes it http://example.com/pass/ via 301 redirect

How can I simply avoid this behavior. I want /pass without trailing slash to go to another location. Thanks in advance!

igor
  • 163
  • 2
  • 2
  • 9
  • I solved it with regex (`/pass/(.*)`...`/$1$is_args$args`), other solutions would be appreciated – igor Dec 19 '13 at 10:58
  • Just in case it helps others finding themselves here questioning 301 redirects coming from their nginx-proxied webapps, make sure it's not the webapp doing it. That was the cause in my case, not nginx. – Jacob Wan Nov 27 '14 at 18:00
  • 3
    Possible duplicate of [How to stop nginx 301 auto redirect when trailing slash is not in URI?](http://serverfault.com/questions/759762/how-to-stop-nginx-301-auto-redirect-when-trailing-slash-is-not-in-uri) – igor Dec 08 '16 at 02:19

2 Answers2

5

There's a special case where a proxy_pass with a location ending in / would result in an automatic implicit 301 redirect without going to the backend; you have to create an explicit location without the trailing slash to avoid this:

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. If this is not desired, an exact match of the URI and location could be defined like this:

location /user/ {
    proxy_pass http://user.example.com;
}

location = /user {
    proxy_pass http://login.example.com;
}

E.g., you gotta create an explicit /pass location in addition to the existing /pass/ one, otherwise, an implicit location /pass {return 301 /pass/…;} will be created for you.

However, are you sure you actually want to do what you're trying to do? If you're going to omit a redirect from /pass to /pass/, then relative paths aren't going to work. Some newer browsers also have a tendency to feature defective UI/UX that may not present the trailing slash to the user, which may make things even more confusing when trying to troubleshoot the distinction.

cnst
  • 12,948
  • 7
  • 51
  • 75
  • 1
    the point is that location `/pass/` is forcing redirect for `/pass` to match it as long as I think, `/pass` should better match location `/` and not `/pass/` – igor Dec 20 '13 at 09:11
  • 10
    @cnst: actually it will, and it's documented as doing that. See the last paragraph of http://nginx.org/en/docs/http/ngx_http_core_module.html#location . What's going on is that if you include any of proxy_pass, fastcgi_pass, uwsgi_pass, scgi_pass, or memcached_pass in a location with a trailing slash, it sets the 'auto_redirect' flag on the location, which triggers the redirect on this line: https://github.com/nginx/nginx/blob/1176952193ccf47078dc84b8494d0496ad1ac4a2/src/http/ngx_http_core_module.c#L1684 – bazzargh Aug 30 '14 at 12:25
2

As you want it to go to another location, a natural solution is to add that location

location /pass {
}

By default, nginx will look for a file called 'pass' in the web root. If you don't like this default, you may add more directives inside this new location block to achieve that.

You may also use

location = /pass {
}

such that nginx can find the match a bit faster.

Chuan Ma
  • 131
  • 1
  • 4