How do you use the environment variables in nginxconf?
I have a nginx docker container that links to another docker container The host name and ip address of the second container is loaded into the nginx container as environment variables on startup, but is not know before then (it's dynamic). I want my nginx.conf
to use these values - e.g.
upstream gunicorn {
server $APP_HOST_NAME:$APP_HOST_PORT;
}
How do you change the environment variables in nginx on startup?
EDIT 1
This is the entire file, after the suggested answer below.
env APP_WEB_1_PORT_5000_TCP_ADDR;
# Nginx host configuration for django_app
# Django app is served by Gunicorn, running under port 5000 (via Foreman)
upstream gunicorn {
server $ENV{"APP_WEB_1_PORT_5000_TCP_ADDR"}:5000;
}
server {
listen 80;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location /static/ {
alias /app/static/;
}
location /media/ {
alias /app/media/;
}
location / {
proxy_pass http://gunicorn;
}
}
Reloading nginx then errors.
$ nginx -s reload
nginx: [emerg] unknown directive "env" in /etc/nginx/sites-enabled/default:1
EDIT 2: more details
Current environment variables
root@87ede56e0b11:/# env | grep APP_WEB_1
APP_WEB_1_NAME=/furious_turing/app_web_1
APP_WEB_1_PORT=tcp://172.17.0.63:5000
APP_WEB_1_PORT_5000_TCP=tcp://172.17.0.63:5000
APP_WEB_1_PORT_5000_TCP_PROTO=tcp
APP_WEB_1_PORT_5000_TCP_PORT=5000
APP_WEB_1_PORT_5000_TCP_ADDR=172.17.0.63
Root nginx.conf.
root@87ede56e0b11:/# head /etc/nginx/nginx.conf
user www-data;
worker_processes 4;
pid /var/run/nginx.pid;
env APP_WEB_1_PORT_5000_TCP_ADDR;
Site nginx configuration.
root@87ede56e0b11:/# head /etc/nginx/sites-available/default
# Django app is served by Gunicorn, running under port 5000 (via Foreman)
upstream gunicorn {
server $ENV{"APP_WEB_1_PORT_5000_TCP_ADDR"}:5000;
}
server {
listen 80;
Reload the nginx configuration
root@87ede56e0b11:/# nginx -s reload
nginx: [emerg] directive "server" is not terminated by ";" in /etc/nginx/sites-enabled/default:3
From the official Nginx docker file.
Using environment variables in nginx configuration:
Out-of-the-box, Nginx doesn't support using environment variables inside most configuration blocks.
But
envsubst
may be used as a workaround if you need to generate your nginx configuration dynamically before nginx starts.Here is an example using docker-compose.yml:
image: nginx
volumes:
- ./mysite.template:/etc/nginx/conf.d/mysite.template
ports:
- "8080:80"
environment:
- NGINX_HOST=foobar.com
- NGINX_PORT=80
command: /bin/bash -c "envsubst < /etc/nginx/conf.d/mysite.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"
The mysite.template file may then contain variable references like this :
listen ${NGINX_PORT};
Update:
But you know what this caused to its variables like this
proxy_set_header X-Forwarded-Host $host;
damaged to.
proxy_set_header X-Forwarded-Host ;
So i use this trick to prevent it
I have a script to run Nginx, that used on the docker-compose
file as command option for Nginx server, i named it run_nginx.sh
.
#!/usr/bin/env bash
export DOLLAR='$'
envsubst < nginx.conf.template > /etc/nginx/nginx.conf
nginx -g "daemon off;"
And because of defined new DOLLAR
variable on run_nginx.sh
script, now content of my nginx.conf.template
file for Nginx itself variable is like this.
proxy_set_header X-Forwarded-Host ${DOLLAR}host;
And for my defined variable is like this.
server_name ${WEB_DOMAIN} www.${WEB_DOMAIN};
Also here , there is my real use case for that.