diff --git a/docker-compose.yml b/docker-compose.yml index ff8380b..9a0453c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,16 +4,19 @@ networks: services: nginx: + build: + context: ./nginx + dockerfile: Dockerfile container_name: nginx + environment: + BACKEND_HOST: "${BACKEND_HOST}" # from .env restart: unless-stopped - image: nginx networks: - app-network ports: - 80:80 - 443:443 volumes: - - ./nginx/nginx.conf:/etc/nginx/nginx.conf - ./certbot/conf:/etc/letsencrypt - ./certbot/www:/var/www/certbot - ./html/:/etc/nginx/html:ro @@ -38,7 +41,7 @@ services: - db networks: - app-network - env_file: + env_file: - ./.env db: diff --git a/nginx/Dockerfile b/nginx/Dockerfile new file mode 100644 index 0000000..b36c6ad --- /dev/null +++ b/nginx/Dockerfile @@ -0,0 +1,13 @@ +FROM nginx:latest + + +# Copy template config +COPY nginx.conf.template /etc/nginx/nginx.conf.template + +# Update package list and install gettext-base +RUN apt-get update && apt-get install -y gettext-base + +# Replace variables at build time +ENV BACKEND_HOST=backend + +CMD /bin/sh -c "envsubst '\$BACKEND_HOST' < /etc/nginx/nginx.conf.template > /etc/nginx/nginx.conf && nginx -g 'daemon off;'" diff --git a/nginx/nginx.conf b/nginx/nginx.conf.template similarity index 95% rename from nginx/nginx.conf rename to nginx/nginx.conf.template index 89eedee..64fbe5b 100644 --- a/nginx/nginx.conf +++ b/nginx/nginx.conf.template @@ -38,7 +38,7 @@ http { } location /posts/ { - proxy_pass http://backend:8080/posts; + proxy_pass http://$BACKEND_HOST:8080/posts; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;