# Uwsgi\_cache only caching root location

**URL:** <https://www.ruby-forum.com/t/uwsgi-cache-only-caching-root-location/243021>\
**Category:** NGINX\
**Created:** [August 15, 2015, 8:50pm UTC](https://www.ruby-forum.com/t/uwsgi-cache-only-caching-root-location/243021 "2015-08-15T20:50:44Z")\
**Posts on this page:** 6\
**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 15, 2015, 8:50pm UTC](https://www.ruby-forum.com/t/uwsgi-cache-only-caching-root-location/243021/1 "2015-08-15T20:50:44Z")

</div>

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:

> **[uwsgi\_cache only caching root location](https://forum.nginx.org/read.php?2,261033,261033#msg-261033)**
>
> 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...

---

<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, 11:23pm UTC](https://www.ruby-forum.com/t/uwsgi-cache-only-caching-root-location/243021/2 "2015-08-15T23:23:12Z")

</div>

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?

> **[Module ngx\_http\_uwsgi\_module](http://nginx.org/en/docs/http/ngx_http_uwsgi_module.html#uwsgi_cache_valid)**
>
> The ngx\_http\_uwsgi\_module module allows passing requests to a uwsgi server.

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. [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 16, 2015, 1:23am UTC](https://www.ruby-forum.com/t/uwsgi-cache-only-caching-root-location/243021/3 "2015-08-16T01:23:32Z")

</div>

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](http://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:

> **[Re: uwsgi\_cache only caching root location](https://forum.nginx.org/read.php?2,261033,261035#msg-261035)**
>
> 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...

---

<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 16, 2015, 3:16am UTC](https://www.ruby-forum.com/t/uwsgi-cache-only-caching-root-location/243021/4 "2015-08-16T03:16:51Z")

</div>

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:  
[http://forum.nginx.org/read.php?2,261033,261036#msg-261036](http://forum.nginx.org/read.php?2,261033,261036#msg-261036)

---

<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 16, 2015, 8:55am UTC](https://www.ruby-forum.com/t/uwsgi-cache-only-caching-root-location/243021/5 "2015-08-16T08:55:06Z")

</div>

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. [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 16, 2015, 7:16am UTC](https://www.ruby-forum.com/t/uwsgi-cache-only-caching-root-location/243021/6 "2015-08-16T07:16:58Z")

</div>

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:

> **[Re: uwsgi\_cache only caching root location](https://forum.nginx.org/read.php?2,261033,261037#msg-261037)**
>
> 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...
