Pregunta

dotcloud provides lots of useful information as environment variables but my nginx.conf cannot access environment variables. What's a good way around this?

Here's the scenario: I'd like to redirect certain URLs from my www static service to my rest service, so I'm currently hardcoding the target URL into nginx.conf. I'd rather use the DOTCLOUD_REST_HTTP_HOST variable since that would allow my service to easily migrate.

¿Fue útil?

Solución

This is a good case for using the postinstall script for your service. dotCloud provides build hooks for running at each stage of the push and deploy cycle, and the postinstall hook runs last, where it has access to all your environment variables. You could read the variables from ~/environment.json or from the actual environment (the file is slightly more dependable, since what gets put into the environment may vary by service type, for historical reasons).

Here is an example dotcloud.yml:

www:
   approot: www
   type: static
   environment:
     LIMITRATEVAR: 2k

and an example nginx.conf (found in www/nginx.conf):

# limitratevar should get set in dotcloud.yml's environment section
# or via `dotcloud env set limitratevar` if you want to set it for all services.
# And then use a postinstall script to update this nginx.conf with sed
limit_rate LIMITRATEVAR;

and finally, the example postinstall (found in www/postinstall) where read the value of $LIMITRATEVAR out of the environment and update nginx.conf with sed:

#!/usr/bin/env bash

# files named "postinstall" will get run automatically when the code is deployed
# to the container where your service runs.
# See:
# http://docs.dotcloud.com/guides/build-file/#prebuild-postbuild-postinstall-build-hooks
# http://docs.dotcloud.com/guides/hooks/#post-install

# Make sure we error-out if there are any problems
set -e
echo "Updating the limit_rate to $LIMITRATEVAR"
sed -i.original "s/LIMITRATEVAR/$LIMITRATEVAR/g" ~/current/nginx.conf
echo "nginx.conf updated. Original stored in nginx.conf.original"

Otros consejos

The solution here works for some, which isn't to say there may be some issue implementing it on DotCloud, but it's worth a go if you haven't tried it.

At top config level:

 env MYVAR;

At http level:

 perl_set $myvar 'sub { return $ENV{"MYVAR"}; }';

Then, for example, at server level:

 root $myvar; 
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top