0
0
Nginxdevops~5 mins

Expires directive in Nginx - Commands & Configuration

Choose your learning style9 modes available
Introduction
Websites often serve files like images, scripts, and stylesheets. The Expires directive helps tell browsers how long to keep these files before asking the server for new ones. This speeds up page loading and reduces server work.
When you want browsers to cache images so they load faster on repeat visits.
When you serve static files like CSS or JavaScript that rarely change.
When you want to reduce bandwidth by avoiding repeated downloads of the same files.
When you want to improve your website's speed and user experience.
When you want to control how long browsers keep files before checking for updates.
Config File - nginx.conf
nginx.conf
http {
    server {
        listen 80;
        server_name example.com;

        location /images/ {
            expires 30d;
        }

        location /css/ {
            expires 7d;
        }

        location /js/ {
            expires 1h;
        }
    }
}

This configuration sets caching times for different file types.

  • location /images/: Images are cached for 30 days.
  • location /css/: CSS files are cached for 7 days.
  • location /js/: JavaScript files are cached for 1 hour.

The expires directive tells browsers how long to keep these files before asking the server again.

Commands
Check the nginx configuration file for syntax errors before applying changes.
Terminal
sudo nginx -t
Expected OutputExpected
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Reload nginx to apply the new configuration without stopping the server.
Terminal
sudo systemctl reload nginx
Expected OutputExpected
No output (command runs silently)
Request headers for an image file to verify the Expires header is set correctly.
Terminal
curl -I http://example.com/images/sample.jpg
Expected OutputExpected
HTTP/1.1 200 OK Server: nginx Date: Wed, 05 Jun 2024 12:00:00 GMT Content-Type: image/jpeg Expires: Fri, 05 Jul 2024 12:00:00 GMT Cache-Control: max-age=2592000
Key Concept

If you remember nothing else from this pattern, remember: the Expires directive tells browsers how long to keep files before checking for updates, speeding up your website.

Common Mistakes
Setting expires time too short or too long without considering file update frequency.
If too short, browsers request files too often, slowing down the site. If too long, users may see outdated files.
Set expires times based on how often files change; longer for rarely changed files, shorter for frequently updated ones.
Not testing nginx configuration before reload.
Syntax errors can cause nginx to fail to reload, making the site unavailable.
Always run 'sudo nginx -t' to check config syntax before reloading.
Summary
Use the Expires directive in nginx to control browser caching times for different file types.
Check nginx configuration syntax with 'nginx -t' before reloading to avoid errors.
Reload nginx to apply changes and verify caching headers with curl.