Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Regex within Nginx sub_filter

Tags:

regex

nginx

I am trying to delete the content of specific attribute within the HTML content while using sub_filter modul in nginx proxy.

This is an example of the attribute that I am trying to delete its content:

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

So far I've tried different variation of regular expresions which should work but actually don't work in nginx, for example this one:

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

Is my approach wrong? How can I achieve this task in nginx?

like image 816
Kingindanord Avatar asked Oct 30 '25 14:10

Kingindanord


1 Answers

Updated

Regex pattern integrity=".*?" is not a valid in sub_filter.

instead u can use subs_filter that add via ngx_http_substitutions_filter_module module https://docs.nginx.com/nginx/admin-guide/dynamic-modules/http-substitutions-filter/

its content would be:

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

eg:

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
        }
    }
}
  • if it use 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;
    }
    
like image 147
Fire Department Avatar answered Nov 04 '25 08:11

Fire Department