在我的大多数 nginx 配置文件中,我有以下几行:
location /resources {
autoindex off;
if ($request_filename ~* ^.+.tpl$) {
rewrite ^ /404.html;
}
try_files $uri /resources/index.php?$args;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2)$ {
expires 365d;
# add_header Cache-Control "public, no-transform";
add_header Cache-Control "no-cache, public, must-revalidate, proxy-revalidate";
}
location / {
# Redirect /foobar/ and /foobar/index.html to /foobar
rewrite ^(.+)/+$ $1 permanent;
rewrite ^(.+)/index.html$ $1 permanent;
if ($request_uri ~ ^/(.*)\.html$) {
return 302 /$1;
}
try_files $uri $uri.html $uri/index.html =404;
# try_files $uri $uri/ /index.php$is_args$args;
}
如果存在,我想删除以下块:
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2)$ {
expires 365d;
# add_header Cache-Control "public, no-transform";
add_header Cache-Control "no-cache, public, must-revalidate, proxy-revalidate";
}
使用 regex101.com 我想出了以下正则表达式来“匹配”该块:
/((location ~\* \\\.\(js\|css\|png\|jpg\|jpeg\|gif\|ico\|svg\|woff\|woff2\)\$ \{)([^}]*)(\}))/mg
regex101 代码生成器生成以下代码:
sed -E 's/((location ~\* \\\.\(js\|css\|png\|jpg\|jpeg\|gif\|ico\|svg\|woff\|woff2\)\$ \{)([^}]*)(\}))//mg;t;d' <<< " location /resources {
autoindex off;
if ($request_filename ~* ^.+.tpl$) {
rewrite ^ /404.html;
}
try_files $uri /resources/index.php?$args;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2)$ {
expires 365d;
# add_header Cache-Control \"public, no-transform\";
add_header Cache-Control \"no-cache, public, must-revalidate, proxy-revalidate\";
}
location / {
# Redirect /foobar/ and /foobar/index.html to /foobar
rewrite ^(.+)/+$ $1 permanent;
rewrite ^(.+)/index.html$ $1 permanent;
if ($request_uri ~ ^/(.*)\.html$) {
return 302 /$1;
}
try_files $uri $uri.html $uri/index.html =404;
# try_files $uri $uri/ /index.php$is_args$args;
}"
但它不会产生任何输出。
如果我想在给定的配置文件中删除该配置部分,我该如何实现这一点,比如说 name myhost.localhost.conf?
sed 版本:
sed (GNU sed) 4.2.2