I'm using nginx to serve assets statically, and also route all non-asset requests to a PHP front controller. I have a location block that looks like:

location / {
    include /etc/nginx/mime.types;
    index index.php;
    try_files $request_uri $request_uri/ /index.php?$query_string;

From what I understand, this should first try to match the request against any known MIME types, and if no matching file is found, it should try the request against my PHP location block (not shown).

This works great if I want to visit http://example.com/css/owls.css, but if I try to visit http://example.com/css/owls.css?t=_423632636, it doesn't recognize the "extension" and so tries to pass the request to my PHP entry point (which of course, fails).

How can I tell nginx that a request for http://example.com/css/owls.css?t=_423632636 (or any other query string that follows a valid MIME type) should first try to match the static file css/owls.css?

If the file (without the query string) does not exist, then it should fall back to the PHP try_files command.


Nginx does this automatically for me, with the try_files line below. Note you're using $request_uri instead of $uri. Try changing that. first.

try_files $uri $uri/ /index.php?$args;

If that doesn't solve your problem please edit your question to include your entire nginx.conf and relevant server block.

  • Very good to know. So $uri is just $request_uri with the query string chopped off? – alexw Feb 16 '17 at 19:46
  • Yes nginx.org/en/docs/http/… – Tim Feb 16 '17 at 19:52

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.