9

Hi I am looking to set up a simple nginx config, I read you can set variables using set $variable content; but so far I've had no luck...

Below is what I have come up with so far/what I am trying to achieve:

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;

}

Here are the contents of /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;
}

Am I not able to set variables in this manner in config files or am I just doing something horribly wrong?

PersianGulf
  • 602
  • 8
  • 21
Alexander Holman
  • 245
  • 1
  • 2
  • 10
  • 3
    Instead (ab)using nginx `set`, I would prefer doing configuration template in CM (configuration management) tools like puppet, ansible, salt, chef, cfengine and friends. – masegaloeh May 13 '15 at 13:44
  • You doing all wrong. Neither `listen` nor `server_name` accepts variables. Describe what you want to achive – Alexey Ten May 13 '15 at 14:25
  • @AlexeyTen server_name accepts variables I've used them when setting up wildcard domains configs. I was hoping that instead of having to create a full config for every new site I could just create a file containing the variables. I'll just create them full individual files for each domain – Alexander Holman May 13 '15 at 14:57
  • You're wrong. `server_name` accept only one variable (`$hostname`) for historical reasons. But it just accepts any string. So `server_name $var` actually expects header `Host: $var`. – Alexey Ten May 13 '15 at 14:59
  • Don't confuse regexp and variable – Alexey Ten May 13 '15 at 15:22
  • @AlexeyTen I'm sorry you're right, just checked and the variable $domain is being set via regex used to match the server_name. – Alexander Holman May 13 '15 at 15:30

2 Answers2

14

The nginx FAQ is pretty clear on this topic:

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.

Building a configuration using external tools is the way to go. I've used m4 + make. I use a custom Python script in one of my projects. The choices are plenty.

Louis
  • 526
  • 3
  • 12
  • 1
    The choices may be "plenty" for somebody who uses those tools a lot, but there seems to be a severe lack of online resources for this topic. I can't find a tutorial on this anywhere. – Phil Mar 24 '16 at 09:18
  • They aren't really "tools", they are languages. Pick the one you're most familiar with, and write a script to generate your nginx vhost with the variables you wish to use, such as domain, encryption usage etc. – Flo Schild Feb 13 '17 at 09:37
  • 4
    @Louis, Do you know what they mean by “macro expansion”? Is there some sort of macro functionality I missed? – Alex Quinn Oct 15 '18 at 14:22
5

Simple answer just use static server configs on a per domain basis, don't try to be smart. I set up a script to generate the config file instead.

Alexander Holman
  • 245
  • 1
  • 2
  • 10