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
Chris Montanaro
  • 830
  • 1
  • 7
  • 8
  • 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

1 Answers1

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)$"
rfelsburg
  • 767
  • 3
  • 7
  • 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 'http://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. http://mdoc.su/o/re_format.7 – cnst Jan 21 '14 at 23:03
  • @cnst First off, don't be an ass. Secondly, You don't use two brackets unless you're trying to match a null expression at the beginning/end. If you read the posix page you just posted, you'll see where it states under character classes: Within a bracket expression, the name of a character class enclosed in `[:' and `:]' stands for the list of all characters belonging to that class. – rfelsburg Jan 21 '14 at 23:41
  • @rfelsburg, go ahead, provide wrong solutions, and call people names who point out your mistakes. `echo 111 | sed -E "s#[[:digit:]]#a#g"` – cnst Jan 21 '14 at 23:45
  • Again, did you actually look at the documentation you provided? I don't know how it's my mistake, when the example I pulled at the bottom was word for word form the page you submitted. – rfelsburg Jan 22 '14 at 14:58
  • @rfelsburg: Those docs - and your quote - say that you can use e.g. `[:digit:]` **within a bracket expression** i.e. use it like `[[:digit:]]`. The outer set is the bracket-expression, the inner set is the character-class. – nickgrim Jun 02 '15 at 09:07