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 –