Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to config nginx for Vue-router on Docker

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?

like image 820
Son Tr. Avatar asked Jan 02 '19 13:01

Son Tr.


People also ask

Where is nginx config in Docker?

By default, the configuration file is named nginx. conf and placed in the directory /usr/local/nginx/conf, /etc/nginx, or /usr/local/etc/nginx.


1 Answers

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
like image 155
Son Tr. Avatar answered Sep 28 '22 14:09

Son Tr.