I have a Nginx proxy server for a Node.js service which reads multiple files and generates a response. I want Nginx to add basic caches for me:
upstream my_http_servers {
server 127.0.0.1:7001;
}
proxy_cache_path /var/cache/nginx-combo levels=1:2 keys_zone=my_cache:10m inactive=1w max_size=200m;
server {
listen 80;
proxy_cache my_cache;
location / {
# %%%%%%%%%%%%% SPECIAL LINE OF IGNORING %%%%%%%%%%%%%
# proxy_ignore_headers Expires Cache-Control Set-Cookie Vary;
# expires 24h;
proxy_cache_lock on;
proxy_cache_lock_timeout 10s;
proxy_cache_key $scheme://$host$uri$is_args$query_string;
proxy_cache_valid 200 1m;
proxy_cache_valid any 5s;
proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504 http_429;
proxy_read_timeout 10s;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Expose-Headers "X-Log, X-Reqid";
add_header Access-Control-Max-Age 2592000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
add_header X-Proxy-Cache $upstream_cache_status;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://my_http_servers;
}
}
I tried to pressing test it with wrk
and it always responded with X-Proxy-Cache: EXPIRED
or X-Proxy-Cache: MISS
, which caused heavy pressure on Node.js since there are too many requests missed the cache.
Then I also tested it on Chrome, and surprisingly found X-Proxy-Cache: HIT
was generated after a first response.
So I guessed the difference in the HTTP headers from wrk
/curl
and Chrome caused the difference. After some attempts I found Set-Cookie: ...
made the difference, and it could be so solved by proxy_ignore_headers Set-Cookie
to ensure Nginx has constant behavior on both sides.
And the question is why? Why Nginx handles requests with/without Set-Cookie: ...
differently? What's happening when Nginx is deciding whether or not to use cache?