I have a Django website where new content gets posted every few seconds.
Authorized users on my website's home page land on /
, whereas unauthorized ones on /unauth
. /unauth
shows similar content as /
, but without any personal details. My website uses nginx (reverse proxy) with gunicorn as the upstream.
I'm trying to implement nginx microcaching on location/unauth
, but have so far been unsuccessful (nothing ever shows up in /var/cache/nginx
). Adding add_header X-Cache-Status $upstream_cache_status;
in the location/unauth
doesn't yield anything at all in the response. It's almost like the loction block is entirely being ignored(!).
Can you help me troubleshoot this? Let me know in case you want to see the entire nginx.conf.
I've added the following in my nginx config file:
#outside the server block
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=100m inactive=6m;
#inside the server block
location /unauth {
add_header X-Cache-Status $upstream_cache_status;
proxy_cache my_cache;
proxy_cache_lock on;
proxy_cache_valid 200 1s;
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
proxy_buffering on;
proxy_buffers 24 4k;
proxy_buffer_size 2k;
proxy_busy_buffers_size 8k;
try_files $uri @http_proxy_to_app;
}
The HTTP response header being produced from http://example.com/unauth/
is as follows:
Status: HTTP/1.1 200 OK
Server: nginx
Date: Sun, 05 Feb 2017 00:10:03 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: close
Vary: Accept-Encoding
Expires: Sun, 05 Feb 2017 00:10:13 GMT
Vary: Cookie
Last-Modified: Sun, 05 Feb 2017 00:10:03 GMT
Cache-Control: max-age=10
X-Frame-Options: SAMEORIGIN
Content-Encoding: gzip
The reason the cache wasn't working was because of the internal redirect to @http_proxy_to_app. The proxy_cache needs to be in the post redirect context.