# How to cache js/css request containing a question mark?

**URL:** <https://www.ruby-forum.com/t/how-to-cache-js-css-request-containing-a-question-mark/243194>\
**Category:** NGINX\
**Created:** [August 31, 2015, 6:00pm UTC](https://www.ruby-forum.com/t/how-to-cache-js-css-request-containing-a-question-mark/243194 "2015-08-31T18:00:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sadaf\_N](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@Sadaf\_N](https://www.ruby-forum.com/u/Sadaf_N)\
**Post date:** [August 31, 2015, 6:00pm UTC](https://www.ruby-forum.com/t/how-to-cache-js-css-request-containing-a-question-mark/243194/1 "2015-08-31T18:00:24Z")

</div>

Following nginx configuration in a server block can cache js and css  
files  
with “js” or “css” as the end of the url.

```
    location ~ .*\.(js|css)?$ {
            expires 7d;
            }

```

But it can’t cache request such as:

http: [//example.com/jquery.js?ver=1.11.3&build=1](https://example.com/jquery.js?ver=1.11.3&build=1)

or

http: [//example.com/style.css?ver=4.3&build=1](https://example.com/style.css?ver=4.3&build=1)

Any way to cache cs/jss files above? Or cache files based on the file  
type  
of request instead of file name?

Posted at Nginx Forum:

> **[How to cache js/css request containing a question mark?](https://forum.nginx.org/read.php?2,261338,261338#msg-261338)**
>
> Following nginx configuration in a server block can cache js and css files with "js" or "css" as the end of the url. location ~ .\*\\.(js|css)?$ { expires 7d; } But it can't cache request such as:

---

<div class="post-metadata">

**Author:** ![log](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e480ec/32.png) [@log](https://www.ruby-forum.com/u/log)\
**Post date:** [August 31, 2015, 6:57pm UTC](https://www.ruby-forum.com/t/how-to-cache-js-css-request-containing-a-question-mark/243194/2 "2015-08-31T18:57:39Z")

</div>

Please try to remove $ in the end of the expression:

something like this:

location ~ .\*.(js|css) {  
expires 7d;  
}

Also, make sure you are using args in the cache key:

proxy\_cache\_key “$host$uri$is\_args$args”;

Regards,  
Biazus

Posted at Nginx Forum:

> **[Re: How to cache js/css request containing a question mark?](https://forum.nginx.org/read.php?2,261338,261339#msg-261339)**
>
> Following nginx configuration in a server block can cache js and css files with "js" or "css" as the end of the url. location ~ .\*\\.(js|css)?$ { expires 7d; } But it can't cache request such as:

---

<div class="post-metadata">

**Author:** ![log](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e480ec/32.png) [@log](https://www.ruby-forum.com/u/log)\
**Post date:** [September 2, 2015, 5:18am UTC](https://www.ruby-forum.com/t/how-to-cache-js-css-request-containing-a-question-mark/243194/3 "2015-09-02T05:18:48Z")

</div>

Thank you, Biazus!

Posted at Nginx Forum:

> **[Re: How to cache js/css request containing a question mark?](https://forum.nginx.org/read.php?2,261338,261373#msg-261373)**
>
> Following nginx configuration in a server block can cache js and css files with "js" or "css" as the end of the url. location ~ .\*\\.(js|css)?$ { expires 7d; } But it can't cache request such as:

---

<div class="post-metadata">

**Author:** ![log](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e480ec/32.png) [@log](https://www.ruby-forum.com/u/log)\
**Post date:** [September 2, 2015, 8:40am UTC](https://www.ruby-forum.com/t/how-to-cache-js-css-request-containing-a-question-mark/243194/4 "2015-09-02T08:40:55Z")

</div>

Well, unfortunately this is not working…

[http://example.com/style.css?ver=4.3](http://example.com/style.css?ver=4.3)  
[http://example.com/jquery-migrate.min.js?ver=1.2.1](http://example.com/jquery-migrate.min.js?ver=1.2.1)

## biazus Wrote:

> proxy\_cache\_key “$host$uri$is\_args$args”;
> 
> Regards,  
> Biazus

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,261338,261374#msg-261374](http://forum.nginx.org/read.php?2,261338,261374#msg-261374)

---

<div class="post-metadata">

**Author:** ![log](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e480ec/32.png) [@log](https://www.ruby-forum.com/u/log)\
**Post date:** [September 2, 2015, 9:08am UTC](https://www.ruby-forum.com/t/how-to-cache-js-css-request-containing-a-question-mark/243194/5 "2015-09-02T09:08:23Z")

</div>

You will need to be a little bit more specific than ‘this is not  
working’  
to get some help.

> **[How To Ask Questions The Smart Way](http://www.catb.org/esr/faqs/smart-questions.html)**

## Btw, the documentation show the default value for proxy\_cache\_key if none is provided, and it already takes arguments (and thus arguments’ separator) into account: [Module ngx\_http\_proxy\_module](http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_key) [Module ngx\_http\_core\_module](http://nginx.org/en/docs/http/ngx_http_core_module.html#var_request_uri)

_B. R._
