在 Nginx 位置规则中使用变量 [英] Using variables in Nginx location rules
问题描述
在 Nginx 中,我试图定义一个变量,它允许我为我的所有位置块配置一个子文件夹.我是这样做的:
In Nginx, I'm trying to define a variable which allows me to configure a sub-folder for all my location blocks. I did this:
set $folder '/test';
location $folder/ {
[...]
}
location $folder/something {
[...]
}
不幸的是,这似乎不起作用.虽然 Nginx 没有抱怨语法,但它在请求 /test/
时返回 404.如果我明确地写入文件夹,它就可以工作.那么如何在位置块中使用变量?
Unfortunately, this doesn't seem to work. While Nginx doesn't complain about the syntax, it returns a 404 when requesting /test/
. If I write the folder in explicitly, it works. So how can I use variables in location blocks?
推荐答案
你不能.Nginx 并不真正支持配置文件中的变量,它的开发人员嘲笑所有要求添加此功能的人:
You can't. Nginx doesn't really support variables in config files, and its developers mock everyone who ask for this feature to be added:
与普通静态配置相比,[变量] 的成本相当高.[A] 宏扩展和包含"指令应该与 [with] 一起使用,例如 sed + make 或任何其他常见的模板机制."http://nginx.org/en/docs/faq/variables_in_config.html
"[Variables] are rather costly compared to plain static configuration. [A] macro expansion and "include" directives should be used [with] e.g. sed + make or any other common template mechanism." http://nginx.org/en/docs/faq/variables_in_config.html
您应该编写或下载一个小工具,允许您从占位符配置文件生成配置文件.
You should either write or download a little tool that will allow you to generate config files from placeholder config files.
更新 下面的代码仍然有效,但我已将其全部打包到一个名为 Configurator 也在 Packagist 上,它允许从模板轻松生成 nginx/php-fpm 等配置文件以及各种形式的配置数据.
Update The code below still works, but I've wrapped it all up into a small PHP program/library called Configurator also on Packagist, which allows easy generation of nginx/php-fpm etc config files, from templates and various forms of config data.
例如我的 nginx 源配置文件如下所示:
e.g. my nginx source config file looks like this:
location / {
try_files $uri /routing.php?$args;
fastcgi_pass unix:%phpfpm.socket%/php-fpm-www.sock;
include %mysite.root.directory%/conf/fastcgi.conf;
}
然后我有一个定义了变量的配置文件:
And then I have a config file with the variables defined:
phpfpm.socket=/var/run/php-fpm.socket
mysite.root.directory=/home/mysite
然后我使用它生成实际的配置文件.看起来你是一个 Python 人,所以一个基于 PHP 的例子可能对你没有帮助,但对于其他使用 PHP 的人来说:
And then I generate the actual config file using that. It looks like you're a Python guy, so a PHP based example may not help you, but for anyone else who does use PHP:
<?php
require_once('path.php');
$filesToGenerate = array(
'conf/nginx.conf' => 'autogen/nginx.conf',
'conf/mysite.nginx.conf' => 'autogen/mysite.nginx.conf',
'conf/mysite.php-fpm.conf' => 'autogen/mysite.php-fpm.conf',
'conf/my.cnf' => 'autogen/my.cnf',
);
$environment = 'amazonec2';
if ($argc >= 2){
$environmentRequired = $argv[1];
$allowedVars = array(
'amazonec2',
'macports',
);
if (in_array($environmentRequired, $allowedVars) == true){
$environment = $environmentRequired;
}
}
else{
echo "Defaulting to [".$environment."] environment";
}
$config = getConfigForEnvironment($environment);
foreach($filesToGenerate as $inputFilename => $outputFilename){
generateConfigFile(PATH_TO_ROOT.$inputFilename, PATH_TO_ROOT.$outputFilename, $config);
}
function getConfigForEnvironment($environment){
$config = parse_ini_file(PATH_TO_ROOT."conf/deployConfig.ini", TRUE);
$configWithMarkers = array();
foreach($config[$environment] as $key => $value){
$configWithMarkers['%'.$key.'%'] = $value;
}
return $configWithMarkers;
}
function generateConfigFile($inputFilename, $outputFilename, $config){
$lines = file($inputFilename);
if($lines === FALSE){
echo "Failed to read [".$inputFilename."] for reading.";
exit(-1);
}
$fileHandle = fopen($outputFilename, "w");
if($fileHandle === FALSE){
echo "Failed to read [".$outputFilename."] for writing.";
exit(-1);
}
$search = array_keys($config);
$replace = array_values($config);
foreach($lines as $line){
$line = str_replace($search, $replace, $line);
fwrite($fileHandle, $line);
}
fclose($fileHandle);
}
?>
然后 deployConfig.ini 看起来像:
And then deployConfig.ini looks something like:
[global]
;global variables go here.
[amazonec2]
nginx.log.directory = /var/log/nginx
nginx.root.directory = /usr/share/nginx
nginx.conf.directory = /etc/nginx
nginx.run.directory = /var/run
nginx.user = nginx
[macports]
nginx.log.directory = /opt/local/var/log/nginx
nginx.root.directory = /opt/local/share/nginx
nginx.conf.directory = /opt/local/etc/nginx
nginx.run.directory = /opt/local/var/run
nginx.user = _www
这篇关于在 Nginx 位置规则中使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!