What attributes(headers, cookies) besides url does Nginx take into account when caching a request?
I was trying to find out this information in nginx/log/access.log file. I have found only information about what type of request had been made(GET) and its status(404, 200, etc.).
Here my nginx.conf file:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
# MY TEST SERVER
http {
# CACHE
proxy_cache_path "C:\data\cache" levels=1:2 keys_zone=all:32m;
server {
listen 80;
location / {
proxy_pass http://127.0.0.1:81/;
proxy_cache all;
proxy_cache_valid 404 502 503 1s;
proxy_cache_valid any 10s;
}
server {
listen 81;
location / {
root "C:\data\www";
}
location /images/ {
root "C:\data";
}
}
}
What does Nginx take into account when caching a request?
The full URL
The relevant config for what Nginx uses when caching a request is proxy_cache_key
So by default Nginx does not take into account any headers when caching a request, only the full url (with get Args)
How to customize the cache key
To customize the cache key only requires using the
proxy_cache_keydirective.To cache based on a cookie value, there's already an example in the documentation - but I suggest:
Why add more text to the key? Consider what happens if you use multiple cookies like so:
It avoids the possibility of a request with only
$cookie_groupcolliding with the cached content for something with the same value only in$cookie_user. It also makes it easier to understand what the files on disk contain, if you have the need to look.If you want to use an arbitrary header - just include the variable for that header e.g.: