Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Cache-Control Headers with nginx
📖 Scenario: You are managing a website and want to control how browsers cache your files to improve loading speed and reduce server load.Using nginx, you will set Cache-Control headers to tell browsers how long to keep files before checking for updates.
🎯 Goal: Configure nginx to add Cache-Control headers with a max-age of 3600 seconds (1 hour) for static files.
📋 What You'll Learn
Create a basic nginx server block configuration
Add a variable to set the cache max-age time
Use the add_header directive to set Cache-Control header with max-age
Print the final nginx configuration snippet
💡 Why This Matters
🌍 Real World
Web servers use Cache-Control headers to tell browsers how long to keep files, improving speed and reducing server load.
💼 Career
DevOps engineers often configure nginx to optimize website performance and caching strategies.
Progress0 / 4 steps
1
Create a basic nginx server block
Create a basic nginx server block configuration with server listening on port 80 and serving files from /var/www/html using root directive.
Nginx
Hint
Use server { ... } block with listen 80; and root /var/www/html;.
2
Add a variable for cache max-age
Inside the server block, add a variable called cache_time and set it to 3600 (seconds).
Nginx
Hint
Use set $cache_time 3600; inside the server block.
3
Add Cache-Control header with max-age
Inside the server block, add an add_header directive to set the Cache-Control header with the value max-age=$cache_time.
Nginx
Hint
Use add_header Cache-Control "max-age=$cache_time"; inside the server block.
4
Print the final nginx configuration
Print the complete nginx configuration stored in the variable nginx_config.
Nginx
Hint
Use print(nginx_config) to display the configuration.
Practice
(1/5)
1. What is the purpose of the Cache-Control header in nginx?
easy
A. To set the server's IP address
B. To configure SSL certificates
C. To tell browsers how to cache files
D. To define the server's hostname
Solution
Step 1: Understand HTTP headers role
HTTP headers communicate instructions between server and browser.
Step 2: Identify Cache-Control header function
Cache-Control tells browsers how and when to cache content.
5. You want to configure nginx to prevent caching of API JSON responses but allow caching of static CSS files for 7 days. Which configuration is correct?