Nginx站点配置模板和变量

前端之家收集整理的这篇文章主要介绍了Nginx站点配置模板和变量前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

嗨,我想设置一个简单的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脚本.选择很多.

猜你在找的Nginx相关文章