dotcloud提供了很多有用的信息作为环境变量,但我的nginx.conf
无法访问环境变量。什么是解决这个问题的好方法?在nginx.conf中使用dotcloud env变量
以下是这种情况:我想将我的www
静态服务中的某些网址重定向到我的rest
服务,因此我目前将目标网址硬编码为nginx.conf
。我宁愿使用DOTCLOUD_REST_HTTP_HOST
变量,因为这可以让我的服务轻松迁移。
dotcloud提供了很多有用的信息作为环境变量,但我的nginx.conf
无法访问环境变量。什么是解决这个问题的好方法?在nginx.conf中使用dotcloud env变量
以下是这种情况:我想将我的www
静态服务中的某些网址重定向到我的rest
服务,因此我目前将目标网址硬编码为nginx.conf
。我宁愿使用DOTCLOUD_REST_HTTP_HOST
变量,因为这可以让我的服务轻松迁移。
这是使用一个很好的例子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"
dotCloud是否禁止访问'env' conf指令? http://wiki.nginx.org/CoreModule#env –