I need help understanding trailing slash behaviour in Nginx

zestyregnum

Novice
I'm setting up nginx as a reverse proxy for squaremap (a world map viewer for Minecraft servers) and encountering unexpected behaviour with trailing slashes. I've followed the squaremap documentation for serving with nginx acting as a reverse proxy (https://github.com/jpenilla/squaremap/wiki/Internal-vs-External-Web-Server), but I'm confused by the results. Here's what I've tried:

squaremap is running at 127.0.0.1:39000

Configuration:

1.
Code:
location /squaremap {
    proxy_pass http://127.0.0.1:39000;
}

Result: Accessing https://example.com/squaremap returns a 404 error.

2.
Code:
location /squaremap {
    proxy_pass http://127.0.0.1:39000/;
}

Result: https://example.com/squaremap shows a blank page, but https://example.com/squaremap/ works fine.

3.
Code:
location /squaremap/ {
    proxy_pass http://127.0.0.1:39000/;
}

Result: https://example.com/squaremap redirects to https://example.com/squaremap/ and then displays the web interface.
https://example.com/squaremap/ works as expected.

In my attempt to figure out what was happening, I read part of the nginx documentation on proxy_pass. However, I'm not sure if my interpretation is correct. My understanding is:
  1. If there's no URI in the proxy_pass directive, the request URI is passed to the upstream unchanged.
  2. If there is a URI in the proxy_pass directive, the part of the request matching the location directive is substituted by the value of the URI in the proxy_pass directive.

Based on this, I created a table of what I think is happening in each of the above cases:

Case Original RequestRequest to UpstreamResult
1https://example.org/squaremap/squaremapError 404
2.ahttps://example.org/squaremap/White page
2.bhttps://example.org/squaremap///Works
3https://example.org/squaremap//Works

My questions are:
  1. Is my interpretation of how nginx processes these requests correct?
  2. Why do I get different results in cases 2a and 3, even though they seem to send the same request to the upstream?
  3. Why doesn't it work without the trailing slash, but works with it?
  4. Is there a configuration that would allow both /squaremap and /squaremap/ to work correctly without a redirect?

I'd appreciate any insights into understanding this behaviour and how to properly configure nginx for this use case.
 
Last edited:
Try using regular expression in your "location" directive like this:

Code:
location ~ ^/squaremap/?$

And keep your tailing slash in the proxy_pass directive like this:

Code:
proxy_pass http://127.0.0.1:39000/;
.

As for your first 3 questions all I can say is try nginx alternatives if you can. I've worked with nginx for some work projects and for personal use and it's a quirky beast to put it mildly.

You've already faced the nakhras of nginx with a simple one liner config and I guarantee you'll repent if you use it for more elaborate stuff.

People have suggested to me to try "caddy" or the one created by cloudflare I don't remember the name but I never got around to it yet.
 
Last edited:
Back
Top