考虑一个我有舞台和制作环境的php应用程序。将有两个单独的配置文件,如:
阶段配置:
server {
root /var/www;
server_name stage.mysite.org;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
prod config:
server {
root /var/www;
server_name mysite.org;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
2个配置文件之间的唯一区别是server_name。现在,为了DRY的利益,我要做的是检查一个nginx配置文件,该文件由两个环境共享。
我意识到我可以在server_name中包含两个url,但这有点奇怪,因为我真的只需要每个环境一个。我可以以某种方式将server_name提取到config.json文件或包含正确URL的东西。像这样:
通用nginx配置:
server {
root /var/www;
server_name ${hostname};
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
stage.config.json
{
"hostname": "stage.mysite.org"
}
谢谢!