Home > Net >  How can I use environment variables passed from deployment file in nginx.conf
How can I use environment variables passed from deployment file in nginx.conf

Time:05-12

Below is the logic in my docker file. I am using nginx to build the application.

FROM node:14-alpine as builder

COPY package.json ./
RUN npm install && mkdir /app && mv ./node_modules ./app
WORKDIR /app

COPY . .

RUN npm run build


FROM nginx:1.16.0-alpine
COPY --from=builder /app/build /usr/share/nginx/html
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d

EXPOSE 3000

CMD ["nginx", "-g", "daemon off;"]

Below is the nginx.conf file

server {
   listen 3000;
      
   location / {
     root /usr/share/nginx/html;
     index index.html index.htm;
     try_files $uri $uri/ /index.html =404;
   }
  
   location /api {
    #  rewrite /api/api(.*) /$1 break;
     proxy_pass http://${backend_host}:${backend_port}/api;
   }
      
   include /etc/nginx/extra-conf.d/*.conf;
}

backend_host and backend_port in the proxy_pass URL will be provided while deploying the image using Deployment file.

Is this possible? If not is there any alternative way for this?

CodePudding user response:

If you want to dynamically mount the nginx.conf I would suggest using the config map with your deployment.yaml

So this way you re-use your docker image multiple times without recreating it and pass the config map to update it.

You docker file will be

FROM node:14-alpine as builder
COPY package.json ./
RUN npm install && mkdir /app && mv ./node_modules ./app
WORKDIR /app
COPY . .
RUN npm run build


FROM nginx:1.16.0-alpine
COPY --from=builder /app/build /usr/share/nginx/html
RUN rm /etc/nginx/conf.d/default.conf
EXPOSE 3000
CMD ["nginx", "-g", "daemon off;"]

Example configmap

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config
data:
  default.conf: |-
    server {
            listen 80 default_server;
            root /var/www/html;
            server_name  _;
            index index.php;
            location / {
                try_files $uri $uri/ /index.php?$args;
            }
            location ~ \.php$ {
                fastcgi_split_path_info ^(. \.php)(/. )$;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                include fastcgi_params;
                fastcgi_param   PATH_INFO       $fastcgi_path_info;
                fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
            }
        }

Mount the config map to deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: app
  labels:
    app: app
spec:
  selector:
    matchLabels:
      app: app
  replicas: 1
  template:
    metadata:
      labels:
        app: app
    spec:
      containers:
        - name: app
          image: app-image
          ports:
          - containerPort: 80
          volumeMounts:
            - name: nginx-config
              mountPath: /etc/nginx/nginx.conf
              subPath: nginx.conf
      volumes:
        - name: nginx-config
          configMap:
            name: confnginx

For more details read at : https://blog.meain.io/2020/dynamic-reverse-proxy-kubernetes/

  • Related