Problem with uwsgi_no_cache

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:

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)

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

Look at $upstream_http_ variables (Module ngx_http_upstream_module)

   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. [email protected]

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: