<p>Craft rewrites the location of those files using a query string, which is what's causing nginx to lose track of them. Using the <a href="http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files"><code>try_files</code></a> directive should force nginx to find and serve them properly.</p>

<pre><code>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";
}
</code></pre>

CC BY-SA 3.0
2016-07-25T17:56:48.930Z
Thank you, extremely helpful. This should be in the Craft docs.
Adam Menczykowski
CC BY-SA 3.0
2014-10-11T12:19:15.387Z
2014-10-11T13:05:44.677Z
2014-10-11T13:05:44.677Z
Kevin Ouellette
<p>When I add this to my Nginx Confirguration file:</p>

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

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

<p>Without that in my configuration, for example, settings.css is OK:</p>

<pre><code>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
</code></pre>

<p>And then with the code, the settings.css is 404 Not Found</p>

<pre><code>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
</code></pre>

<cache><server><nginx>
Adding cache headers to Nginx configuation file breaks the /admin area
# Adding cache headers to Nginx configuation file breaks the /admin area

(Asked by: username_0 on 2014-10-11)

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

**Tags:** cache, server, nginx


---

## 1 answers

### Answer by username_1 on 2014-10-11. Score: 9 | Accepted answer

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";
}

> **Comments:**
> **username_2**: Thank you, extremely helpful. This should be in the Craft docs.  

---

