How to config nginx for Vue-router on Docker

2019-07-06 12:14发布

问题:

I'm setting up a docker image from nginx to serve a Vue app as static files. My vue app uses Vue-Router and it works perfectly on an other server. My nginx config is just like this: https://router.vuejs.org/guide/essentials/history-mode.html#example-server-configurations

And now I wanna migrate to docker, and this is my Dockerfile

# build stage
FROM node:9.11.1-alpine as build-stage
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build

# production stage
FROM nginx:1.15.8-alpine as production-stage
COPY docker/nginx/prod.conf /etc/nginx/nginx.conf/prod.conf         # [*]
COPY --from=build-stage /app/dist /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

And this is the docker/nginx/prod.conf

server {
  listen 80;
  server_name _ default_server;

  root /usr/share/nginx/html;

  location / {
    try_files $uri $uri/ /index.html;
  }
}

It works with the home page, for example: http://192.168.1.10 but got 404 on other URL, such as http://192.168.1.10/settings

The prod.conf was copied to the nginx folder, I do see it.

Do you have any idea, or am I missing something?

回答1:

This is how I solved my problem.

# Add nginx config
COPY .docker/nginx/prod.conf /temp/prod.conf
RUN envsubst /app < /temp/prod.conf > /etc/nginx/conf.d/default.conf