mod_headers set cookie expire date - apache

Currently, I'm using this code in my Apache config to save a cookie on an image request:
<FilesMatch "^image\.jpg$">
Header set Set-Cookie: "cookiename=value"
</FilesMatch>
How can I set the cookie expiry date? I tried googling it but came up with nothing..
Thanks,

should look into http header than apache directive. Take a look here. http://en.wikipedia.org/wiki/HTTP_cookie#Expires_and_Max-Age

Related

How to remove expiration header from apache

I have been using the apache mod expiration module for setting an expiration HTTP header (https://httpd.apache.org/docs/2.4/mod/mod_expires.html). This module set an expiration header in all server responses.
I want to remove the expiration date for all the redirect 302. Is there a way to remove the expiration header for 302 redirects.
Thank you,
It can be achieved by the below code.
Header unset Expires "expr=%{REQUEST_STATUS} == 302"

The files served by my Apache Server are not being saved on browser cache

I have installed and configured an Apache server with a virtualhost which serves images.
When I load my page with images on the browser the second time (the images should be on cache after the first time the page is loaded), the browser doesnt get the images (or files) from cache, and think that it should.
What is wrong? I am using Google Chrome, and when I load some other web from other server the cache works, so I think that I have some problem with the Apache, but I am not sure.
Thank you very much.
The response is a video-segment which is played by Dash player.
Response headers:
accept-ranges:bytes
content-length:194431
date:Wed, 09 Mar 2016 07:42:07 GMT
etag:"2f77f-52acd33f8b167"
last-modified:Tue, 02 Feb 2016 17:55:12 GMT
server:Apache/2.4.18 (Unix) OpenSSL/1.0.2e PHP/7.0.2
status:200
After doing that:
ExpiresActive On
# Set up caching on media files for 1 year (forever?)
<FilesMatch "\.(mp4|m4s)$">
ExpiresDefault "access plus 3600 seconds"
Header set Cache-Control "public"
Header set Content-Type "video/mp4"
Header set Vary "Host"
Header set Access-Control-Allow-Origin "*"
</FilesMatch>
The headers are:
accept-ranges:bytes
access-control-allow-origin:*
cache-control:public
content-length:194431
content-type:video/mp4
But it doesnt save on the cache, I mean, when I reload the web or the video, it does not get it from the cache as it should.
You are not including any cache-control headers telling the browser to cache the resources. The spec is unclear how to handle this but most browsers choose not to cache unless explicitly told to.
You need to include Apache config like this (assuming you are already including the mod_expiries module):
ExpiresActive On
# Set up caching on media files for 1 week
<filesMatch ".([iI][cC][oO]|[gG][iI][fF]|[jJ][pP][gG]|[jJ][pP][eE][gG]|[pP][nN][gG]|[fF][lL][vV]|[pP][dD][fF]|[sS][wW][fF]|[mM][oO][vV]|[mM][pP]3|[wW][mM][vV]|[pP][pP][tT])$">
ExpiresDefault A604800
Header append Cache-Control "public"
</filesMatch>
# Set up caching on font files for 6 months
<filesMatch ".([eE][oO][tT]|[tT][tT][fF]|[sS][vV][gG]|[Ww][Oo][Ff][Ff]|[Ww][Oo][Ff][Ff]2)$">
ExpiresDefault 15552000
Header append Cache-Control "public"
</filesMatch>
Which will then produce a header like this:
cache-control:max-age=10800, public
I've written a blog post on this if you want more details: https://www.tunetheweb.com/performance/http-performance-headers/caching/
OK, I have solved the problem.
I was using an https connection and the certificate I was using didnt have the correct parameters (common name must be the name of the url programmed on the server) and the certificate of the server must be authenticate by some CA, so I have made CA certificates to authenticate the server certificates. This means that if you have an unsecure https connection, Chrome does not save files on cache.
After having all the certificates corrected, I have upload to google chrome the certificates to trust my web (configuration of google Chrome, advanced options, Manage https/ssl certificates). After that, I can save files on cache because of the certificates which makes the https connection secure (green lock on the chrome url bar).
That was my problem and this is how I solved it.
Thanks for your answers and your time!! ;)

How to set expires tags on images that are on a CDN

My site runs on Apache and I have set expires headers in my .htaccess file like so:
<ifmodule mod_expires.c>
<FilesMatch "\.(ico|jpg|jpeg|png|gif|swf)$">
ExpiresActive On
ExpiresDefault "access plus 1 year"
Header append Cache-Control "public"
</FilesMatch>
</ifmodule>
This works great for images that come from my domain. However, 95% of my images are hosted on a cdn called CDN77, which runs on Nginx.
When I run Yslow on my site, it says that all the images being served from CDN77 do not have expires set. I have also confirmed the expires header is not set for those images by viewing the headers in LiveHTTP (Firefox headers viewer plugin).
CDN77 has said they have no way on their end to configure this.
Any thoughts or ideas as to how to set the expires for the images served from the cdn?
Change your CDN. First of all, they SHOULD allow clients to create exceptions for headers and nginx has a very elaborate configuration set for that. Secondly, the default mode should be to copy response headers from the upstream server (you), and only change or add headers that is specific to the CDN, like the host header, server header and strip any cookie headers you may be sending.
You’re not able to set expiry headers on files that are hosted on CDN77. If you require custom cache headers, you’ll need to contact their support department.
Expiry headers will be applied to all files on your CDN. It’s not possible to apply different expiry values to indiviudal files or specific file types.

Can browser display objects from its cache without receiving a 304 status code?

I'm trying to understand if is it possible to avoid request for some embedded objects, loading them directly from cache without asking to web server if the object is valid or not (i don't want web server response to me with 304 http status code) Is it possible ? Does the expire header works for this way? How?
Of course: Request:
<script scr="my_js.php"></script>
Response:
<? header("HTTP/1.1 304 Not Modified");
header("Expires: Mon, 31 Dec 2035 12:00:00 gmt");
header("Cache-Control: max-age=".(60*60*24*365));
echo "//this is a simpe example"; ?>
Solved
Browser loads resources from his cache without asking them to the web server only the first time you open the page (new tab or new browser window).
The other times browser ALWAYS ask information to the server about the resources saved in his cache. Then, the web server response with 200 or 301.
Yes, setting a distant expiry header and the asset will not be downloaded again until that expiry.
If you remove the Last-Modified and ETag header, you will totally eliminate If-Modified-Since and If-None-Match requests and their 304 Not Modified Responses, so a file will stay cached without checking for updates until the Expires header indicates new content is available!
Source.
From my htaccess ...
<IfModule mod_headers.c>
Header unset Pragma
FileETag None
Header unset ETag
# cache images/pdf docs for 10 days
<FilesMatch "\.(ico|pdf|jpg|jpeg|png|gif|js)$">
Header set Expires "Mon, 31 Dec 2035 12:00:00 gmt"
Header unset ETag
Header unset Last-Modified
</FilesMatch>
# cache html/htm/xml/txt diles for 2 days
<FilesMatch "\.(html|htm|xml|txt|xsl)$">
Header set Cache-Control "max-age=7200, must-revalidate"
</FilesMatch>
</IfModule>
it seems doesn't works .... for example firebug's net panel show me always 200 status code and access.log file report me that external objects are always requested by the browser.

Apache caching problem

I'm loading some json through apache as per:
http://arguments.callee.info/2010/04/20/running-apache-and-node-js-together/
The JSON however is outdated when I use the apache url. The node.js :8000 url serves the correct data.
How can I make sure apache doesn't cache json?
Thanks.
You can append a "cache killer" on the URL you are fetching asynchronously. That is some value that will always make the URL unique.
var url = "http://example.com/service.json?" + new Date().getTime();
A possible solution would be to setup the expire headers to the past and make sure that the browser does not cache nay json via cache-control haders for json files and
You can try to add this to your apache config file :
<FilesMatch "\.(json|json)$">
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Thu, 01 Jan 1970 00:00:00 GMT"
</FilesMatch>
The mod_headers module will need to be installed in Apache to use this method.
If you are interested you can have a read at the roots
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9