Cloud Foundry环境变量在nginx.conf中不起作用

问题描述 投票:0回答:1

我正在尝试创建一个通用的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环境变量已解决。

相反,当我将我的应用程序推送到云代工厂时,我得到:

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 version:

{ "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

cloudfoundry swisscomdev
1个回答
1
投票

如果您正在使用Nginx构建包,则可以使用文档中的方法来访问环境变量。

location /api { proxy_pass {{env "MY_DEV_PROXY"}}; }

https://docs.cloudfoundry.org/buildpacks/nginx/#env


如果您使用的是Staticfile buildpack,则不能使用Nginx buildpack中的相同功能(至少在撰写本文时)。

Staticfile buildpack会自动为您生成大部分/全部Nginx配置,因此从技术上讲,您不需要插入任何环境变量。但是,您可以在Staticfile buildpack中包含自定义Nginx片段,因此想要从这些片段访问环境变量是合理的。

如果你想这样做,你需要做这样的事情:

  1. 参见Custom Location instructions here。你需要在root中设置一个替代的location_includeStaticfile。这将引用并指示Nginx处理您通过应用程序提供的自定义配置。
  2. 而不是指定自定义配置文件,请指定自定义erb脚本。例如:nginx/conf/includes/custom_header.conf.erb。这应该包含您的配置作为模板,但您可以引用像<%= ENV["MY_VAR"] %>这样的env变量。您还可以在erb模板中执行任何其他有效的操作。 location /api { proxy_pass <%= ENV["MY_DEV_PROXY"] %>; }
  3. .profile script添加到应用程序的根目录。在此脚本中,您将使用erb处理模板文件并生成实际配置。 erb nginx/conf/includes/custom_header.conf.erb > nginx/conf/includes/custom_header.conf 当您的应用启动时,它将运行此脚本并将您的模板转换为实际的自定义配置。然后Nginx将加载自定义配置。

希望有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.