Can nginx.conf access environment variables?

6/18/2015

I'm trying to run a docker container with nginx on a kubernetes cluster. I'm using the environment variable service discovery for all my other containers, so I would like to keep it consistent and not have to bring something like skydns into the mix just because of this. Is it possible to access environment variables in nginx such that I can tell it to proxy-pass to a kubernetes service?

-- Christian Grabowski
docker
environment-variables
kubernetes
nginx

5 Answers

7/10/2015

There were tons of issues with doing the hacky HEREDOC, including it only having one time service discovery (not much better than hard coding). So my solution ended up being using confd to template nginx and restart nginx when the environment variables change. Here's the link to confd: https://github.com/kelseyhightower/confd

-- Christian Grabowski
Source: StackOverflow

11/29/2017

Keeping an included config file in the ConfigMap mounted as volume should work too.

You might need to change the config files' structure for that though.

-- user6317694
Source: StackOverflow

6/20/2015

You mean use the value of an env var set in this way in a config file for nginx? One thing I have done in the past is to have a run.sh config script that is run by the Docker container which uses the env variable to effect substation in template file for an nginx config -- is that would you mean?

-- Satnam Singh
Source: StackOverflow

6/23/2015
-- Satnam Singh
Source: StackOverflow

6/19/2015

In the spec you can define an environment variable e.g.

spec:
  containers:
  - name: kibana-logging
    image: gcr.io/google_containers/kibana:1.3
    livenessProbe:
      name: kibana-liveness
      httpGet:
        path: /
        port: 5601
      initialDelaySeconds: 30
      timeoutSeconds: 5
    env:
      - name: "ELASTICSEARCH_URL"
        value: "http://elasticsearch-logging:9200"
    ports:
    - containerPort: 5601
      name: kibana-port
      protocol: TCP

This will cause the environment variable ELASTICSEARCH_URL to be set to http://elasticsearch-logging:9200. Will this work for you?

Cheers,

Satnam

-- Satnam Singh
Source: StackOverflow