嗨,我想设置一个简单的Nginx配置,我读你可以使用set $variable content设置变量;但到目前为止,我没有运气……
以下是我到目前为止所提出的/我想要实现的目标:
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;
}
以下是/ 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;
}
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.