Uwsgi_cache only caching root location

Hello all -

I’m having an issue where nginx is only caching homepage requests. If i
send requests to my server, the HTML at the homepage is saved, but
requests
to any URI otherwise do not save in the cache and upstream_cache_status
returns with a MISS.

How can I fix my config so that requests other than / will cache
properly?

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:

On Sat, Aug 15, 2015 at 02:50:13PM -0400, daveyfx wrote:

Hi there,

I’m having an issue where nginx is only caching homepage requests. If i
send requests to my server, the HTML at the homepage is saved, but requests
to any URI otherwise do not save in the cache and upstream_cache_status
returns with a MISS.

How can I fix my config so that requests other than / will cache properly?

http://nginx.org/r/uwsgi_cache_valid

What in the full response from the uwsgi server for the homepage
request? What is the full response for one other request?

“tcpdump” or otherwise snoop the traffic between nginx and uwsgi. Or
perhaps check the nginx debug log.

f

Francis D. [email protected]

Hi Francis -

In the uwsgi logs for my Python application, I see in the request logs:

[pid: 15085|app: 0|req: 25/81] 38.103.38.200 () {32 vars in 503 bytes}
[Sat
Aug 15 22:22:08 2015] GET /congress?mref=nav => generated 108292 bytes
in 66
msecs (HTTP/1.1 200) 4 headers in 166 bytes (3 switches on core 0)

I have my apps servers on the upstream configured to use uwsgi protocol
(for
use with uwsgi pass in nginx) and decided to see what would happen if I
switched to http for the app, and configure nginx to use proxy_
directives
in lieu of all the uwsgi_ directives. Strangely, this still did not fix
the
caching issue.

Here’s the output of the tcpdump on my apps node, still with http
protocol
used instead of uwsgi.

GET /congress?mref=nav HTTP/1.1
Host: origin.xxxx.com
X-Real-IP: xxx.xxx.xxx.xxx
X-Forwarded-For: xxx.xxx.xxx.xxx
Authorization: Basic REZQMTphZHRlc3Q=
User-Agent: curl/7.15.5 (i686-redhat-linux-gnu) libcurl/7.15.5
OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5
Accept: /
HTTP/1.1 200 OK
X-Frame-Options: SAMEORIGIN
Content-Type: text/html; charset=utf-8
Set-Cookie: obj_id=1732; Path=/
Set-Cookie: landing_type=pub_chan; Path=/

Just to test, I even set proxy_cache_valid to “any” and still only the
home
page will cache. Also commented out the proxy_no_cache directive in my
location / block and strangely, the issue persists.

Thank you,
Dave

Posted at Nginx Forum:

Realized you asked for the results of both requests.

Here’s the curl -svo /dev/null output from:

Home page:

GET / HTTP/1.1
User-Agent: curl/7.15.5 (i686-redhat-linux-gnu) libcurl/7.15.5
OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5
Host: apps01.njprod.amc:9001
Accept: /

< HTTP/1.1 200 OK
< X-Frame-Options: SAMEORIGIN
< Content-Type: text/html; charset=utf-8

And a page that will not cache:

GET /congress?mref=nav HTTP/1.1
User-Agent: curl/7.15.5 (i686-redhat-linux-gnu) libcurl/7.15.5
OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5
Host: apps01.njprod.amc:9001
Accept: /

< HTTP/1.1 200 OK
< X-Frame-Options: SAMEORIGIN
< Content-Type: text/html; charset=utf-8
< Set-Cookie: obj_id=1732; Path=/
< Set-Cookie: landing_type=pub_chan; Path=/

Thank you,
Dave

Posted at Nginx Forum:

On Sun, Aug 16, 2015 at 01:16:21AM -0400, daveyfx wrote:

Hi there,

uwsgi_ignore_headers Set-Cookie;

This solved my issue. The header is not being sent on the home page, but is
sent with almost all other pages.

Yes, that’s the reason in this case.

“”"
If the header includes the “Set-Cookie” field, such a response will
not be cached.

Processing of one or more of these response header fields can be
disabled
using the uwsgi_ignore_headers directive.
“”"

If “Set-Cookie” were cached, then everyone who fetches from the cache
would be invited to set the same cookie, which probably defeats the
purpose of cookies.

Another way to achieve the same would be to turn off the “Set-Cookie”
on the upstream, for all pages that don’t need to do it. (Possibly
the upstream is running unnecessary code to check for the existence
if a cookie; and if not, then create a new unique cookie for future
comparison.)

You’re effectively doing this now, with the nginx config; but this nginx
method doesn’t (trivially) allow you to get a Set-Cookie to the browser
(and avoid caching) for the few pages which might actually need it.

Thanks for the tips.

You’re welcome.

Good that you found an answer, and thanks for sharing it.

Cheers,

f

Francis D. [email protected]

uwsgi_ignore_headers Set-Cookie;

This solved my issue. The header is not being sent on the home page,
but is
sent with almost all other pages.

Thanks for the tips.

Posted at Nginx Forum: