0

Simple nginx config

server {
  listen 0.0.0.0:80;

  server_name localhost 127.0.0.1;

  location /en-us/ {
    return 301 http://www.lego.com;
  }
}

Here's what happens when I curl that config without a trailing slash on /en-us.

curl -v http://127.0.0.1/en-us -o /dev/null
# HTTP/1.1 404 Not Found

I expect that 404. The location block is using a prefix match, and so it makes sense to me that /en-us is not matching that block.

Here's where I get confused...

Same config, but with a proxy_pass

Here I use the same config, but instead of returning a 301 I do a proxy_pass.

server {
  listen 0.0.0.0:80;

  server_name localhost 127.0.0.1;

  location /en-us/ {
    proxy_pass http://www.lego.com;
  }
}

Now watch what happens when I run the same curl.

curl -v http://127.0.0.1/en-us -o /dev/null
# HTTP/1.1 301 Moved Permanently

That 301 on /en-us makes no sense to me. It's redirecting to Location: http://127.0.0.1/en-us/.

In the first config, /en-us seemed to not match my location block (404). In the second config, /en-us did (301).

  1. Why do the same location rules seem to match differently between my two configs?
  2. Why would that second config result in a 301 to add a trailing slash?

I'm using nginx version: nginx/1.4.6 (Ubuntu)

2
  • 1
    nginx.org/r/location read last paragraph Commented Mar 24, 2016 at 22:58
  • That explains it perfectly. Thank you! "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, ... then the special processing is performed ... a permanent redirect with the code 301 will be returned to the requested URI with the slash appended ..." Commented Mar 25, 2016 at 0:49

1 Answer 1

0

The documentation explains things clearly, as is pointed out in the comments.

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, ... then the special processing is performed ... a permanent redirect with the code 301 will be returned to the requested URI with the slash appended ...

Special processing is done for trailing slashes on a proxy_pass

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.