javascript gzip compression - apache

I am using the following code in my .htaccess file kept in the server root to enable gzip compression. However, the javascript is not getting compressed.
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/css text/plain text/xml text/javascript application/x-javascript application/x-httpd-php
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</IfModule>
How to enable compression of javascript? I have checked that mod_deflate is enabled and the index page is getting compressed.
My website is hosted here.

We are not treating javascript as a simple text file here. text/javascript should be replaced by application/javascript as shown.
Application/x-javascript was used for IE as it ignored scripts.
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/css text/plain text/xml application/javascript application/x-javascript application/x-httpd-php
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</IfModule>
Cheers!

Related

Problem with mod_deflate - compressing executable file

When trying to download file with.EXE extension in the site, the files are coming as .GZ
Environment
Centos 7 64
Apache 2.4.6
Changing the file in /etc/httpd/conf/httpd.conf I have already tried using each of these forms below, however, to no avail (I restarted apache and deleted the browser cache on each attempt):
1:
SetEnvIfNoCase Request_URI \.exe$ no-gzip dont-vary
2:
<FilesMatch \.exe$>
SetEnv no-gzip 1
</FilesMatch>
3:
SetEnv mod_deflate off
4:
SetEnv no-gzip off
5:
In the file /etc/httpd/conf.modules.d/00-base.conf I commented:
LoadModule deflate_module modules/mod_deflate.so
6:
I tried to delete the file, but it did not work.
/usr/lib64/httpd/modules/mod_deflate.so
I was reviewing httpd.conf and when I removed the addtype directive (AddType application / x-gzip .tgz .rar .zip .exe .dll) the EXE file was no longer compressed.
I had put this because the browser was taking too long to start the download EXE, in addition, RAR file instead of downloading, was being displayed as a binary in the browser.
So I did the following to solve (I do not know if it was the indicated output, but my knowledge is not enough, I'm new in this area):
<IfModule mod_deflate.c>
# compress text, html, javascript, css, xml...:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/atom_xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/x-httpd-php
AddOutputFilterByType DEFLATE application/x-httpd-fastphp
AddOutputFilterByType DEFLATE application/x-httpd-eruby
SetOutputFilter DEFLATE
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch ^HMSI[E] !no-gzip !gzip-only-text/html
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|zip|bz2|rar|exe|pdf|doc|dll|jpg|sit|mp4|mov|mp3|3gp|webm|rm|avi|ogv)$ no-gzip dont-vary
<FilesMatch \.exe$>
ForceType application/exe
Header set Content-Disposition attachment
</FilesMatch>
<FilesMatch \.rar$>
ForceType application/rar
Header set Content-Disposition attachment
</FilesMatch>
</IfModule>

Mod deflate: Server taking more resources after enabling compression in httpd.conf

I have added following snippet in httpd.conf
# mod_deflate configuration
<IfModule mod_deflate.c>
# Restrict compression to these MIME types
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/css
# Level of compression (Highest 9 - Lowest 1)
DeflateCompressionLevel 1
# Netscape 4.x has some problems.
BrowserMatch ^Mozilla/4 gzip-only-text/html
# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla/4\.0[678] no-gzip
# MSIE masquerades as Netscape, but it is fine
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
</IfModule>
Compression is working absolutely fine, but the server is taking too long to respond back to the requests, I have checked with top command as well, which shows 99% cpu usage.
How can I make this compression more efficient? or any other commands that may come handy in analyzing further?
If the responses are cacheable, add mod_expires and mod_cache+mod_disk_cache to dynamically cache the compressed variants.

Content-Encoding: gzip doesn't work for text/html Content-Type

I use the config below in my .htaccess. However, whilst other Content-Type get gzipped, the text/html Content-Type doesn't. Anyone knows why?
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
Header append Vary User-Agent env=!dont-vary
</IfModule>
Try to insert the AddType for html before it, like:
AddType text/html .html .htm
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
Header append Vary User-Agent env=!dont-vary
Here you have a complete list of MIME types: http://www.htaccess-guide.com/adding-mime-types/
*Edit: Try to change the rule then:
<ifModule mod_deflate.c>
<filesMatch "\.(js|css|html|php)$">
SetOutputFilter DEFLATE
</filesMatch>
</ifModule>
I ran across this while having the same issue on a Magento site. In my case the answer was to enable zlib output compression in php.ini because the html was being dynamically generated.
For me it was because of the charset. Whereas:
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/json text/json
did not work,
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript application/json text/json application/javascript;charset=utf-8 text/css;charset=utf-8
worked. I didn't feel like looking for better answers like how I can instruct Apache to parse out the charset from the content type and I figured that just giving the whole String as is should work. Fortunately it did for me.

Gzip compression (mod_deflate) not working

I have just added, or tried to add, gzip compression to my website in order to make it faster. This is the code I have put into .htaccess I got from this answer.
<IfModule mod_deflate.c>
#The following line is enough for .js and .css
AddOutputFilter DEFLATE js css
#The following line also enables compression by file content type, for the following list of Content-Type:s
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml
#The following lines are to avoid bugs with some browsers
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</IfModule>
So now I'm using a firebug extension called PageSpeed to monitor gzip compression and it is not working. I also use http://gtmetrix.com/ and the results are the same. Can anyone explain to me what more I have to do to enable gzip compression? Big thanks!

How to disable the DEFLATE module for a specific directory?

I am trying to figure out how to disable the DEFLATE module (gzipping) for a specific directory on my server. This is what I have in /etc/httpd/conf/httpd.conf
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
I can add something to my .htaccess file in the specific directory or even add a something to my /vhosts/domain.com/httpdocs/conf/vhosts.conf file. I can't seem to get it to work though. Any suggestions?
I removed my original code from /etc/httpd/conf/httpd.conf and added this to my vhost.conf on this domain
<Directory "/var/www/rockchurch.com/httpdocs">
AddOutputFilterByType DEFLATE html txt xml css js php
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
php_admin_value open_basedir none
php_admin_value safe_mode off
Options FollowSymLinks
</Directory>
<Directory "/var/www/rockchurch.com/httpdocs/tiny">
RemoveOutputFilter DEFLATE html txt xml css js php
</Directory>
And works well. Apparently having it in /etc/httpd/conf/httpd.conf universally adds it to all domains, which is great, but can't be changed in specific directories elsewhere.
Another way to accomplish this is to put the following line in a .htaccess file in the target directory:
SetEnv no-gzip 1
It worked for me when the GZIP was causing issues with mod_substitute.