12

I'm having a problem with nginx's sub_filter rewrite rules not working with CSS files. I'm serving content on a path (/site) and need all URLs in JS & CSS to be prefixed correctly.

I've specified the mime type when linking in the CSS. From the template:

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

nginx has sub filters enabled and I've explicitly specified to include 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 reference to the CSS file is rewritten correctly. From the HTML output:

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

The issue is it's not rewriting within the CSS file, so image paths are incorrect:

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

I've tried being overly permissive without any difference:

    sub_filter_types *;

Have I misunderstood the use of sub_filter? I assumed that because the CSS was being served directly by nginx it would also be rewritten.

1 Answer 1

18

I found the solution after some searching and wasted some time trying some that didn't work so hopefully this helps someone else searching and found this post.

Apparently, by default sub_filter works only for text/html. I tried various other options to enable for text/javascript text/css like this, but which didn't work:

sub_filter_types text/xml text/css text/javascript;

Finally got it working by filtering all types like this:

sub_filter_once off;
sub_filter_types *;

Remember to restart nginx and remember to clear cache on your browser.

Sign up to request clarification or add additional context in comments.

3 Comments

Most of javascript files MIME type aren't text/javascript but application/javascript, that's probably why it wasn't working.
i also wasted lot of time to figure it out. Thanks @85noob
And just remembering, GZIP must be off to work with Sub Filter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.