在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]宏扩展和"include"指令应与[sed + make]或任何其他常用模板机制一起使用." 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.
更新下面的代码仍然有效,但是我已经将它们全部包装到一个名为 Packagist 上使用> Configurator ,它可以从模板轻松生成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屋!