0

I have nginx running and want to serve an static application at the URL https://example.com/myapp.

I have a server block like so:

server {
  listen 443 ssl http2;
  server_name example.com;

  root /var/www/html;

  location /myapp/ {
    root /home/tri/myapp/dist;
    rewrite ^/myapp/?(.*)$ /$1 break;
  }
}

I expect that by going to https://example.com/myapp, it will serve the index.html file, but that's not the case.

If I go to https://example.com/myapp/index.html, the file is served correctly. However, if I just go to https://example.com/myapp, I get this error in nginx:

2020/03/28 15:17:40 [error] 27905#27905: *11410 open() "/var/www/html/index.html" failed (2: No such file or directory), client: 73.119.123.242, server: example.com, request: "GET /myapp/ HTTP/2.0", host: "example.com"

So for some reason, it's falling back to the default root directive.

What am I doing wrong here?

Tri Nguyen
  • 9,950
  • 8
  • 40
  • 72

3 Answers3

1

You can use this config

 location /myapp {

        try_files $uri $uri/ /home/tri/myapp/dist/index.html?$args;
        }

Linuxian
  • 106
  • 9
0

I'm still not sure why my original configuration doesn't work, but thanks to this answer https://stackoverflow.com/a/10647080/1368032, I switched to using alias, and it's now working for me (without the rewrite):

server {
  listen 443 ssl http2;
  server_name example.com;

  root /var/www/html;

  location /myapp/ {
    alias /home/tri/myapp/dist/;

  }
}
Tri Nguyen
  • 9,950
  • 8
  • 40
  • 72
-1

Your location “/myapp/“ won’t match “/myapp” without the trailing “/“ - you probably want to tweak the location value to make that optional, but I’m not near a computer where I can verify the correct syntax just at the moment.

Gwyn Evans
  • 1,361
  • 7
  • 17