I am using .htaccess to cache the js / css / etc files on the website. Everything works fine, but for some reason my pages get cached, too. I looked into the headers, and it works like this:
Browser requests a page http://poko.lt/up
Server responds with 301 to http://poko.lt/up/ (with Date and Last Modified headers equal to current time)
Browser requests http://poko.lt/up/
Server responds with the page, and with Date and Last Modified headers equal to my last forced refresh (like, yesterday), but Cache-control header is fine (max-age=0)
I get the old version of the page :/
My .htaccess looks like this:
# Turn on Expires and set default to 0
ExpiresActive On
ExpiresDefault A0
# 1 YEAR
<FilesMatch "\.(jpg|jpeg|png|gif|svg|eot|ttc|ttf|otf)$">
ExpiresDefault A29030400
Header append Cache-Control "public"
Header unset Last-Modified
Header unset ETag
FileETag None
</FilesMatch>
# 2 WEEKS
<FilesMatch "\.(css|js|swf)$">
ExpiresDefault A1209600
Header append Cache-Control "proxy-revalidate"
Header unset Last-Modified
Header unset ETag
FileETag None
</FilesMatch>
# NO CACHE
<FilesMatch "\/$">
ExpiresDefault A0
Header append Cache-Control "proxy-revalidate"
Header unset Last-Modified
Header unset ETag
FileETag None
</FilesMatch>
And, one can test it with the website http://poko.lt (the three red icons in the top middle of the page are the ones causing the problems). I am using FF4, and checking headers with Firebug.
Related
I have an elastic beanstalk server running an Apache proxy on Amazon Linux 2. I want to set the cache-control header on my index.html file to public, max-age=0.
In order to update my Apache config I understand I can add a config file to .platform/httpd/conf.d. In my first attempt I created this file:
<FilesMatch "index\.html">
Header set Cache-Control "public, max-age=0"
</FilesMatch>
This did not work. Looking around I think this is because the directives I have used are intended for Apache's htaccess file or within a <VirtualHost> section of the conf file.
Any ideas how I can get this working? Note I found this answer had some useful information.
EDIT: I also tried this in a conf file (it didn't set any cache-control headers).
<IfModule mod_expires.c>
# Turn on the module.
ExpiresActive on
# Set the default expiry times.
ExpiresDefault "access plus 2 days"
ExpiresByType text/html "access plus 600 seconds"
</IfModule>
EDIT: I also tried switching to nginx. I added a file .platform/nginx/conf.d/cache.conf, with the following contents
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 2d;
add_header Cache-Control "public, no-transform";
}
After deployment and a server restart, no cache-control headers were set on the files specified.
Do the cache control directives need to specify if a query string is present in the http request for proper matching?
Currently using query strings for static asset versioning, and disabled Etags for those - but the directives are not taking effect whatsoever. Looking at the headers, Etags are still used and there is no cache-control defined. Gmatrix and Lighthouse both state that static assets have no cache control set.
Example file requested: app.js?v=1.3.5
Here is the .htaccess file, followed by the troubleshooting steps
<IfModule mod_headers.c>
# One month for media files and icons
<FilesMatch "\.(ogg|mp3|ico|jpg|jpeg|png|svg|webp|webmanifest|xml)\?.*$">
Header set Cache-Control "max-age=2592000, public"
Header unset ETag
FileETag None
</FilesMatch>
# One week for CSS/JS files except service worker file
<FilesMatch "^(?!sw).+\.(css|json|js)\?.*$">
Header set Cache-Control "max-age=604800, public"
Header unset ETag
FileETag None
</FilesMatch>
# No cache for HTML files (checks with server for changes else serves cache)
<FilesMatch "\.(html)$">
Header set Cache-Control "no-cache, must-revalidate"
</FilesMatch>
<FilesMatch "^(sw\.js)$">
Header set Cache-Control "no-cache, no-store, must-revalidate"
</FilesMatch>
</IfModule>
Troubleshooting steps
Directives don't seem to affect html pages either so assuming this may be a config or syntax issue.
Check syntax with validator: https://www.lyxx.com/freestuff/002.html
Ensure AllowOverride is set to All in /etc/apache2/sites-available/mysite.conf
Try setting AllowOverride to All in /etc/apache2/apache2.conf for /var/www/
Try adding the directives directly in /etc/apache2/sites-available/mysite.conf
Restart apache after changes: service apache2 restart
The mod_headers module was not activated!
Use apachectl -M to check for active modules (Debian/Ubuntu).
If headers module not listed:
Use a2enmod headers to activate the headers module.
Restart apache2 with service apache2 restart to implement changes.
At this point was able to test whether the query string needs to be added in the FilesMatch expression.
Turns out the query string needs to be omitted from the expression.
Use: \.(js)$
Don't use: \.(js)\?.*$
I'm using apacher web server to stre the static content (like images, css etc.) with my website. Below is my apache configurations set as below :
<IfModule mod_expires.c>
ExpiresActive on
<FilesMatch "\.(ico|jpg|jpeg|png|gif|js|css|swf|html)$">
ExpiresActive On
ExpiresDefault "access plus 30 minutes"
</FilesMatch>
</IfModule>
<IfModule mod_headers.c>
<FilesMatch ".+\.(ico|jpg|jpeg|png|gif|js|css|swf|html|woff)$">
Header set X-XSS-Protection "1; mode=block"
Header set X-Content-Type-Options nosniff
</FilesMatch>
</IfModule>
Please note that the same apache configuration is working in test region, but not working in prod region for same jpeg images.
The cache works well for woff,css, js,png files, but doesnt work for my few jpeg images which are loaded from specific folder for one of the website screen. Any idea why?
Thanks in advance.
In our application we have both gif and jpg images for the same set of images. I've both gif and jpg images in my local server. Hence I opened all the below gif images in MSPaint and stored it as .jpg.
Next_disabled.jpg
Next.jpg
Prev_disabled.jpg
Prev.jpg
Then I've deployed these images to apache where static content is present, followed by apache restart. Then the issue was resolved and the images are showing properly.
I have different types of dynamic urls like
/dynamic/image/day/blah/blah/blah.jpg
/dynamic/image/week/blah/blah/blah.png
/dynamic/image/month/blah/blah/blah.gif
this urls are not actual filesystem paths but served by a php script ..
IN httpd.conf :
whey I try with the following configuration :
ExpiresActive On
ExpiredDefault A0
<LocationMatch */image/day/*>
ExpiresDefault A86400
</LocationMatch>
<LocationMatch */image/week/*>
ExpiresDefault A604800
</LocationMatch>
It applies last one 'A604800'(after week) expiry header to both type of files .. means even for /day/ type of urls the last ExpiresDefault is overriding to all the above ExpiresDefault .
I have also tried putting FilesMatch inside LocationMatch like this :
<LocationMatch */image/week/*>
<FilesMatch "\.(jpg|jpeg|png)">
ExpiresDefault A604800
</FilesMatch>
</LocationMatch>
But now not a single directing inside FilesMatch is executing meanse Expires header is not set ..
I have also tried using Header set ,, but looks like it doesn't follow Locationmatch condition but just last Header Set whould apply to all ..
Those are dynamic url , not actual directories , so I can't use different htaccess for different day,week etc ..
Any Solution ..?
I am trying to force no-cache on csv file on my site.
I added those lines to httpd.conf, by the documentation from apache:
ExpiresActive On
ExpiresDefault A0
<FilesMatch "\.(html|csv|htm)$">
ExpiresDefault A0
Header set Cache-Control "no-cache, no-store, must-revalidate, max-age=0, proxy-revalidate, no-transform"
Header set Pragma "no-cache"
</FilesMatch>
But when I am trying to get the page at the first time, i get this on the csv file :
Request Method:GET
Status Code:200 OK (from cache)
Do you have any idea what i am doing wrong?!
Thanks!!
Gabi.
I checked it out and saw I have Django Middleware enabled, thet handled the cache.. I disabled it and now it works..
Sorry.. :)