Remove the Exipre header in Linux apache server - apache

I am getting Expire Header as Thu, 19 Nov 1981 08:52:00 GMT on my webpage how i can remove expire header using apache.
Thanks
Thanigaivelan

In your .htacess:
ExpiresActive Off
It is not good to disable this complete.
Also take a look at mod_expires.
This module controls the setting of the Expires HTTP header and the
max-age directive of the Cache-Control HTTP header in server
responses. The expiration date can set to be relative to either the
time the source file was last modified, or to the time of the client
access.
These HTTP headers are an instruction to the client about the
document's validity and persistence. If cached, the document may be
fetched from the cache rather than from the source until this time has
passed. After that, the cache copy is considered "expired" and
invalid, and a new copy must be obtained from the source.
To modify Cache-Control directives other than max-age (see RFC 2616
section 14.9), you can use the Header directive.
When the Expires header is already part of the response generated by
the server, for example when generated by a CGI script or proxied from
an origin server, this module does not change or add an Expires or
Cache-Control header.
ExpiresByType text/html "access plus 1 month 15 days 2 hours"
ExpiresByType image/gif "modification plus 5 hours 3 minutes"

Related

AEM Dispatcher (4.3.3) always returning 200 instead of 304 (Apache 2.4.6)

I have configured ETags (using ACS Commons ETag support) on my AEM server and disabled ETag on Apache. But once the file gets cached on the Dispatcher, Apache always returns 200 with response body instead of 304 Not modified. I have validated that the ETag value stored in the ".h" file and in the response is the same as the value of the "If-None-Match" header of the request.
If I remove the cached files from the dispatcher and resend the request then AEM correctly returns 304.
I have also disabled mod_deflate as I have found at some places that the deflate's "-gzip" suffix could cause issues with ETag matching.
Also, instead of ETag, I have tried a similar thing with Last-Modified and If-Modified-Since headers. But no luck with that as well. I have noticed that we generally have Last-Modified headers in place in most cases and I have never seen 304. So, it seems like it is not a version-specific issue. I couldn't find any configuration documentation related to this. Could someone please guide me where am I going wrong?
Check this https://issues.apache.org/bugzilla/show_bug.cgi?id=45023
I have enabled deflate
RequestHeader edit "If-None-Match" "^\"(.*)-gzip\"$" "\"$1\""
Header edit "ETag" "^\"(.*[^g][^z][^i][^p])\"$" "\"$1-gzip\""
We don't use Etags since it is diffcult to sync them across a cluster and Last Modified works fine.
# turn off Etags completely, since they will differ across the cluster
FileETag None
# FileETag None is not enough for every server.
Header unset ETag
# instead we use Expires and Cache-Control headers
ExpiresActive On
# set Expires default to 15 minutes, so browser caches for a visit
ExpiresDefault "access plus 15 minutes"
# but a maybe few types are exempt from this
ExpiresByType text/cache-manifest "access plus 0 seconds"
ExpiresByType text/html "access plus 0 seconds"
ExpiresByType text/xml "access plus 0 seconds"
ExpiresByType application/xml "access plus 0 seconds"
ExpiresByType application/json "access plus 0 seconds"
# set CacheControl public header
# so content is cached in Firefox, even over https
# "public" keyword MUST be the first value in the header, or it will not work in FF
Header onsuccess edit Cache-Control "^(.*)$" "public, $1"
You can adjust the timeouts accordingly, depending on your content.

.htaccess ExpiresDefault 1 day

If I add only these 2 lines to the .htacces file, will all the files on the website be kept in the cache for 1 day?
ExpiresActive On
ExpiresDefault "access plus 1 day"
Is it enough and it works?
ExpiresActive On
ExpiresDefault "access plus 1 day"
If you have no other overriding directives then these directives do indeed instruct the browser to cache the response for 1 day, by sending back the appropriate Expires and Cache-Control: max-age HTTP response headers. (Expires is required for old browsers. All modern browsers will use the Cache-Control header instead.)
Is it enough and it works?
Whether it is "enough" is entirely dependent on the nature of your site. Depending on your site, some responses perhaps shouldn't be cached at all, whilst other static resources should be cached for much longer.
And it's entirely possible you have other directives or even a front-end proxy or CDN that overrides these response headers.

Apache: set max-age or expires in .htaccess for directory

I have a handful of directories with content which will never change.
Is it possible to create .htaccess file which tells the browser that anything in this directory and sub- directories can be cached for a very long time?
I would like to copy the same .htaccess file in each directory if possible.
If this is possible would you recommend max-age over expires or both?
So it does look possible.... the .htaccess file syntax is:
Header unset Last-Modified
FileETag none
ExpiresActive On
ExpiresDefault "access plus 1 years"
This will turn off Etags and turn on cache-control: max-age
Then put this .htaccess file in the directory and all files (including it's sub-directories will be cached for 1 year.
I decided to put all my cache-able content under a single root directory and edit the httpd.conf as
<Directory "C:\somedir\cache">
Header unset Last-Modified
FileETag none
ExpiresActive On
ExpiresDefault "access plus 1 years"
</Directory>
I am still in the process of testing this. I just hope this does not turn off Etags for the rest of the site. So far it looks like it's working as planned.
UPDATE (after 6 months):
Setting the ExpiresDefault and allowing e-tags is the best thing to do.
in httpd.conf:
<Directory "C:\somedir\cache">
ExpiresActive On
ExpiresDefault "access plus 1 year"
</Directory>
Make sure "somedir" is inside of the apache root (such as htdocs).
Allowing e-tags is a good because after 1 year, the browser will re-validate the file by passing the e-tag. The web server will send back a 304 - Not Modified and reset the max-age to 1 year. This is very efficient.
All in all, you can watch the apache log file and see that items in /cache dir are begin served once.
Note: I have found that setting Header append Cache-Control "public" is ok to do if you want.
Final Version:
Here's the final version: (just add this at the bottom of the httd.conf)
<Directory "C:\somedir\cache">
ExpiresActive On
ExpiresDefault "access plus 1 year"
Header append Cache-Control "public"
</Directory>
Inspecting the header should reveal this:
Accept-Ranges:bytes
Cache-Control:max-age=31536000, public
Connection:Keep-Alive
Content-Language:en
Content-Length:746
Content-Type:text/css
Date:Thu, 29 May 2014 15:23:50 GMT
ETag:"240000000add63-2ea-4f4086d72ad01"
Expires:Fri, 29 May 2015 15:23:50 GMT
Keep-Alive:timeout=40, max=200
Last-Modified:Fri, 07 Mar 2014 18:28:59 GMT
This will:
Set the max-age for 1 year (the longest recommended)
Send the expires tag of 1 year
Send an Etag, so after 1 year the browser will perform etag validation
Let intermediate caching devices/services know that they can cache the file for 1 year.
FYI, if you do what is mentioned above and your Apache won't restart then you may be getting this error:
The Apache service named reported the following error:
>>> Invalid command 'ExpiresActive', perhaps misspelled or defined by a module not included in the server configuration.
You can find that error by clicking "Start", type in "Computer Management", launch it, in the tree open "Event Viewer -> Windows Logs -> Application". That's where I found the error above.
Easy fix, just uncomment this line in httpd.conf:
#LoadModule expires_module modules/mod_expires.so

Overwrite cache-headers with mod_expires

I want to set cache-headers using the mod_expires module from apache. My configuration looks somewhat like this:
<LocationMatch ^/static >
ExpiresDefault "access plus 1 years"
</LocationMatch>
The problem is, that the files are generated by a third system I don't control. These system provides files with the following headers:
Date Mon, 24 Oct 2011 08:39:02 GMT
Cache-Control no-cache,no-store,must-revalidate
Pragma no-cache
Expires Thu, 01 Dec 1994 16:00:00 GMT
These headers makes it impossible to set the cache-headers with mod_expires. http://httpd.apache.org/docs/2.2/mod/mod_expires.html tells us why:
When the Expires header is already part of the response generated by the server, for example when generated by a CGI script or proxied from an origin server, this module does not change or add an Expires or Cache-Control header.
Is there any possible way to circumvent this rule and overwrite the headers with mod_expires?
Update:
One possible solution, to avoid this limitation is to use only mod_headers to set the cache-headers. Unfortunately, this isn't an alternative because the values have to be calculated.
Thanks it advance.
Unfortunately, it's a known limitation and we had to fall back to use only mod_headers.
Regilero's suggestion won't work because header directives will be processed very late in the response processing - after mod_expire directive. So you'd unset the headers after mod_expires did (or didn't) what it was supposed to do.
If it's apache 2.2 you could try putting early at the end of each header directive. That will tell it to do this in an early stage of response processing as opposed to at the end.
so try:
<LocationMatch ^/static >
Header unset Cache-Control early
Header unset Pragma early
Header unset Expires early
ExpiresDefault "access plus 1 years"
</LocationMatch>
Haven't tested tho, but give it a try...
Have you tried mixing it with mod_headers?
<LocationMatch ^/static >
Header unset Cache-Control
Header unset Pragma
Header unset Expires
ExpiresDefault "access plus 1 years"
</LocationMatch>
Not tested, but in case of...

With Apache httpd, how do I configure no caching for a given UserAgent?

I have Apache HTTPD configured to add a cache header to requests for most static content:
ExpiresActive On
ExpiresDefault "access plus 1 year"
# Force JNLP and BSH files to expire immediately so updates are checked for
# and seen (We need this so we see changes in the dynamic content in both)
ExpiresByType application/x-java-jnlp-file "now"
ExpiresByType application/x-bsh "now"
How can I disable this caching for any request where the UserAgent contains the string JNLP? When the request comes from the User Agent JNLP (for example "User-Agent: JNLP/6.0 javaws/1.6.0_12 (b04) Java/1.6.0_12") I don't want any Cache-Control or other cache-related headers on the HTTP response.
I can find configuration examples for several things based on user agent, but I cannot figure out how to configure caching depending on the user agent.
Your ExpiresByType directive looks like a good idea... if that's not enough, then try using BrowserMatch:
BrowserMatch JNLP ua_is_jnlp
This sets the environment variable ua_is_jnlp to some value for any request whose user agent header contains the string JNLP (there is also a case-insensitive variant, BrowserMatchNoCase). Then you can clear any caching headers with
Header unset Cache-Control env=ua_in_jnlp
Header unset Expires env=ua_in_jnlp
although if you want to disable caching entirely, you'd be better off setting
Header set Cache-Control no-cache env=ua_in_jnlp
Header set Expires 0 env=ua_in_jnlp