2

I have the following nginx configurations:

server {

    listen 80;

    charset     utf-8;
    client_max_body_size 5M;

    location /severbat {

        root /root/severbat/dist;
        try_files $uri /index.html;
    }

    location /swagger {
        root /root/av/swagger-ui-dist;
        try_files $uri /index.html;
    }

}

And I files /root/severbat/dist/index.html and /root/av/swagger-ui-dist/index.html exist. But if I open url http://localhost/severbat I see nginx greetings page instead of index.html

However if I serve files under / it works:

location / {
    root /root/severbat/dist;
    try_files $uri /index.html;
}

nginx-acesss.log:

2018/10/03 09:32:44 [debug] 23052#23052: *519 trying to use file: "/swagger" "/root/av/swagger-ui-dist/swagger"
2018/10/03 09:32:44 [debug] 23052#23052: *519 trying to use file: "/index.html" "/root/av/swagger-ui-dist/index.html"
2018/10/03 09:32:44 [debug] 23052#23052: *519 internal redirect: "/index.html?"
2018/10/03 09:32:44 [debug] 23052#23052: *519 rewrite phase: 1
2018/10/03 09:32:44 [debug] 23052#23052: *519 test location: "/severbat"
2018/10/03 09:32:44 [debug] 23052#23052: *519 test location: "/api"
2018/10/03 09:32:44 [debug] 23052#23052: *519 using configuration ""
2018/10/03 09:32:44 [debug] 23052#23052: *519 http cl:-1 max:5242880
...
2018/10/03 09:32:44 [debug] 23052#23052: *519 content phase: 17
2018/10/03 09:32:44 [debug] 23052#23052: *519 http filename: "/usr/share/nginx/html/index.html"
deathangel908
  • 173
  • 1
  • 2
  • 8

1 Answers1

1

Hello and welcome to ServerFault.

I think your problem is that NGINX appends the matched location to the specified root, so when you enter, for example, http://localhost/swagger NGINX looks for the directory /root/av/swagger-ui-dist/swagger and doesn't find it.

That's how the root directive works in NGINX

You might want to use the alias directive instead:

location /severbat {
    alias /root/severbat/dist;
    try_files $uri $uri/ index.html;
}

location /swagger {
    alias /root/av/swagger-ui-dist;
    try_files $uri $uri/ index.html;
}
Daniele Santi
  • 2,529
  • 1
  • 25
  • 22
  • Nginx respond with 404. If location is `/` configuration `/index.html` doesn't work as well – deathangel908 Oct 03 '18 at 09:49
  • @deathangel908 looks like the problem was elsewhere. I've updated my answer accordingly. – Daniele Santi Oct 03 '18 at 10:04
  • This is NOT a valid answer! Look at "Using the try_files $uri directive with alias": https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/ As you can see this is not going to work as expected. – Marek Marczak Aug 21 '20 at 15:02
  • I am having the exact same issue, and switching to `alias` also gives me a 404. Any updates on a solution to this issue? – Bernardo Subercaseaux Sep 15 '20 at 16:18