Apache2 mod_expires not working
Asked Answered
L

3

10

I have recently deployed a website on a VPS that supports Apache2, and was working on performance improvement suggestions by YSlow. Among those improvements were using mod_deflate and mod_expires, and since I have root access, I can directly edit the Apache config files.

mod_deflate is now working, but I am having issues with mod_expires so here is what I have done:

enable expires module and restart the server

a2enmod expires
service apache2 restart

create a new file "expires.conf" under mods_enabled that contains the following:

<IfModule mod_expires.c>
  # Enable expirations
  ExpiresActive On

  # Default directive
  ExpiresDefault "access plus 1 month"

  # My favicon
  ExpiresByType image/x-icon "access plus 1 year"

  # Images
  ExpiresByType image/gif "access plus 1 month"
  ExpiresByType image/png "access plus 1 month"
  ExpiresByType image/jpg "access plus 1 month"
  ExpiresByType image/jpeg "access plus 1 month"

  # CSS
  ExpiresByType text/css "access plus 1 month"

  # Javascript
  ExpiresByType application/javascript "access plus 1 year"
</IfModule>

However, when checking on various browsers Dev Tools under Network, the files show a Cache Control of None.

I thought of using my mod_expires script above in a .htaccess located in the root directory of my website: /var/www/sitename/public_html, since settings in the .htaccess override any prior settings made in the apache config files.

For that, i enabled the use of htaccess in apache2.conf.

I tested that my htaccess file was indeed working by willingly introducing an error in it (commented out the IfModule open tag). Refreshing the browser gave me a 500 server error. So the htaccess file was being processed.

Unfortunately, the browser dev tools still showed no cache control, and I am running out of ideas.

Am I missing something? Thanks!

Landman answered 28/10, 2015 at 21:33 Comment(1)
Obvious question but I presume you have included mod_expiries include line (often commented out) as otherwise that IfModule statement will fail and none of the config will be read.Sargasso
L
0

I finally fixed the issue:

  • firstly i noticed a difference between all the other config files and the one i had created under mods-enabled: the others were symlinks to their respective files under mods-available. This shouldn't be an issue but definitely better stick to the convention.

  • It seems I may not have restarted the apache2 service after creating the file

Landman answered 29/10, 2015 at 0:15 Comment(0)
G
2

In my case, I had to:

  1. Move the expires.conf to /etc/apache2/conf-available/ . I think this would be necessary to load the configs after the module.
mv /etc/apache2/mods-available/expires.conf /etc/apache2/conf-available/
  1. Also I had forgotten to enable the config before restarting apache.
a2enconf expires
service apache2 reload
Godavari answered 7/6, 2022 at 8:9 Comment(0)
I
0

Most likely, your expires.conf is getting parsed before the expires module is loaded, as you have included the configuration file in the mods-enabled folder.

Place your config file in conf.d and restart Apache.

Isobaric answered 28/10, 2015 at 22:10 Comment(1)
thanks for the suggestion, but the apache2.conf does something along the lines of: 1) process all files matching /mods-enabled/*.load. 2) process all files matching /mods-enabled/*.conf. which would ensure that the module is loaded before parsing the config file. Is that correct? Wouldn't the .htaccess hack I tried resolve this too?Landman
L
0

I finally fixed the issue:

  • firstly i noticed a difference between all the other config files and the one i had created under mods-enabled: the others were symlinks to their respective files under mods-available. This shouldn't be an issue but definitely better stick to the convention.

  • It seems I may not have restarted the apache2 service after creating the file

Landman answered 29/10, 2015 at 0:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.