5

When I add this to my Nginx Confirguration file:

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
    expires 7d;
    add_header Pragma "public";
    add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}

In the admin area I get all 404 responses on those assets (js, css, png, etc).

Without that in my configuration, for example, settings.css is OK:

Request URL:http://{site}admin/resources/css/settings.css?d=1411426522
Request Method:GET
Status Code:200 OK
Cache-Control:max-age=31536000
Connection:keep-alive
Expires:Sun, 11 Oct 2015 02:25:24 GMT
Last-Modified:Mon, 22 Sep 2014 22:55:22 GMT
Pragma:cache

And then with the code, the settings.css is 404 Not Found

Request URL:http://{site}/admin/resources/css/settings.css?d=1411426522
Request Method:GET
Status Code:404 Not Found
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:keep-alive
Expires:Sat, 04 Oct 2014 02:31:56 GMT
Pragma:no-cache
8

Craft rewrites the location of those files using a query string, which is what's causing nginx to lose track of them. Using the try_files directive should force nginx to find and serve them properly.

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
  try_files $uri /index.php?$query_string;
  expires 7d;
  add_header Pragma "public";
  add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
  • 1
    Thank you, extremely helpful. This should be in the Craft docs. – Adam Menczykowski Jul 25 '16 at 17:56

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.