0

I'm trying to serve two static flat html files , but my location settings are only ever resolving into one. When I go to myurl.com/a/b it always serves up a.html.

The first thing I tried was nesting the /a/b location as according to this answer,

location /a/ {
 location /a/b {
  try_files /b.html
 }
 try_files /a.html
}

but only ever got a.html.

I then added the ^~ according to this location priority question thinking /a/b would get matched first, but it's still not.

location ^~ /a/b {
  try_files /b.html;
 }
 location /a/ {
  try_files /a.html;
 }

In the comments there was a link to https://nginx.viraptor.info/ , and if I copy the ^~ attempt into there the result is:

Final Match:
Location: /a/b
Match type: priority prefix

Both of these I also attempted with and without the $uri and $uri/, ex:

try_files $uri $uri/ XX.html

Some lines from my Nginx log files which I think are relevant:

[debug] 1222#1222: *10775 http uri: "/a/b/"
...
[debug] 1222#1222: *10775 test location: "/"
[debug] 1222#1222: *10775 test location: "a/"
[debug] 1222#1222: *10775 test location: ~ "/\.ht"
[debug] 1222#1222: *10775 using configuration "/a/"
...
[debug] 1222#1222: *10775 trying to use file: "/a.html" "/var/www/html/a.html"
[debug] 1222#1222: *10775 try file uri: "/a.html"

I then tried using an exact match according to this question, but still my result is a.html

 location = /a/b/ {
  try_files /b.html =404;
 }
 location /a/ {
  try_files /a.html =404;
 }

What am I doing wrong with my config?

Logue1021
  • 54
  • 8

1 Answers1

0

If both your static files a.html and b.html are located in one place. You need only one location to serve requests /a/ and /a/b:

location / {
   root ... ;
   try_files $uri =404;
   rewrite /([^/]+)/$ /$1.html last;
}
Aleksey Vaganov
  • 487
  • 3
  • 9