From df2beb7b1ddb96adba673b0fda55f2106b4608d5 Mon Sep 17 00:00:00 2001 From: Adam French Date: Thu, 20 Nov 2025 22:13:58 +0000 Subject: [PATCH] adding backend port env var to nginx --- docker-compose.yml | 1 + nginx/Dockerfile | 1 + nginx/entrypoint.sh | 4 ++-- nginx/nginx.conf.template | 2 +- 4 files changed, 5 insertions(+), 3 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index e734b31..6d197b0 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,6 +10,7 @@ services: container_name: nginx environment: BACKEND_HOST: "${BACKEND_HOST}" # from .env + BACKEND_PORT: "${BACKEND_PORT}" # from .env DOMAIN: "${DOMAIN}" # from .env restart: unless-stopped depends_on: diff --git a/nginx/Dockerfile b/nginx/Dockerfile index d0a5d7c..bfd6372 100644 --- a/nginx/Dockerfile +++ b/nginx/Dockerfile @@ -12,6 +12,7 @@ RUN apt-get update && apt-get install -y gettext-base # Replace variables at build time ENV BACKEND_HOST=backend +ENV BACKEND_PORT=8080 ENV DOMAIN=example.com ENTRYPOINT ["/entrypoint.sh"] diff --git a/nginx/entrypoint.sh b/nginx/entrypoint.sh index dbef42f..8faaf52 100755 --- a/nginx/entrypoint.sh +++ b/nginx/entrypoint.sh @@ -4,10 +4,10 @@ set -e # Check if certificate exists if [ -f "/etc/letsencrypt/live/$DOMAIN/fullchain.pem" ] && [ -f "/etc/letsencrypt/live/$DOMAIN/privkey.pem" ]; then echo "Certificates found. Using production nginx config." - envsubst '$BACKEND_HOST $DOMAIN' < /etc/nginx/nginx.conf.template > /etc/nginx/nginx.conf + envsubst '$BACKEND_HOST $BACKEND_PORT $DOMAIN' < /etc/nginx/nginx.conf.template > /etc/nginx/nginx.conf else echo "Certificates NOT found. Using setup nginx config." - envsubst '$BACKEND_HOST $DOMAIN' < /etc/nginx/nginx_setup.conf.template > /etc/nginx/nginx.conf + envsubst '$BACKEND_HOST $BACKEND_PORT $DOMAIN' < /etc/nginx/nginx_setup.conf.template > /etc/nginx/nginx.conf fi # Start nginx diff --git a/nginx/nginx.conf.template b/nginx/nginx.conf.template index 82df8ef..4551c40 100644 --- a/nginx/nginx.conf.template +++ b/nginx/nginx.conf.template @@ -49,7 +49,7 @@ http { } location /posts/ { - proxy_pass http://$BACKEND_HOST:8080/posts; + proxy_pass http://$BACKEND_HOST:$BACKEND_PORT/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;