2013-10-13 62 views
2

dotcloud提供了很多有用的信息作为环境变量,但我的nginx.conf无法访问环境变量。什么是解决这个问题的好方法?在nginx.conf中使用dotcloud env变量

以下是这种情况:我想将我的www静态服务中的某些网址重定向到我的rest服务,因此我目前将目标网址硬编码为nginx.conf。我宁愿使用DOTCLOUD_REST_HTTP_HOST变量,因为这可以让我的服务轻松迁移。

+0

dotCloud是否禁止访问'env' conf指令? http://wiki.nginx.org/CoreModule#env –

回答

4

这是使用一个很好的例子postinstall脚本为您服务。 dotCloud提供build hooks用于在推送和部署周期的每个阶段运行,而postinstall挂接运行最后一次,它可以访问所有环境变量。您可以从~/environment.json或从实际环境中读取变量(该文件稍微可靠一些,因为放入环境的内容可能因服务类型而异,这是出于历史原因)。

下面是一个例子dotcloud.yml

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

和示例nginx.conf(在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; 

最后,示例性postinstall(在WWW /安装后发现)其中读取$ LIMITRATEVAR的值超出环境并使用sed更新nginx.conf:

#!/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" 
2

解决方案here适用于某些人,这并不是说在DotCloud上可能会出现一些问题,但如果您还没有尝试过,那么值得一试。

在顶部配置水平:

env MYVAR; 

在http级别:

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

然后,例如,在服务器级:

root $myvar; 
+0

DotCloud将我的'nginx.conf'文件包含在全局指定的'server {}'块中。由于我不能控制顶层配置,我不能使用'env'指令。 – prideout

+0

Gotcha。在那种情况下,我什么也没有,对不起。 –

+0

你需要和谁来控制,然后:) –