Rewriting paths in CSS and JS via nginx sub_filter

I am having a problem with nginx sub_filterrewrite rules not working with CSS files. I serve the content along the path ( /site) and must have the correct prefixes for all URLs in JS and CSS.

I pointed out the mime type when binding in CSS. From the template:

<link href="/static/css/site.css" type="text/css" rel="stylesheet" />

nginx has helper filters, and I explicitly indicated to enable text/css:

location /site {
    access_log /opt/logs/test/nginx-access-site.log combined if=$loggable;
    error_log  /opt/logs/test/nginx-errors-site.log error;

    rewrite ^(/site)$ $1/;
    rewrite ^/site(.+) $1 break;

    sub_filter "test.domain.tld" "test.domain.tld/site";
    sub_filter "'/" "'/site/";
    sub_filter '"/' '"/site/';
    sub_filter "http:" "https:";
    sub_filter_types text/html text/css text/javascript;
    sub_filter_once off;

    include proxy_params;
    proxy_pass http://site_test$1$is_args$args;
    proxy_redirect http://test.domain.tld/ https://test.domain.tld/site/;
}

The link to the CSS file is being rewritten correctly. From HTML output:

<link href="/site/static/css/site.css" type="text/css" rel="stylesheet" />

The problem is that it doesn't overwrite in the CSS file, so the image paths are wrong:

.sortable th .asc {
  background-image: url("/static/img/up_arrow.gif");
}

I tried to be overly permissive without any differences:

    sub_filter_types *;

Did I misunderstand the use sub_filter? I assumed that since CSS was served directly by nginx, it would also be rewritten.

+4
source share

Source: https://habr.com/ru/post/1660891/


All Articles