在上游声明中使用variables?

有没有可能在nginx上游指令中使用variables?

upstream appserver { server unix:/socks/$host.sock } 

我一直在寻找我的小心,找不到答案。

A)可能吗?
B)这是一个可怕的主意吗?

做这样的configuration通常是一个糟糕的主意。

你最好做以下事情:

一般configuration:

 http { ... include upstreams/*.conf; ... server { ... 

上行信/ *屏的configuration:

使用一个脚本来生成这些文件,并为你更改variables,然后使用nginx -s reload加载configuration

示例python脚本:

 from os import system upstream = "something" with open('upstreams/bro.conf', 'w') as f: f.write('upstream {0} {{\n\tserver unix:/socks/{0}.sock\n}}'.format(upstream) system('nginx -s reload') 

a)不。

b)是的。 认为它可能对开发configuration有用,

我发现在dev中使用这个代码段没有问题:

 proxy_pass http://unix:/socks/$host.sock;