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?
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?
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.
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.
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.