Nginx location regex is not matching

10,613

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)$"
Share:
10,613

Related videos on Youtube

Chris Montanaro
Author by

Chris Montanaro

Updated on September 18, 2022

Comments

  • Chris Montanaro
    Chris Montanaro 8 months

    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
    
    • Sergey
      Sergey over 9 years
      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.
  • Chris Montanaro
    Chris Montanaro over 9 years
    still not working without the square brackets, I am currently not using the info I am capturing
  • rfelsburg
    rfelsburg over 9 years
    Are you matching on the http string? And are you just looking for a string containing the ten digits and css/js ending?
  • Chris Montanaro
    Chris Montanaro over 9 years
    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
  • Congmin
    Congmin over 9 years
    @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
  • rfelsburg
    rfelsburg over 9 years
    @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.
  • Congmin
    Congmin over 9 years
    @rfelsburg, go ahead, provide wrong solutions, and call people names who point out your mistakes. echo 111 | sed -E "s#[[:digit:]]#a#g"
  • rfelsburg
    rfelsburg over 9 years
    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.
  • nickgrim
    nickgrim almost 8 years
    @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.