Set up Nginx caching for static assets and API responses
✓Works with OpenClaudeYou are a DevOps engineer or backend developer. The user wants to configure Nginx caching for static assets and API responses to improve application performance and reduce server load.
What to check first
- Run
nginx -vto confirm Nginx is installed and check version - Check
/etc/nginx/nginx.confexists and Nginx process is running withsystemctl status nginx - Verify disk space available for cache with
df -h /var/cache(default cache location)
Steps
- Create a dedicated cache directory with
mkdir -p /var/cache/nginxand set permissions withchown -R nginx:nginx /var/cache/nginx && chmod 700 /var/cache/nginx - Define cache zones in the
httpblock of/etc/nginx/nginx.confusingproxy_cache_pathdirective with size, levels, and inactive time parameters - Configure cache key using
proxy_cache_keyto include scheme, host, request_uri, and custom variables like$request_method - Set cache validity periods with
proxy_cache_validfor different HTTP status codes (200, 301, 404, etc.) with separate TTLs - Add
proxy_cachedirective in server/location blocks to activate caching for specific routes - Configure cache control headers with
proxy_ignore_headersandproxy_pass_headerto respect or override upstream Cache-Control directives - Enable cache bypass and purging with
proxy_cache_bypassconditions and optional purge module for manual invalidation - Test cache behavior with
curl -iheaders checkingX-Cache-StatusorAgeresponse headers, then reload Nginx withnginx -s reload
Code
# /etc/nginx/nginx.conf - http block
http {
# Define cache zones for static assets and API responses
proxy_cache_path /var/cache/nginx/static levels=1:2 keys_zone=static_cache:10m max_size=1g inactive=60m use_temp_path=off;
proxy_cache_path /var/cache/nginx/api levels=1:2 keys_zone=api_cache:50m max_size=5g inactive=10m use_temp_path=off;
# Cache key configuration
proxy_cache_key "$scheme$request_method$host$request_uri";
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
# Include other nginx settings
include /etc/nginx/mime.types;
default_type application/octet-stream;
server {
listen 80;
server_name example.com;
# Static assets caching (images, CSS, JS)
location ~* \.(jpg|jpeg|png|gif|ico|css|js|svg|woff|woff2|ttf|eot)$ {
proxy_pass http://backend;
proxy_cache static_cache;
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Networking Skills
Other Claude Code skills in the same category — free to download.
HTTP Client
Create configured HTTP client with interceptors
Retry Logic
Implement retry logic with exponential backoff
Circuit Breaker
Implement circuit breaker pattern
Request Queue
Queue and batch HTTP requests
Proxy Setup
Set up reverse proxy configuration
SSL Setup
Configure SSL/TLS certificates
DNS Setup
Configure DNS records
Load Balancer
Set up load balancing configuration
Want a Networking skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.