# Problem with uwsgi\_no\_cache

**URL:** <https://www.ruby-forum.com/t/problem-with-uwsgi-no-cache/242985>\
**Category:** NGINX\
**Created:** [August 13, 2015, 6:07am UTC](https://www.ruby-forum.com/t/problem-with-uwsgi-no-cache/242985 "2015-08-13T06:07:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafa\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/958977/32.png) [@Rafa\_F](https://www.ruby-forum.com/u/Rafa_F)\
**Post date:** [August 13, 2015, 6:07am UTC](https://www.ruby-forum.com/t/problem-with-uwsgi-no-cache/242985/1 "2015-08-13T06:07:40Z")

</div>

Hi all -

I’m attempting to exclude application/json data from storing in nginx’s  
cache. All other content types are OK to cache. I thought that the  
below  
config would work for me, but nginx is still caching everything that is  
proxying. What am I doing wrong?

## in http block

```
map $http_content_type $no_cache {
   default 0;
   "application/json" 1;
}

```

## in vhost block

```
location / {
   uwsgi_cache www;
   uwsgi_cache_valid 200 10m;
   uwsgi_cache_methods GET HEAD;
   uwsgi_cache_bypass $no_cache;
   uwsgi_no_cache $no_cache;
   add_header X-uWSGI-Cache $upstream_cache_status;
    include uwsgi_params;
    uwsgi_pass www;
}

```

I’ve tried a few other ways to set $no\_cache to 1 for json content.  
Tried  
the following in both the server block and the location / block.  
if ($http\_content\_type = “application/json”) {  
set $no\_cache 1;  
}

Here’s my build:  
nginx version: nginx/1.6.2  
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-3) (GCC)  
TLS SNI support enabled  
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx  
–conf-path=/etc/nginx/nginx.conf  
–error-log-path=/var/log/nginx/error.log  
–http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid  
–lock-path=/var/run/nginx.lock  
–http-client-body-temp-path=/var/cache/nginx/client\_temp  
–http-proxy-temp-path=/var/cache/nginx/proxy\_temp  
–http-fastcgi-temp-path=/var/cache/nginx/fastcgi\_temp  
–http-uwsgi-temp-path=/var/cache/nginx/uwsgi\_temp  
–http-scgi-temp-path=/var/cache/nginx/scgi\_temp --user=nginx  
–group=nginx  
–with-http\_ssl\_module --with-http\_geoip\_module  
–with-http\_realip\_module  
–with-http\_stub\_status\_module --with-file-aio --with-ipv6  
–without-http\_ssi\_module --without-http\_split\_clients\_module  
–without-http\_referer\_module --without-http\_scgi\_module  
–without-http\_browser\_module --without-mail\_pop3\_module  
–without-mail\_imap\_module --without-mail\_smtp\_module  
–add-module=/home/makerpm/rpmbuild/BUILD/nginx-1.6.2/mod/ngx\_http\_redis-0.3.7  
–add-module=/home/makerpm/rpmbuild/BUILD/nginx-1.6.2/mod/nginx-x-rid-header  
–add-module=/home/makerpm/rpmbuild/BUILD/nginx-1.6.2/mod/nginx-upload-module  
–add-module=/home/makerpm/rpmbuild/BUILD/nginx-1.6.2/mod/nginx-upload-progress-module-0.8.4  
–add-module=/home/makerpm/rpmbuild/BUILD/nginx-1.6.2/mod/echo-nginx-module-0.57  
–with-ld-opt=-luuid --with-http\_spdy\_module --with-cc-opt=‘-O2 -g -pipe  
-Wall -Wp,-D\_FORTIFY\_SOURCE=2 -fexceptions -fstack-protector  
–param=ssp-buffer-size=4 -m64 -mtune=generic’

Thanks for your help.

Posted at Nginx Forum:

> **[Problem with uwsgi\_no\_cache](https://forum.nginx.org/read.php?2,260977,260977#msg-260977)**
>
> Hi all - I'm attempting to exclude application/json data from storing in nginx's cache. All other content types are OK to cache. I thought that the below config would work for me, but nginx is still caching everything that is proxying. What am I...

---

<div class="post-metadata">

**Author:** ![daveyfx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b487fb/32.png) [@daveyfx](https://www.ruby-forum.com/u/daveyfx)\
**Post date:** [August 13, 2015, 9:47am UTC](https://www.ruby-forum.com/t/problem-with-uwsgi-no-cache/242985/2 "2015-08-13T09:47:54Z")

</div>

On Thu, Aug 13, 2015 at 12:06:50AM -0400, daveyfx wrote:

Hi there,

> I’m attempting to exclude application/json data from storing in nginx’s  
> cache. All other content types are OK to cache. I thought that the below  
> config would work for me, but nginx is still caching everything that is  
> proxying. What am I doing wrong?
> 
> ## in http block
> 
> ```
> map $http_content_type $no_cache {
> 
> ```

$http\_ variables refer to the request from the client to  
nginx. ([Module ngx\_http\_core\_module](http://nginx.org/r/$http_))

You want to care about the response from upstream to nginx.

Look at $upstream\_http\_ variables ([Module ngx\_http\_upstream\_module](http://nginx.org/r/$upstream_http_))

> ```
> uwsgi_cache_bypass $no_cache;
> 
> ```

That says “do not read the response from the cache if this is true”.

Since you can only sensibly decide that after the upstream response has  
been fetched, you probably do not want that here.

> ```
> uwsgi_no_cache $no_cache;
> 
> ```

That says “do not write to the cache if this is true”.

You do want that.

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![daveyfx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b487fb/32.png) [@daveyfx](https://www.ruby-forum.com/u/daveyfx)\
**Post date:** [August 15, 2015, 7:48pm UTC](https://www.ruby-forum.com/t/problem-with-uwsgi-no-cache/242985/3 "2015-08-15T19:48:48Z")

</div>

Thank you, Francis. For anyone wondering what my corrected  
configuration  
looks like, here it is.

All the JSON content returned by the upstream is now ignored and only  
caching HTML content as desired.

## cache zone config

uwsgi\_cache\_path /var/cache/nginx/files keys\_zone=www:10m inactive=10m;  
uwsgi\_cache\_key “$scheme$host$uri$is\_args$args”;

## in http block

map $upstream\_http\_content\_type $no\_cache {  
“application/json” 1;  
default 0;  
}

## in server block for my vhost

```
location / {
    uwsgi_cache www;
    uwsgi_cache_valid 200 10m;
    uwsgi_cache_methods GET HEAD;
    uwsgi_no_cache $no_cache;
    add_header X-uWSGI-Cache $upstream_cache_status;
    include uwsgi_params;
    uwsgi_pass www;
}

```

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