Unix SED-将字符串添加到nginx配置块的末尾
默认情况下,/etc/nginx/nginx.conf文件如下所示:Unix SED-将字符串添加到nginx配置块的末尾,unix,sed,Unix,Sed,默认情况下,/etc/nginx/nginx.conf文件如下所示: user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_ty
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
我想在http{}块的末尾添加一些内容,例如:
include /etc/nginx/sites-enabled/*.conf;
使其成为:
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*.conf;
}
重要的是,它不依赖于任何现有内容,它只应以右括号作为参考点
我目前有以下代码:
sed -i '/^http {/a\ include \/etc\/nginx\/sites\-enabled\/\*\.conf\;' /etc/nginx/nginx.conf
不幸的是,这是在块的开头插入它,而不是在块的结尾。这可能对您有用(GNU-sed): 使用范围将焦点放在
http
部分,并在结束}
之前插入新内容
也可以写成:
sed -e '/^http {/,/^}/{/^}/i\new content' -e '}' file
令人惊叹的!谢谢!:)
sed -e '/^http {/,/^}/{/^}/i\new content' -e '}' file