Cloud Foundry 环境变量在内部不起作用 nginx.conf
Cloud Foundry environment variables not working inside nginx.conf
我正在尝试创建一个通用 nginx.conf
,它根据环境执行 proxy_pass
。
我正在使用来自 Cloud Foundry staticfile-buildpack 的 fixture
https://github.com/cloudfoundry/staticfile-buildpack/tree/master/fixtures/pushstate_and_proxy_pass/
我想根据环境变量设置代理通行证
这是proxy.conf的代码:
location /api { proxy_pass {{env "MY_DEV_PROXY"}}; }
我希望我之前设置的 MY_DEV_PROXY 环境变量得到解决。
相反,在将我的应用程序推送到 Cloud Foundry 时,我得到:
ERR 2019/02/19 08:18:39 [emerg] 88#0:指令 "proxy_pass" 未以“;”结尾在 /home/vcap/app/nginx/conf/includes/proxy.conf:1
当使用直接值而不是变量时:
location /api { proxy_pass https://my-dev-proxy.com; }
一切正常。
cf curl /v2/info && cf 版本:
{
"description": "Cloud Foundry provided by Swisscom",
"min_cli_version": "6.42.0",
"min_recommended_cli_version": "latest",
"api_version": "2.128.0",
"osbapi_version": "2.14",
}
cf version 6.42.0+0cba12168.2019-01-10
如果您使用的是 Nginx 构建包,则可以使用文档中的方法访问环境变量。
location /api { proxy_pass {{env "MY_DEV_PROXY"}}; }
https://docs.cloudfoundry.org/buildpacks/nginx/#env
如果您使用的是 Staticfile buildpack,则无法使用 Nginx buildpack 中的相同功能(至少在撰写本文时)。
Staticfile buildpack 会自动为您生成 most/all Nginx 配置,因此从技术上讲您不需要插入任何环境变量。但是,您可以在 Staticfile buildpack 中包含自定义 Nginx 片段,因此希望从这些片段访问环境变量是合理的。
如果你想这样做,你需要做这样的事情:
参见Custom Location
instructions here。您需要在 Staticfile
中设置替代 root
和 location_include
。这将引用并指示 Nginx 处理您通过应用程序提供的自定义配置。
不指定自定义配置文件,而是指定自定义 erb
脚本。例如:nginx/conf/includes/custom_header.conf.erb
。这应该包含您的配置作为模板,但您可以引用环境变量,如 <%= ENV["MY_VAR"] %>
。您还可以执行 erb 模板中有效的任何其他操作。
location /api { proxy_pass <%= ENV["MY_DEV_PROXY"] %>; }
将 .profile
script 添加到应用程序的根目录。在此脚本中,您将使用 erb
来处理模板文件并生成实际配置。
erb nginx/conf/includes/custom_header.conf.erb > nginx/conf/includes/custom_header.conf
当您的应用程序启动时,它将 运行 此脚本并将您的模板转换为实际的自定义配置。然后 Nginx 将加载自定义配置。
希望对您有所帮助!
我正在尝试创建一个通用 nginx.conf
,它根据环境执行 proxy_pass
。
我正在使用来自 Cloud Foundry staticfile-buildpack 的 fixture
https://github.com/cloudfoundry/staticfile-buildpack/tree/master/fixtures/pushstate_and_proxy_pass/
我想根据环境变量设置代理通行证
这是proxy.conf的代码:
location /api { proxy_pass {{env "MY_DEV_PROXY"}}; }
我希望我之前设置的 MY_DEV_PROXY 环境变量得到解决。
相反,在将我的应用程序推送到 Cloud Foundry 时,我得到:
ERR 2019/02/19 08:18:39 [emerg] 88#0:指令 "proxy_pass" 未以“;”结尾在 /home/vcap/app/nginx/conf/includes/proxy.conf:1
当使用直接值而不是变量时:
location /api { proxy_pass https://my-dev-proxy.com; }
一切正常。
cf curl /v2/info && cf 版本:
{
"description": "Cloud Foundry provided by Swisscom",
"min_cli_version": "6.42.0",
"min_recommended_cli_version": "latest",
"api_version": "2.128.0",
"osbapi_version": "2.14",
}
cf version 6.42.0+0cba12168.2019-01-10
如果您使用的是 Nginx 构建包,则可以使用文档中的方法访问环境变量。
location /api { proxy_pass {{env "MY_DEV_PROXY"}}; }
https://docs.cloudfoundry.org/buildpacks/nginx/#env
如果您使用的是 Staticfile buildpack,则无法使用 Nginx buildpack 中的相同功能(至少在撰写本文时)。
Staticfile buildpack 会自动为您生成 most/all Nginx 配置,因此从技术上讲您不需要插入任何环境变量。但是,您可以在 Staticfile buildpack 中包含自定义 Nginx 片段,因此希望从这些片段访问环境变量是合理的。
如果你想这样做,你需要做这样的事情:
参见
Custom Location
instructions here。您需要在Staticfile
中设置替代root
和location_include
。这将引用并指示 Nginx 处理您通过应用程序提供的自定义配置。不指定自定义配置文件,而是指定自定义
erb
脚本。例如:nginx/conf/includes/custom_header.conf.erb
。这应该包含您的配置作为模板,但您可以引用环境变量,如<%= ENV["MY_VAR"] %>
。您还可以执行 erb 模板中有效的任何其他操作。location /api { proxy_pass <%= ENV["MY_DEV_PROXY"] %>; }
将
.profile
script 添加到应用程序的根目录。在此脚本中,您将使用erb
来处理模板文件并生成实际配置。erb nginx/conf/includes/custom_header.conf.erb > nginx/conf/includes/custom_header.conf
当您的应用程序启动时,它将 运行 此脚本并将您的模板转换为实际的自定义配置。然后 Nginx 将加载自定义配置。
希望对您有所帮助!