DEV Community

Kai's brain dump
Kai's brain dump

Posted on • Updated on • Originally published at braindumpk.substack.com

Nginx caching tips

Performance tips

  • Put the cache on a dedicated SSD/NVME disk. On an HDD, the performance hit caused by constant read/writes to disk of the cache manager will dwarf any caching benefits.
  • Don’t cache HTTP responses upon first hit. Use proxy_cache_min_uses 2; to only cache items that have been accessed more than once. This way, you will alleviate the proxy cache’s write pressure and prevent your cache from filling up with content that’s seldom accessed.
  • Avoid unexpected backend traffic spikes caused by cache misses:
    • Set proxy_cache_lock on; 
      • When there is a cache miss and multiple simultaneous requests hit the same URL, send a single request to your backend while the rest of the requests wait on the Nginx side.
    • Add proxy_cache_use_stale updating; to your Nginx config.
      • This flag instructs Nginx to keep serving stale cache content to users while the cache is being refreshed.
  • Save bandwidth by refreshing your Nginx cache with conditional GET requests, set proxy_cache_revalidate on; 
  • Cold start blues: when you cold start an Nginx instance with a big existing cache on disk, wait a few minutes before you expose it to the public. Nginx needs to scan the entire cache to load its metadata into memory.

Nginx proxy cache for Tomcat servers

Java web apps running on Apache Tomcat always set a JSESSIONID cookie in the HTTP response, even if your web visitor is not logged into any account. To prevent session IDs from leaking into your Nginx cache, you need to filter out any Set-Cookie response headers returned by your backend.

proxy_ignore_headers Set-Cookie;
proxy_hide_header Set-Cookie;
Enter fullscreen mode Exit fullscreen mode

How to bypass the cache for dynamic content and logged-in users

Your backend should set a nocache cookie in the HTTP responses for dynamic content. Then you include proxy_cache_bypass $cookie_nocache; in your Nginx configuration to bypass your Nginx cache for those requests.

Nginx proxy cache for multilingual sites: use this cache key

The default Nginx caching key doesn’t work well with sites with multiple subdomains. You can customize your cache key by setting proxy_cache_key. This one has worked well for me:

proxy_cache_key $scheme$host$uri$is_args$args;
Enter fullscreen mode Exit fullscreen mode

How to purge cache items

If your Nginx runs on a Debian server, I highly recommend you install the nginx-extras package from the Debian repos, instead of the nginx package from the official repos. The nginx-extras package comes with the ngx_cache_purge community module, whereas the nginx package from the official repos doesn’t.

(If you liked this, you might enjoy 6 Nginx configuration pitfalls that bit me in the ass)

Top comments (0)