httpd.conf 中的 sed 仅适用于一个

httpd.conf 中的 sed 仅适用于一个

我有一个包含以下虚拟主机的 httpd.conf 文件:

<VirtualHost *:443>
JkMount /soncn/* tomcat3
SSLProtocol -all +TLSv1
SSLCipherSuite RSA:!LOW:!EXP:!eNULL:!aNULL:!RC4
SSLEngine on
SSLCertificateFile bla
SSLCertificateKeyFile  bla
RewriteEngine On
RewriteOptions Inherit
ServerName dionisos
</VirtualHost>
SSLSessionCache  dbm:/opt/apache/logs/ssl_gcache_data
<VirtualHost *:8443>
SSLProtocol -all +TLSv1
SSLCipherSuite RSA:!LOW:!EXP:!eNULL:!aNULL:!RC4
SSLEngine on 
SSLCertificateFile bla
SSLCertificateKeyFile  bla
RewriteEngine On
RewriteOptions Inherit
ServerName dionisos
ProxyRequests Off
ProxyPreserveHost On
PerlModule CSA_Filter
<Proxy *>
Require all granted
</Proxy>
ProxyPass / ajp://162.237.187.150:8012/
ProxyPassReverse / ajp://162.237.185.150:8012/
ProxyTimeout 3600
</VirtualHost>
<VirtualHost *:8191>
SSLProtocol -all +TLSv1
SSLCipherSuite RSA:!LOW:!EXP:!eNULL:!aNULL:!RC4
SSLEngine on
SSLCertificateFile bla
SSLCertificateKeyFile  bla
RewriteEngine On
RewriteOptions Inherit
ServerName dionisos
ProxyRequests Off
ProxyPreserveHost On
PerlModule CSA_Filter
<Proxy *>
Require all granted
</Proxy>
ProxyPass / ajp://bla:8010/
ProxyPassReverse / ajp://bla:8010/
ProxyTimeout 3600
</VirtualHost>

我只想为 VirtualHost *:443 插入以下行:

<Proxy *>
Require all granted
</Proxy>
ProxyPass /App https://servername:8080/App/
ProxyPassReverse /App https://servername:8080/App/

我进行了很多尝试,但找不到合适的 sed :

cat httpd.conf|tr '\n' '^'|sed 's#\(<VirtualHost \*:443>.*ServerName dionisos\)#\1 \n<Proxy *>\nRequire all granted\n</Proxy>\nProxyPass /App https://servername:8080/App/\nProxyPassReverse /Wibox https://servername:8080/App/ \n#g'|tr '^' '\n' > httpd.conf.updated

答案1

使用 GNU sed 和 GNU bash:

将新行放入文件 (insert.txt) 中。

sed '/^<VirtualHost \*:443>/r insert.txt' httpd.conf

或者使用标准输入:

echo -e 'lines\nto\ninsert' | sed '/^<VirtualHost \*:443>/r /dev/stdin' httpd.conf

如果您想“就地”编辑 httpd.conf,请使用 sed 的选项-i

相关内容