在Nginx位置规则中使用变量

时间:2013-03-14 18:08:08

标签: variables nginx webserver

在Nginx中,我正在尝试定义一个变量,它允许我为所有位置块配置子文件夹。我这样做了:

set $folder '/test';

location $folder/ {
   [...]
}

location $folder/something {
   [...]
}

不幸的是,这似乎不起作用。虽然Nginx没有抱怨语法,但在请求/test/时会返回404。如果我明确地写了文件夹,它就可以了。那么如何在位置块中使用变量呢?

4 个答案:

答案 0 :(得分:63)

你做不到。 Nginx并不真正支持配置文件中的变量,它的开发人员嘲笑要求添加此功能的所有人:

  与纯静态配置相比,“[变量]”相当昂贵。[A]宏扩展和“包含”指令应该与[例如] sed + make或任何其他常用模板机制一起使用。“ http://nginx.org/en/docs/faq/variables_in_config.html

您应该编写或下载一个允许您从占位符配置文件生成配置文件的小工具。

更新以下代码仍然有效,但我已将其全部包装到Configurator上名为Packagist的小型PHP程序/库中,这样可以轻松生成nginx / php-fpm等配置文件,来自模板和各种形式的配置数据。

e.g。我的nginx源配置文件如下所示:

location  / {
    try_files $uri /routing.php?$args;
    fastcgi_pass   unix:%phpfpm.socket%/php-fpm-www.sock;
    include       %mysite.root.directory%/conf/fastcgi.conf;
}

然后我有一个定义了变量的配置文件:

phpfpm.socket=/var/run/php-fpm.socket
mysite.root.directory=/home/mysite

然后我使用它生成实际的配置文件。看起来你是一个Python人,所以基于PHP的例子可能对你没有帮助,但对于其他使用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看起来像:

[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

答案 1 :(得分:7)

这已经晚了很多年,但是自从找到解决方案以来,我将其发布在这里。通过使用maps,可以执行所要求的操作:

map $http_host $variable_name {
    hostnames;

    default       /ap/;
    example.com   /api/;
    *.example.org /whatever/;
}

server {
    location $variable_name/test {
        proxy_pass $auth_proxy;
    }
}

如果您需要在多个服务器之间共享同一终结点,则还可以通过简单地将值设置为默认值来降低成本:

map "" $variable_name {
    default       /test/;
}

映射可用于基于字符串的内容初始化变量,并且可在http范围内使用,从而允许变量是全局的并且可在服务器之间共享。

答案 2 :(得分:2)

@ danack的PHP生成脚本的修改python版本。它生成所有文件&amp;位于build/内的文件夹到父目录,替换所有{{placeholder}}个匹配项。在运行脚本之前,您需要cd进入build/

文件结构

build/
-- (files/folders you want to generate)
-- build.py

sites-available/...
sites-enabled/...
nginx.conf
...

build.py

import os, re

# Configurations
target = os.path.join('.', '..')
variables = {
  'placeholder': 'your replacement here'
}


# Loop files
def loop(cb, subdir=''):
  dir = os.path.join('.', subdir);

  for name in os.listdir(dir):
    file = os.path.join(dir, name)
    newsubdir = os.path.join(subdir, name)

    if name == 'build.py': continue
    if os.path.isdir(file): loop(cb, newsubdir)
    else: cb(subdir, name)


# Update file
def replacer(subdir, name):
  dir  = os.path.join(target, subdir)
  file = os.path.join(dir, name)
  oldfile = os.path.join('.', subdir, name)

  with open(oldfile, "r") as fin:
    data = fin.read()

  for key, replacement in variables.iteritems():
    data = re.sub(r"{{\s*" + key + "\s*}}", replacement, data)

  if not os.path.exists(dir):
    os.makedirs(dir)

  with open(file, "w") as fout:
    fout.write(data)


# Start variable replacements.
loop(replacer)

答案 3 :(得分:0)

你可以做与你提议的相反的事情。

location (/test)/ {
   set $folder $1;
}

location (/test_/something {
   set $folder $1;
}