如何通过厨师将域名传递给Nginx conf

时间:2015-10-28 10:34:21

标签: amazon-web-services nginx chef

我已经通过厨师安装了Nginx,但我必须将" AWS实例公共IP传递给Chef Recipe"

我正在创建AWS实例,并通过Chef在该实例上安装了我的应用程序。

我必须使用我的默认安装方法和我的Nginx.conf配置文件

运行nginx

那么我们如何在Nginx配置中使用新创建的实例Public IP或EC-2 URL,即代替server_name,以便我可以浏览URL?或者如何将域名传递给Nginx.conf?

2 个答案:

答案 0 :(得分:1)

您可以使用EC2元数据:

curl http://169.254.169.254/latest/meta-data/public-ipv4
curl http://169.254.169.254/latest/meta-data/public-hostname

要获取所有可用密钥的列表,请尝试以下操作:

curl http://169.254.169.254/latest/meta-data/

ami-id
ami-launch-index
ami-manifest-path
block-device-mapping/
hostname
instance-action
instance-id
instance-type
local-hostname
local-ipv4
mac
metrics/
network/
placement/
product-codes
profile
public-hostname
public-ipv4
public-keys/
reservation-id
security-groups

然而 server_name 可以为空,请参阅the docs

  

如果服务器块中没有定义server_name,则nginx使用   空名称作为服务器名称。 Nginx版本高达0.8.48使用机器的主机名作为服务器   在这种情况下的名称。如果服务器名称定义为“$ hostname”(0.9.4),   使用机器的主机名。

答案 1 :(得分:1)

你可以利用ohai插件ec2给出正确的属性,主要针对这种情况node['ec2']['public_ipv4']

缺点是,ohai无法猜测自己是否在AWS实例上并且没有运行插件,为此您必须创建一个提示文件,在那里ho sa {{3为此。

有一个例子可以在厨师的运行中创建提示,但他们是相当复杂的IMO,这就是我要做的事情(未经测试,评论如果它破坏):

# Resource to reload the ec2 plugin if the hint is created
ohai 'ec2' do
  plugin 'ec2' 
  action :nothing
end

ohai_hint 'ec2' do
  notifies :reload, 'ohai[ec2]', :immediately # to force ohai to run the ec2 plugin now
end

你在运行列表的早期就已经开始使用这个食谱了,这本ercipe的食谱中必须有一个depends 'ohai'来加载ohai食谱{/ 1}}。