在 Nginx sub_filter 中使用正则表达式

问题描述 投票:0回答:1

我试图在 nginx 代理中使用

sub_filter
模块时删除 HTML 内容中特定属性的内容。

这是我尝试删除其内容的属性的示例:

integrity="sha256-JcCQTfQJfXQCI9Pgu4C8DsPBm7t6k/3SvAXTFPsXuBk= sha512-l4H3xn9koLbrXcevxo6Hs0TnepFHVLGgQM9tUMgsYxLDsyfVzWen1sqgozMDp1hh7dF1aR3bhNTp+jjrEBTIIA=="

到目前为止,我已经尝试了不同的正则表达式变体,这些变体应该可以工作,但实际上在 nginx 中不起作用,例如这个:

sub_filter integrity=".*?" integrity="";

我的做法错了吗?我怎样才能在nginx中完成这个任务?

regex nginx
1个回答
1
投票

更新了

正则表达式模式完整性=“.*?”在 sub_filter 中无效。

相反,您可以使用通过

subs_filter
模块添加的
ngx_http_substitutions_filter_module
https://docs.nginx.com/nginx/admin-guide/dynamic-modules/http-substitutions-filter/

其内容为:

'integrity="[^"]*"' '' gi;

例如:

http {
    server {
        listen 80;
        server_name example.com;

        location / {
            subs_filter 'integrity="[^"]*"' '' gi;
            subs_filter_once off;
            subs_filter_types text/html; # for specific type
        }
    }
}
  • 如果使用 php fastcgi

    location ~ \.php$ {
    
           subs_filter 'integrity="[^"]*"' '' gi;
           subs_filter_once off;
           subs_filter_types text/html; # for specific type
    
          #--your php configs--
          include snippets/fastcgi-php.conf;
          fastcgi_pass php_upstream;      
          #fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }
    
© www.soinside.com 2019 - 2024. All rights reserved.