nginx config. I know that some content changes a bit during the day, but not during the…
- User
nginx config. I know that some content changes a bit during the day, but not during the night. How can I cache it for longer time in the night, but reduce the cache in the day?
Please avoid using lua (or similar) in nginx for this. It has to be purely declarative if possible.
- Assistant
To implement time-based caching behavior in NGINX without using Lua or any scripting, you're limited to what can be expressed declaratively in the configuration. NGINX does not natively support time-based conditional logic in its configuration files. However, you can achieve your goal by using a combination of:
- Multiple server blocks or location blocks with different caching rules.
- A cron job or external script that reloads NGINX with different configurations depending on the time of day.
Here’s a practical approach:
🧩 Strategy: Use Two Config Files and Reload NGINX via Cron
You can define two separate configuration snippets or files:
- One for daytime (e.g., 8 AM to 8 PM) with shorter cache duration.
- One for nighttime (e.g., 8 PM to 8 AM) with longer cache duration.
Then, use a cron job to switch between them by symlinking the appropriate config and reloading NGINX.
Step 1: Define Two Cache Configurations
Create two files:
/etc/nginx/conf.d/cache_day.conf
location /your-content/ { proxy_pass http://backend; proxy_cache my_cache; proxy_cache_valid 200 10m; proxy_cache_use_stale error timeout updating; }/etc/nginx/conf.d/cache_night.conf
location /your-content/ { proxy_pass http://backend; proxy_cache my_cache; proxy_cache_valid 200 2h; proxy_cache_use_stale error timeout updating; }Step 2: Use a Symlink to Point to the Active Config
Create a symlink that NGINX includes:
ln -s /etc/nginx/conf.d/cache_day.conf /etc/nginx/conf.d/cache_active.confIn your main nginx.conf or another included file:
include /etc/nginx/conf.d/cache_active.conf;Step 3: Set Up Cron Jobs to Switch Configs
Edit your crontab (e.g., with crontab -e) and add:
# Switch to night config at 8 PM 0 20 * * * ln -sf /etc/nginx/conf.d/cache_night.conf /etc/nginx/conf.d/cache_active.conf && nginx -s reload # Switch to day config at 8 AM 0 8 * * * ln -sf /etc/nginx/conf.d/cache_day.conf /etc/nginx/conf.d/cache_active.conf && nginx -s reloadThis way, NGINX will use different cache durations depending on the time of day, without requiring Lua or dynamic logic inside the config.
✅ Pros
- Purely declarative NGINX configuration.
- No Lua or 3rd-party modules required.
- Easy to understand and maintain.
⚠️ Cons
- Requires external cron job and file management.
- NGINX reloads twice a day (which is lightweight, but still a consideration).
If you’re using a configuration management tool (like Ansible, Puppet, etc.), you can automate this even more cleanly.
Let me know if you want a version that uses map or variables for more advanced routing!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.