加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > Nginx > 正文

Nginx站点配置模板和变量

发布时间:2020-12-13 21:08:33 所属栏目:Nginx 来源:网络整理
导读:嗨,我想设置一个简单的nginx配置,我读你可以使用set $variable content设置变量;但到目前为止,我没有运气 以下是我到目前为止所提出的/我想要实现的目标: server {################################################### Set variablesset $port 80; # e.g.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读