I am trying to learn caching using nginx to cache my NEXT APIs. This is my nginx.config
worker_processes 1;
events {
worker_connections 1024;
}
http {
proxy_cache_path /tmp/nginx/cache levels=1:2 keys_zone=portfolio:10m max_size=1g inactive=60m use_temp_path=off;
server {
listen 2300;
server_name localhost;
# Default route - no caching
location / {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
# API routes with caching
location /api/ {
proxy_pass http://localhost:3000/api/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
# Cache configuration
proxy_cache portfolio;
proxy_cache_methods GET HEAD;
proxy_cache_key "$request_uri";
proxy_cache_valid any 10m;
proxy_cache_lock on;
proxy_cache_lock_age 10s;
proxy_cache_lock_timeout 3s;
add_header X-Cache-Status $upstream_cache_status;
add_header Cache-Control "public, max-age=600";
}
}
}
It's caching properly and if i use curl the x-cache-status properly shows HIT after the first request but in browser although caching is working, that I can understand from the the response time and also no API call in NEXT app, the x-cache-status always shows MISS. Sometimes for some APIs it shows HIT but not consistent. How to solve it?