Nginx缓存设置未生效,求助排查问题原因
tst.txt Hey there, let's walk through the most common reasons your Nginx cache isn't kicking in even when you modify tst.txt—I've debugged tons of these issues, so here's where to start checking:
Missing or incorrect cache directives in your location block
You might have set upproxy_cache_path, but forgotten to enable caching in the target location withproxy_cache mycache;(replacemycachewith your keys_zone name). Also, double-checkproxy_cache_valid: if you set it to0orno-cache, Nginx won't store any cache. For example, a valid rule would look like:location /tst.txt { proxy_cache mycache; proxy_cache_valid 200 1h; # Cache 200 OK responses for 1 hour }Cache-control headers overriding Nginx settings
If the origin server sendsCache-Control: no-cache, no-storeor anExpiresheader set to a past timestamp, Nginx will respect these and skip caching. You can override this withproxy_ignore_headers Cache-Control Expires;if you want to force caching regardless of origin headers.Permission issues on the cache directory
Nginx runs under a specific user (usuallynginxorwww-data). If your cache directory is owned byrootand doesn't have read/write permissions for the Nginx user, it can't create cache files. Run these commands to verify:ls -ld /path/to/your/cache/dir # Check directory permissions ps aux | grep nginx # Find the user Nginx runs asFix it with
chown -R nginx:nginx /path/to/your/cache/dir(adjust user/group as needed).You didn't reload/restart Nginx after changing config
It's easy to forget—after modifying your cache settings, you need to apply them with:nginx -s reloadIf you skipped this, Nginx is still using the old config without caching enabled.
Problematic cache key configuration
If yourproxy_cache_keyincludes variable request data (like$http_cookieor$http_user_agent), every request might generate a unique cache key. That means even for the sametst.txt, Nginx stores multiple cache copies, making it look like caching isn't working. Stick to a simple key like$scheme$proxy_host$request_uriunless you need variation.Testing method is misleading
Your browser might be using its own local cache, so you're not actually hitting Nginx's cache. Test withcurland check response headers for clues:curl -I http://your-domain/tst.txtLook for
X-Cache: HIT(if you addedadd_header X-Cache $upstream_cache_status;to your config) or theAgeheader, which shows how long the cache has been stored.Cache path misconfiguration
Check yourproxy_cache_pathsettings:max_sizemight be full, so Nginx can't add new cache files.inactivemight be set too short (e.g.,1m), so cache gets purged almost immediately after being stored.levelsmight be incorrectly set, leading to Nginx failing to create the directory structure for cache files.
Location rule isn't matching your request
Maybe your location block (e.g.,location /tst.txt) is being overridden by a more specific or higher-priority location (like alocation ~* \.txt$that doesn't have caching enabled). Usenginx -Tto print your full config and verify which location is handling thetst.txtrequest.Nginx version compatibility issues
Older Nginx versions (pre-1.1.12) had limited support for some cache features. If you're running a really old version, upgrading might fix the problem. Check your version withnginx -v.
内容的提问来源于stack exchange,提问作者NarūnasK

