Gzip compression in HTML response works only sometime - apache

I have a very annoying problem with my .htaccess. I saw it first time with the gzip compression but it affects Keep-alive and other things too.
The problem:
If I call the startpage of my website sometimes it gets the response as a compressed (gzip) version sometimes as a normal not compressed html.
(The transferred size of html sometimes is 9KB from 26KB sometimes 26KB from 26KB)
I tested it with different browsers and also with this online tool:
http://checkgzipcompression.com/
Sometimes it shows that I have compression active, sometimes it shows that it is disabled.
Here are some resouces
1) The good one (the response that I would expect all the time)
Accept-Ranges:bytes
Cache-Control:max-age=85419
Connection:keep-alive, close
Content-Encoding:gzip
Content-Length:8834
Content-Type:text/html
Date:Thu, 29 Oct 2015 10:59:55 GMT
ETag:"2282-5233bfba505e6"
Expires:Fri, 30 Oct 2015 10:43:35 GMT
Last-Modified:Thu, 29 Oct 2015 10:43:35 GMT
Server:Apache/2.4.10
Vary:Accept-Encoding,Host
2) The bad one: Response without keep-alive, Vary:Accept-Encoding, Content-Encoding
Cache-Control:max-age=85486
Connection:close
Content-Length:30347
Content-Type:text/html; charset=utf-8
Date:Thu, 29 Oct 2015 11:05:56 GMT
ETag:"e6fb558891db803ebcebd1b6aae83283"
Expires:Fri, 30 Oct 2015 10:50:42 GMT
Pragma:public
Server:Apache/2.4.10
X-Powered-By:PHP/5.3.28
3) And the ugly one: .htaccess (without redirects and rewrites)
### Begin: Browser caching of ressource files ###
# Enable long browser caching for JavaScript and CSS files.
# This affects Frontend and Backend and increases performance.
# You can also add other file extensions (like gif, png, jpg), if you want them to be longer cached, too.
<FilesMatch "\.(jpg|png|js|css)$">
<IfModule mod_expires.c>
ExpiresActive on
ExpiresDefault "access plus 7 days"
</IfModule>
FileETag MTime Size
</FilesMatch>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/css "access plus 7 days"
ExpiresByType text/javascript "access plus 7 days"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/bmp "access plus 1 year"
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType application/x-shockwave-flash "access plus 7 days"
ExpiresByType application/javascript "access plus 7 days"
ExpiresByType application/x-javascript "access plus 7 days"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresByType application/x-font-woff "access plus 1 year"
</IfModule>
<IfModule mod_deflate.c>
<FilesMatch "\.(js|jpg|jpeg|gif|png|svg|css)$">
ExpiresActive on
ExpiresDefault "access plus 1 month"
SetOutputFilter DEFLATE
</FilesMatch>
</IfModule>
# BEGIN keep alive
<ifModule mod_headers.c>
<FilesMatch "\.(js|css|xml)$">
Header append Vary Accept-Encoding
</FilesMatch>
### Begin: Static File Cache (options) ####
# Set proper content type and encoding for gzipped html.
<FilesMatch "\.gz">
Header set Content-Encoding gzip
</FilesMatch>
# if there are same problems with ForceType, please try the AddType alternative
# Set proper content type gzipped html
<FilesMatch "\.html\.gz">
ForceType text/html
</FilesMatch>
<FilesMatch "\.xml\.gz">
ForceType text/xml
</FilesMatch>
<FilesMatch "\.rss\.gz">
ForceType text/xml
</FilesMatch>
### End: Static File Cache ###
Header set Connection keep-alive
</ifModule>
# END keep alive
### End: Browser caching of ressource files ###
Facts:
I have no LoadBalancing.
The same config is working like a charm on my local machine
The same .htaccess is working on the same server on a different VirtualHost (staging server)
(They could have slightly different php.ini)
How do I test it:
I simply pressing Crtl+F5 and experience response 1 and 2 randomly.

Related

expire headers and cache control with mod header

I have added expire header in my htaccess file so is there still need to add mod_header for cache-control?
My expire header is :
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
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"
ExpiresByType text/css "access 1 month"
ExpiresByType application/javascript "access plus 1 year"
</IfModule>
And My mod_header for cache-control is :
<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
<FilesMatch "\.(js|css|swf)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
Let's read the documentation for 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.
The answer is therefore: It depends.
mod_expires sets the max-age directive, but does not set the directive that tells the client if the cache is public or private. When nothing is defined, I believe the cache will do a best-effort in determining if it wants to cache this particular response. On the other hand, it isn't possible to set the Expires-header with mod_header.
I think in your case the mod_header rules are obsolete, but you might want to inspect requests in your browser to see if caching happens as you expect.

Keep-Alive doesn't allow all persistent connections

I want to speed up my website. I runned test on http://www.webpagetest.org/ website that checks performance and optimization of website.
I enabled Keep-Alive inside .htaccess file. Problem is that result of testing says that only 18% of all connections are allowed (that ones that are linked or included via google)
ScreenShot:
Can someone explain to me how to allow all connection?
CODE THAT I USED:
<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 1 month”
# Javascript
ExpiresByType application/javascript "access plus 1 year"
</IfModule>
<IfModule mod_headers.c>
<FilesMatch ".(js|css|xml|gz|html)$">
Header append Vary: Accept-Encoding
</FilesMatch>
</IfModule>
<IFModule mod_deflate.c>
<filesmatch "\.(js|css|html|jpg|png|php)$">
SetOutputFilter DEFLATE
</filesmatch>
</IFModule>
<IfModule mod_headers.c>
Header set Connection keep-alive
</IfModule>
Enable KeepAlive configuration in your Apache Configuration file
KeepAlive On

htaccess file being ignored, trying to leverage browser caching for Pagespeed

I'm having a problem with my .htaccess file. I'm optimizing my site using Google Pagespeed and GTMetrix and both keep saying "leverage browser caching". So I just added my .htaccess file and included the following lines
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresByType application/xhtml+xml "access plus 600 seconds"
ExpiresDefault "access 1 month"
</IfModule>
## EXPIRES CACHING ##
# BEGIN Cache-Control Headers
<ifModule mod_headers.c>
<filesMatch "\.(ico|jpe?g|png|gif|swf)$">
Header set Cache-Control "public"
</filesMatch>
<filesMatch "\.(css)$">
Header set Cache-Control "public"
</filesMatch>
<filesMatch "\.(js)$">
Header set Cache-Control "private"
</filesMatch>
<filesMatch "\.(x?html?|php)$">
Header set Cache-Control "private, must-revalidate"
</filesMatch>
</ifModule>
# END Cache-Control Headers
I first had nothing, then I tried with just the 'expires caching', first piece of code and after that didn't change a thing in the re-tested results, I added the second pair of code -> cache control headers. Strangely, I'm still scoring the same.
I'm on the verge of thinking my HTaccess file looks alright, but there is a problem on perhaps my server.. Or well, I just don't know. You guys probably do!
My .htaccess file is in the same folder as the index.html and assets folders are located.
The file structure looks like this:
/public_html/domain/company_name/website/.htaccess - For the .htaccess file
/public_html/domain/company_name/website/index.html - For the index.html file
THIS URL will link to the index.html file above.
When writing the title, I saw numbers of other related topics, however, most of them didn't work.
Where many related topics posted something about a corrupted or interfering file in the /etc/.. I don't have a /etc/ folder, so that couldn't be it.
Ok, so I found the problem! For anyone who can't find why it ain't working for them, try this!
My problem was that mod_expires wasn't active.
Create a php file and paste in the following code
<?php
print_r(apache_get_modules());
?>
And see if mod_expires is active. If it isn't showing, try this tutorial to active mod_expires. Then edit your htaccess file again and you'll probably see it's working!

Apache not sending Last-Modified header even if explicitly set

I have a problem with the Last-Modified header.
I created a simple test page:
<?
header('Last-Modified: Thu, 14 Feb 2013 12:41:31 GMT');
?>
When I open this test page with my browser, if I check the headers there's no Last-Modified header. All the other standard headers are set correctly. Anyway, if I access the test page with https, the Last-Modified header is set.
I also tried the same test on another server, and I can see the header correctly set even without https.
So, what could block/unset the header on the first server? I'm not including the httpd.conf file because is very long. I'll just include the caching section, even if I don't see anything wrong or related to this problem:
ServerSignature Off
ServerTokens Prod
Header unset ETag
FileETag None
<FilesMatch "(?i)^.*\.(ico|flv|jpe?g|png|gif|js|css).*$">
Header set Cache-Control "public, no-transform"
Header unset Vary:
</FilesMatch>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault A2592000
ExpiresByType text/css "access plus 1 week"
ExpiresByType text/plain "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType application/x-javascript "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType image/x-icon "access plus 1 year"
</IfModule>
Thanks in advance
try
ssi off;
in nginx config for this domain
There doesn't appear to be anything wrong with the header, but using <? ?> instead of <?php ?> can cause problems on some servers.

Last-Modified not working for .htaccess

I'm tyring to implement browser caching and follow Google PageSpeed's recommendation about setting Last-Modified to a data that is "sufficiently far enough in the past." I have the following in my .htaccess:
<IfModule mod_headers.c>
<FilesMatch "\.(json|pdf|swf|bmp|gif|jpeg|jpg|png|svg|tiff|ico|flv|js)$">
Header Set Last-Modified "Fri, 01 Jan 2010 12:00:00 GMT"
</FilesMatch>
</IfModule>
I have mod_headers installed on my server.
Unfortunately, Google PageSpeed still complains and warns me:
Leverage browser caching
The following cacheable resources have a short freshness lifetime. Specify an expiration at least one week in the future for the following resources:
And then lists PNGs, GIFs, JPGs, etc. Yahoo YSlow says basically the same thing.
Looking at the response headers of one of my resources that should be caching, I see this:
Date: Tue, 19 Oct 2010 20:12:04 GMT
Server: Apache/2.2.14 (Ubuntu)
Last-Modified: Tue, 07 Sep 2010 23:51:33 GMT
Etag: "2e0e34-2a43-48fb413a96a20"
Accept-Ranges: bytes
Content-Length: 10819
Content-Type: image/png
As you can see, the Last-Modified data does not match what I specified in .htaccess.
Any ideas what I am doing wrong?
Removing Last-Modified is not what Google PageSpeed is asking. It wants to see the following headers in your servers response when browsers asks for static files:
Cache-Control max-age=...
Expires ...
in place of dots the server will place values.
In order to do this, you simply need to add to .htaccess the following lines:
<IfModule mod_headers.c>
<FilesMatch "\.(json|pdf|swf|bmp|gif|jpeg|jpg|png|svg|tiff|ico|flv|js)$">
ExpiresActive On
ExpiresDefault "access plus 1 year"
Header append Cache-Control "public"
</FilesMatch>
</IfModule>
You'll see Google PageSpeed stopping to complain.
Have you considered just using unset Last-Modified?
Example:
<IfModule mod_headers.c>
<FilesMatch "\.(json|pdf|swf|bmp|gif|jpeg|jpg|png|svg|tiff|ico|flv|js)$">
Header unset Last-Modified
</FilesMatch>
</IfModule>
The FilesMatch section looks fine, so it's probably just some fiddly bit with Header Set. Hell, might even be case sensitive. Try Header set instead of Header Set
If this isn't what you want, then let me know and I'll think about it a bit more. Unset should work though,
This works:
<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 1 month"
# Javascript
ExpiresByType application/javascript "access plus 1 year"
</IfModule>