您好我正在设置一个简单的nginxconfiguration,我读你可以设置variablesset $variable content; 但到目前为止,我没有运气…
以下是我到目前为止/我想要达到的目标:
server { ################################################## # Set variables set $port 80; # eg 80 or 443; set $domain domain.com *.domain.com; # eg www.domain.com or just domain.com set $subdomain false; # eg 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; }
我不能在configuration文件中以这种方式设置variables,或者我只是在做一些可怕的错误?
nginx常见问题解答在这个话题上非常明显:
问:是否有合适的方法使用nginxvariables来缩短configuration的各个部分,将它们作为用于将configuration的一部分作为模板工作的macros?
答: variables不应该被用作模板macros。 variables是在处理每个请求期间的运行时间中评估的,因此与简单的静态configuration相比,它们相当昂贵。 使用variables来存储静态string也是一个坏主意。 相反,应该使用macros扩展和“include”指令来更容易地生成configuration,并且可以使用外部工具(例如sed + make或任何其他通用模板机制)来完成configuration。
使用外部工具构buildconfiguration是一条路。 我用m4 + make 。 我在我的一个项目中使用自定义的Python脚本。 select很多。
简单的答案只是在每个域的基础上使用静态服务器configuration,不要试图变得聪明。 我设置了一个脚本来生成configuration文件。