`try_files` not working with nginx docker image

I have a simple Angular single-page application that I’m trying to serve with Nginx docker image, version 1.17.1.

In my Dockerfile, I build and package Angular, then copy the resulting files to the nginx image.

FROM node:12.6.0 AS package-stage
WORKDIR /app
COPY ./angular/package.json ./angular/package-lock.json ./
RUN npm install
COPY ./angular/. ./
RUN npm run ng build -- --configuration=production

FROM nginx:1.17.1
COPY --from=package-stage /app/dist/* /usr/share/nginx/html/
COPY ./nginx/. /

The final COPY step copies my nginx configuration into the image. Currently, this is just /etc/conf.d/default.conf. I’ve changed only 1 line in this file, as shown below:

server {
    listen       80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        try_files $uri $uri/ /index.html;  <-- Added by me
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

}

The page index.html is accessible at http://localhost, but adding any additional url parameters (for example, http://localhost/dashboard), lands me at the Nginx 404 error page.

For information, here’s the contents of /usr/share/nginx/html within the Docker container:

# cd /usr/share/nginx/html
# ls -l
total 1616
-rw-r--r-- 1 root root  27338 Jul 28 17:13 3rdpartylicenses.txt
-rw-r--r-- 1 root root    494 Jun 25 12:19 50x.html
-rw-r--r-- 1 root root  15406 Jul 28 17:12 favicon.ico
-rw-r--r-- 1 root root   1027 Jul 28 17:13 index.html
-rw-r--r-- 1 root root 637237 Jul 28 17:12 main-es2015.22ca1a6907ac396057b0.js
-rw-r--r-- 1 root root 727972 Jul 28 17:13 main-es5.26d2792d89e453a5208c.js
-rw-r--r-- 1 root root  37298 Jul 28 17:12 polyfills-es2015.9a05c5eeb2c24cb2663d.js
-rw-r--r-- 1 root root 115451 Jul 28 17:13 polyfills-es5.fdab6b769da451ba6a00.js
-rw-r--r-- 1 root root   1440 Jul 28 17:12 runtime-es2015.d5623f03f1e64ac8e12f.js
-rw-r--r-- 1 root root   1440 Jul 28 17:13 runtime-es5.a8c9c2928baa49aa82ad.js
-rw-r--r-- 1 root root  64080 Jul 28 17:12 styles.56c73d0b9f8117f2238e.css

I make sure to hard-refresh the page each time, because I know that browser caching can be an issue sometimes. But still no luck.

Here are some example logs from the nginx container:

Camerons-Air:My-Site cameronhudson$ docker run -it --rm -p 80:80 gcr.io/my-site/github.com/cameronhudson8/my-site/frontend:latest
2019/07/28 17:55:50 [error] 7#7: *1 open() "/usr/share/nginx/html/dashboard" failed (2: No such file or directory), client: 172.17.0.1, server: localhost, request: "GET /dashboard HTTP/1.1", host: "localhost"
172.17.0.1 - - [28/Jul/2019:17:55:50 +0000] "GET /dashboard HTTP/1.1" 404 555 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36" "-"

It’s as if the try_files line is being ignored completely.

Source: StackOverflow