0
0
Nginxdevops~30 mins

Expires directive in Nginx - Mini Project: Build & Apply

Choose your learning style9 modes available
Using the Expires Directive in Nginx
📖 Scenario: You are managing a website and want to improve its loading speed by telling browsers to cache static files like images and stylesheets. This helps visitors load your site faster on repeat visits.
🎯 Goal: You will create an Nginx configuration that uses the expires directive to set caching times for different file types.
📋 What You'll Learn
Create a server block listening on port 80
Add a location block for /images/ to cache images for 30 days
Add a location block for /css/ to cache CSS files for 7 days
Add a location block for /js/ to cache JavaScript files for 7 days
Use the expires directive with exact values as specified
💡 Why This Matters
🌍 Real World
Web servers use caching headers to reduce load times and bandwidth by telling browsers how long to keep files.
💼 Career
DevOps engineers and system administrators often configure Nginx to optimize website performance and user experience.
Progress0 / 4 steps
1
Create the basic server block
Create an Nginx server block that listens on port 80 and serves files from /var/www/html using the root directive.
Nginx
Need a hint?

Use server {} block with listen 80; and root /var/www/html;.

2
Add caching for images
Inside the server block, add a location /images/ block that sets expires 30d; to cache images for 30 days.
Nginx
Need a hint?

Use location /images/ { expires 30d; } inside the server block.

3
Add caching for CSS and JavaScript
Add two more location blocks inside the server block: location /css/ and location /js/. Set expires 7d; in both to cache CSS and JavaScript files for 7 days.
Nginx
Need a hint?

Add location /css/ { expires 7d; } and location /js/ { expires 7d; } inside the server block.

4
Display the final Nginx configuration
Print the complete Nginx server block configuration you created.
Nginx
Need a hint?

Print the entire server block configuration exactly as written.