0

In my nginx config file I have this location block (modified for debugging purposes):

location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
    expires 1;
    log_not_found off;
    return 515 "Request URI is $request_uri. Caught you";
}

When I request this URI from the server: /img/signup/be0e1d34fabf2514d49db9baec09f858.png?w=198&h=198&s=9a7d22504c5ae176a936a0f18cccacba

sure enough, it matches and I get a 515 response with this body: Request URI is /img/signup/be0e1d34fabf2514d49db9baec09f858.png?w=198&h=198&s=9a7d22504c5ae176a936a0f18cccacba. Caught you

Why? The regex doesn't match, at least not intuitively (either it must end in a question mark followed by a series of digits, or in .png etc.), nor when I sense-checked it at Regex101.com.

mpavey
  • 393
  • 3
  • 6
  • 1
    nginx considers the URI in the location directive to be only the part before the question mark – Federico Sierra Oct 19 '17 at 19:54
  • Got it, thanks @FedericoSierra. Based on the [documentation for the `location` directive](http://nginx.org/en/docs/http/ngx_http_core_module.html#location) referring to the 'request URI' as the string to be compared against, I guessed it was equivalent to the `$request_uri` variable :-( Feel free to add this as an answer! – mpavey Oct 19 '17 at 20:25

1 Answers1

0

Nginx isn't actually matching against the section after the question mark. Those are the query args. Nginx is only seeing up until the end of the path (.png). Since that's the case, it matches just fine.