【问题标题】:using dotcloud env variables in nginx.conf在 nginx.conf 中使用 dotcloud 环境变量
【发布时间】:2013-10-21 09:13:42
【问题描述】:

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

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

【问题讨论】:

标签: dotcloud


【解决方案1】:

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

在顶级配置级别:

 env MYVAR;

在http级别:

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

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

 root $myvar; 

【讨论】:

  • DotCloud 将我的 nginx.conf 文件包含在全局指定的 server {} 块中。由于我无法控制顶级配置,因此无法使用 env 指令。
  • 明白了。在那种情况下,我什么都没有,对不起。
  • 你需要和控制的人谈谈,然后:)
【解决方案2】:

这是为您的服务使用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/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"

【讨论】:

    猜你喜欢
    • 2019-08-24
    • 2019-07-12
    • 2015-09-04
    • 1970-01-01
    • 1970-01-01
    • 2023-01-20
    • 2023-01-22
    • 1970-01-01
    • 2020-03-02
    相关资源
    最近更新 更多