嗨,我想设置一个简单的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;
}
我不能在配置文件中以这种方式设置变量,或者我只是做了可怕的错误?
nginx FAQ在这个主题上非常明确:
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.
使用外部工具构建配置是可行的方法.我用了m4
制作.我在我的一个项目中使用自定义Python脚本.选择很多.