问题描述
我有一个nginx服务器,它在一个文件中有大约30多个虚拟主机块。
所有虚拟主机块的运行方式如下:
I have a nginx server and it has around 30+ virtual host block in a single file.All the virtual host block go like that:
server
{
#concrete configuration items
server_name myserver.hostname.com;
#concrete configuration items
#and so on....
}
我的问题是如何将每个服务器块分割成一个由server_name值命名的文件?例如,上述服务器块保存到名为myserver.hostname.com.conf
的文件中,我想使用shell代码来完成此任务。
My question is how can I split every server block into a single file named by server_name value? for example the above server block save to a file named myserver.hostname.com.confI would like to use shell code to complete this task.
顺便说一句,我不确定让每个虚拟主机都有其配置文件是否是一个好主意。但是随着虚拟主机的增加,将它们全部粘贴到一个公共文件中变得很麻烦。
By the way, I not sure whether it is a good idea to make every virtual host has its config file.But I did thing as virtual host increasing it became a messy to stuck them all in a common file.
推荐答案
您可以使用 csplit
命令按上下文拆分文件:
You can use csplit
command to split file by context:
$ csplit input.conf '/^\s*server\s*$/' {*}
然后 mv
(重命名)这些文件为 server_name
来自内容:
Then mv
(rename) those files to server_name
from content:
$ for i in xx*; do mv $i `grep -oPm1 '(?<=server_name).+(?=;)' $i`; done
这篇关于如何使用Shell将Nginx虚拟主机(config)文件拆分为较小的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!