2

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.

4

1 回答 1

2

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;
}
于 2018-08-27T10:17:45.270 回答