Modelos e variáveis de configuração do site Nginx

3

Oi eu estou olhando para configurar uma configuração nginx simples, eu li que você pode definir variáveis usando set $variable content; mas até agora eu não tive sorte ...

Abaixo está o que eu tenho feito até agora / o que eu estou tentando alcançar:

server {

##################################################
# Set variables
set $port               80;                        # e.g. 80 or 443;
set $domain         domain.com *.domain.com;   # e.g. www.domain.com or just domain.com
set $subdomain          false;                     # e.g. subdomain in subdomain.domain.com or false
set type               live;                      # live, dev or stage

##################################################
# Include /web/sites configuration template
include /etc/nginx/site-config-template;

}

Aqui está o conteúdo de / etc / nginx / site-config-template:

##################################################
# If $subdomain is not false at slash
if ( $subdomain ) {
    set $subdomain "{$subdomain}/";
}

##################################################
# Define server main variables
listen          $port;
server_name     $domain;
root            /web/sites/$domain/$subdomain$type/public_html/current/;
index           index.php index.html;

##################################################
# Logs
access_log  /web/sites/$domain/$subdomain$type/logs/access.log;
error_log   /web/sites/$domain/$subdomain$type/logs/error.log;

##################################################
# push all to index.php
location / {
    try_files $uri $uri/ /index.php$args;
}

##################################################
# pass php files to fastcgi
location ~ \.php$ {
    try_files $uri =404;
    include fastcgi_params;
    fastcgi_param SITE_TYPE $type;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
}

Não sou capaz de definir variáveis dessa maneira em arquivos de configuração ou estou apenas fazendo algo terrivelmente errado?

    
por Alexander Holman 13.05.2015 / 15:12

2 respostas

6

O FAQ do nginx é bastante claro sobre este assunto:

Q: Is there a proper way to use nginx variables to make sections of the configuration shorter, using them as macros for making parts of configuration work as templates?

A: Variables should not be used as template macros. Variables are evaluated in the run-time during the processing of each request, so they are rather costly compared to plain static configuration. Using variables to store static strings is also a bad idea. Instead, a macro expansion and "include" directives should be used to generate configs more easily and it can be done with the external tools, e.g. sed + make or any other common template mechanism.

Construir uma configuração usando ferramentas externas é o caminho a percorrer. Eu usei m4 + make . Eu uso um script Python personalizado em um dos meus projetos. As escolhas são muitas.

    
por 13.05.2015 / 18:23
2

Resposta simples basta usar as configurações do servidor estático em uma base por domínio, não tente ser inteligente. Eu configurei um script para gerar o arquivo de configuração.

    
por 13.05.2015 / 17:33