默认情况下, /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_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
不幸的是,这是将其插入到块的开头,而不是结尾。
答案 0 :(得分:0)
这可能对您适合GNU sed):
sed '/^http {/,/^}/!b;/^}/i\new content' file
使用范围将重点放在http
部分上,并在结束}
之前插入新内容。
也可以写成:
sed -e '/^http {/,/^}/{/^}/i\new content' -e '}' file