misunderstood regex location

dcaillibaud nginx-forum at nginx.us
Tue May 6 15:03:56 UTC 2014


I understand my mistake, thanks to both of you.

May I suggest to insist on this in
http://nginx.org/en/docs/http/ngx_http_core_module.html#location, with a
remark on the fact that ^~ is not usable with a regex, for example with

syntax: 	
location [ = | ^~ ] uri { ... }
location ~ | ~*  regexUri { ... }
location @name { ... }

and adding there Igor advice (repeated many times in this forum like in
http://forum.nginx.org/read.php?2,247529,247718 or
http://forum.nginx.org/read.php?2,174517,174534#msg-174534), that it's a
better practice to have a prefix location list at first level then put regex
location within

Reviewing all my server definitions, I'm starting to imagine what a
nightmare could be, with on the first level a mix of standard prefix
location, ^~ and ~ ^/ ;-)

I was also wondering wich has precedence for /images/foo.jpeg in these case,


1) (I guess D, because ^/ before prefix shortcut regex)

location ^~ /images/ {
    location ~* \.(gif|jpg|jpeg)$ {
      [ configuration D ]
    }
}

location ~* \.(gif|jpg|jpeg)$ {
    [ configuration E ]
}


2) (I guess E but I'm not so sure), this can also be a pitfall example (how
complicating things can lead to unexpected behaviour)

location /images/ {
    location ~* \.(gif|jpg|jpeg)$ {
      [ configuration D ]
    }
}

location ~* \.(gif|jpg|jpeg)$ {
    [ configuration E ]
}

2bis) this makes 2) understandable (more predictable)

location /images/ {
    location ~* \.(gif|jpg|jpeg)$ {
      [ configuration D ]
    }
    location ~* \.svg$ {
      [ configuration D2 ]
    }
}

location / {
  location ~* \.(gif|jpg|jpeg|svg|pdf)$ {
      // static stuff not in /images/ 
      [ configuration E ]
  }
}


PS: is there a way to preserve space in this forum to make code easier to
read ? (I tried <pre>, <code> and [code])

Posted at Nginx Forum: http://forum.nginx.org/read.php?2,249846,249868#msg-249868



More information about the nginx mailing list