Skyfall174
Skyfall174

Reputation: 33

Serve .min.css files first nginx

I need to configure nginx server. I have folder with css files there are files some_file.min.css but in html website ask for some_file.css. I need to redirect this request to *min.css and if minified file doesnt exist return normal .css file

location ~* \.(css)$ {
        root /usr/share/nginx/html;
        try_files $1.min.$2 $uri =404;
        expires 30d;
  }

This piece of code does not work.

Upvotes: 2

Views: 331

Answers (2)

Mike
Mike

Reputation: 1

Try this:

location ~* ^(.*)(?<!\.min)\.(js|css|html)$ {
    try_files $1.min.$2 $uri =404;
}

Upvotes: 0

Richard Smith
Richard Smith

Reputation: 49772

You need to split the URI into two parts and insert the .min. sequence in between. Your try_files statement is probably ok, but you have not captured the correct $1 and $2 variables.

You need to capture that part of the URI before the .css. For example:

location ~* ^(.*)\.css$ {
    root /usr/share/nginx/html;
    try_files $1.min.css $uri =404;
    expires 30d;
}

Upvotes: 2

Related Questions