5

The following has been working to cache css and js for me:

location ~ "^(.*)\.(min.)?(css|js)$" {
    expires max;
}

results:

$ curl -I http://mysite.com/test.css
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 16 Jan 2014 18:55:28 GMT
Content-Type: text/css
Content-Length: 19578
Last-Modified: Mon, 13 Jan 2014 18:54:53 GMT
Connection: keep-alive
Expires: Thu, 31 Dec 2037 23:55:55 GMT
Cache-Control: max-age=315360000
X-Backend: stage01
Accept-Ranges: bytes

I am trying to get versioning setup for my js / css using a 10 digit unix timestamp and am having issues getting a regex match with the following valid a regex.

location ~ "^(.*)([\d]{10})\.(min\.)?(css|js)$" {
    expires max;
}

results:

$ curl -I http://mysite.com/test_1234567890.css
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 16 Jan 2014 19:05:03 GMT
Content-Type: text/css
Content-Length: 19578
Last-Modified: Mon, 13 Jan 2014 18:54:53 GMT
Connection: keep-alive
X-Backend: stage01
Accept-Ranges: bytes
  • 1
    Try to run nginx in debug mode, you will certainly see reg_exp match or not, You also need provide us full nginx.conf, because some other rules can be triggered earlier. – Sergey Jan 20 '14 at 21:54
2

I would try it without the brackets around \d, brackets are normally used for either contiguous characters or numbers, your brackets say match either character '\' or 'd'

"^(.*)(\d{10})\.(min\.)?(css|js)$"

Also, are you specifically saving the group info that you're capturing inside the parens?

For clarity on the comments below.

Start simple.

"^.*[0-9]*\.css$"

"^.*[:digit:]*\.css$"

"^.*\d*\.css$"

Depending on which works, use that base for the digits and expand as below.

"^.*\d{10}\.(min\.)?(css|js)$"

If you're matching the http/https as well, you can start it with the http/s stuff below, remember to change to which ever digit notation worked.

"^http(s)?://.*[0-9]{10}\.(min\.)?(css|js)$"
  • still not working without the square brackets, I am currently not using the info I am capturing – Chris Montanaro Jan 16 '14 at 19:14
  • Are you matching on the http string? And are you just looking for a string containing the ten digits and css/js ending? – rfelsburg Jan 16 '14 at 19:16
  • Yse, this is a location block that is included in a server declaration. I am looking for a 10 digit number in the name ending in .css, .js, .min.css or .min.js – Chris Montanaro Jan 16 '14 at 19:19
  • If the 10 digits will always be before the ending, ie 1234567890.css then one of these should work "^http(s)?://.*\d{10}\.(css|js)$" "^http(s)?://.*[0-9]{10}\.(css|js)$" "^http(s)?://.*[:digit:]{10}\.(css|js)$" "^http(s)?://.*\d+\.(css|js)$" Should match. If you're having problems build from what you know works. For instance, with the above URL 'mysite.com/test_1234567890.css' Does '.*\d*\.css' Work? – rfelsburg Jan 16 '14 at 19:20
  • @rfelsburg, i'm not sure why you claim that [:digit:]* is going to match anything of interest, the docs say it should be more like [[:digit:]]*, if anything. mdoc.su/o/re_format.7 – cnst Jan 21 '14 at 23:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.