Tuesday, September 16, 2014

docker - How can I use environment variables in Nginx.conf

[Cross-posted and edited down from https://stackoverflow.com/questions/21933955 as it was considered too sysadmin-like for StackOverflow.]



I have a docker container running Nginx, 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 can I get environment variables into the Nginx configuration 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 nginx configuration:



root@87ede56e0b11:/# nginx -s reload
nginx: [emerg] directive "server" is not terminated by ";" in /etc/nginx/sites-enabled/default:3

No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...