How to add expires headers in the server?

by jacey.lubowitz , in category: SEO , 2 years ago

How to add expires headers in the server?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

3 answers

Member

by dortha , 2 years ago

@jacey.lubowitz 

Adding Expires headers to your server response can improve the speed and performance of your website by allowing the browser to cache certain resources. Here are the steps to add Expires headers in your server:

  1. Enable mod_expires module: If you're using Apache as your web server, you need to enable the mod_expires module. You can do this by running the following command on your server:
1
a2enmod expires


  1. Configure the Expires headers: Once the module is enabled, you need to configure the Expires headers. You can do this by adding the following code to your server configuration file:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType image/jpeg "access plus 1 year"
  ExpiresByType image/gif "access plus 1 year"
  ExpiresByType image/png "access plus 1 year"
  ExpiresByType text/css "access plus 1 month"
  ExpiresByType application/pdf "access plus 1 month"
  ExpiresByType text/javascript "access plus 1 month"
  ExpiresByType application/x-javascript "access plus 1 month"
  ExpiresDefault "access plus 2 days"
</IfModule>


This code sets different expiration times for different types of resources, such as images, CSS, JavaScript, etc.

  1. Test your configuration: After adding the Expires headers, you should test your configuration to make sure it's working as expected. You can use online tools such as GTmetrix or PageSpeed Insights to check whether the headers have been added correctly.


Note that adding Expires headers may not work in some cases, such as when you have dynamic content that changes frequently. In such cases, you may need to use other caching mechanisms, such as ETag or Last-Modified headers, to improve your website's performance.

Member

by dudley , a year ago

@jacey.lubowitz 

Please note that the steps provided assume that you have access to the server configuration file, and you have the necessary permissions to make changes. If you are using a shared hosting environment or a website builder, these steps may not be applicable. In such cases, you may need to check with your hosting provider or use a plugin or tool provided by your website builder to add Expires headers.

Member

by julio , a year ago

@jacey.lubowitz 

Additional information:


If you are using NGINX as your web server, you can add Expires headers by modifying your server configuration file. Here's how you can do it:

  1. Open your NGINX configuration file. The location of this file may vary depending on your server setup. Common locations include /etc/nginx/nginx.conf, /etc/nginx/sites-available/default, or /etc/nginx/conf.d/default.conf.
  2. Inside the server block, add the following code to enable Expires headers:
1
2
3
location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
    expires 1y;
}


This code sets the expires header for images (jpg, jpeg, png, gif, ico), CSS, and JS files to 1 year. You can adjust the expiration time by changing the value (e.g., 1m for 1 month, 1d for 1 day).

  1. Save the configuration file and restart NGINX for the changes to take effect. The command to restart NGINX may vary depending on your system, but common commands include service nginx restart or /etc/init.d/nginx restart.


After adding the Expires headers, remember to test your website to ensure the headers are being applied correctly. You can use browser developer tools or online tools like GTmetrix or PageSpeed Insights to check the response headers of your resources.