gpt4 book ai didi

dotcloud - 在 nginx.conf 中使用 dotcloud 环境变量

转载 作者:行者123 更新时间:2023-12-01 04:26:47 24 4
gpt4 key购买 nike

dotcloud 提供了许多有用的信息作为环境变量,但我的 nginx.conf 无法访问环境变量。有什么好的解决方法?

场景如下:我想将某些 URL 从我的 www 静态服务重定向到我的 rest 服务,所以我目前正在将目标 URL 硬编码到 nginx.conf。我宁愿使用 DOTCLOUD_REST_HTTP_HOST 变量,因为这样可以让我的服务轻松迁移。

最佳答案

这是为您的服务使用 postinstall 脚本的好例子。dotCloud 提供 build hooks用于在推送和部署周期的每个阶段运行,postinstall Hook 最后运行,它可以访问所有环境变量。您可以从 ~/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/postinstall),其中从环境中读取 $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 - 在 nginx.conf 中使用 dotcloud 环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19349493/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com