i want to compress json response data . but it's not working.
cant't see the "Content-Encoding: gzip" in response headers and data size is not commpressed.
But html can be compressed
enter image description here
enter image description here
I can't see from your small screenshot, but you need to make sure the gzip_types directive includes application/json. For example:
gzip_types text/plain application/x-javascript application/json ...
This is probably declared in your nginx.conf.
If in doubt, a good default set up might be:
gzip_types
application/atom+xml
application/geo+json
application/javascript
application/x-javascript
application/json
application/ld+json
application/manifest+json
application/rdf+xml
application/rss+xml
application/vnd.ms-fontobject
application/wasm
application/x-web-app-manifest+json
application/xhtml+xml
application/xml
font/eot
font/otf
font/ttf
image/bmp
image/svg+xml
text/cache-manifest
text/calendar
text/css
text/javascript
text/markdown
text/plain
text/xml
text/vcard
text/vnd.rim.location.xloc
text/vtt
text/x-component
text/x-cross-domain-policy;
Remember text/html is always compressed by default, so you don't need to include that.
According the GTMetrix Page and Googe Pagespeed I need to enable for many ressources the GZIP compression. But www.checkgzipcompression.com says GZIP is enabled. How can I enable my GZIP Compression?
www.checkgzipcompression.com will count GZIP enable even if GZIP is enabled for a single filetype only while pagespeed tools like Pingdom and GTMetrix want you to integrate GZIP compression for all the file types out possible.
You can enable GZIP compression both manually by updating your .htaccess or by using a plugin which will do the same for you but with a user friendly UI.
If you want to enable GZIP compression manually, add the following code to your .htaccess file in your WP root directory.
<IfModule mod_deflate.c>
# Compress HTML, CSS, JavaScript, Text, XML and fonts
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE application/x-font
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE application/x-font-truetype
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE font/opentype
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
# Remove browser bugs (only needed for really old browsers)
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Header append Vary User-Agent
</IfModule>
The above code is for Apache server. If you're on NGINX server, use this code instead in nginx.conf file:
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
gzip_vary on;
gzip_types text/plain text/css text/javascript image/svg+xml image/x-icon application/javascript application/x-javascript;
Now, GZIP compression can be enabled with many caching plugins, popular of them is W3 Total Cache. Check this article on how to setup W3 Total Cache and it's GZIP compression module.
Code Source
In several nginx tutorial sites explaining "how to set up gzip compression," I've seen this list of MIME types repeated:
gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
However, I immediately found that this list did not result in compression being enabled for JavaScript in Chromium. I had to add application/javascript to the list. Which leads me to believe this list is outdated.
Is there a definitive list of all the content types I would want to gzip?
There is no definitive list of the file types you would want to gzip. Any file type readable as plain text (i.e. non-binary files) are able to be gzipped, and so a "definitive" list would be massive. Therefore, it ultimately depends on which file types you are actually serving, which you can check for any given file via the HTTP Content-Type header.
If you want to be doubly sure you are covering all possible MIME types for a particular extension (which I think is reasonable), Looking at this SO post, this text file contains a pretty darn exhaustive list.
It's important to note that some binary file types like .png and .pdf (even .woff) incorporate compression into the format itself and as such should not be gzipped (because doing so could produce a compressed file larger than the original). My rule of thumb is: if my code editor can't read the file as UTF-8 text, gzipping the file would not be wise (or at least it wouldn't be very efficient).
FWIW, I typically gzip the following formats (in my Apache .htaccess) on my site:
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/xml text/css text/javascript application/javascript application/x-javascript application/json application/xml image/svg+xml
</IfModule>
I have added the following code in my .htaccess file:
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
</ifmodule>
However, I have one URL "http://www.example.com/foo/" that doesn't behave well when gzipped.
This is for a wordpress site, so the /foo/ isn't a real folder/directory -- it's just the permalink slug. I've found the following code to exclude folders but I can't get it to work and I think it's because /foo/ is not an actual directory?
SetEnvIf request_uri ^/foo(.*) no-gzip dont-vary
Is there a way to exclude any URL that contains /foo/* from being deflated?
Thank you!
I am trying to enable gzip compression for components of my website. I have ubuntu 11.04 server and nginx 1.2.
in my nginx configuration of the website, i have this
gzip on;
#gzip_min_length 1000;
gzip_http_version 1.1;
gzip_vary on;
gzip_comp_level 6;
gzip_proxied any;
gzip_types text/plain text/html text/css application/json application/javascript application/x-javascript text/javascript text/xml application/xml application/rss+xml application/atom+xml application/rdf+xml;
#it was gzip_buffers 16 8k;
gzip_buffers 128 4k; #my pagesize is 4
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
and Yslow and google speed measures are advising me to use gzip to reduce transmission over network.
now when i try to curl -I my_js_file i got
curl -I http://www.albawaba.com/sites/default/files/js/js_367664096ca6baf65052749f685cac7b.js
HTTP/1.1 200 OK
Server: nginx/1.2.0
Date: Sun, 14 Apr 2013 13:15:43 GMT
Content-Type: application/x-javascript
Content-Length: 208463
Connection: keep-alive
Last-Modified: Sun, 14 Apr 2013 10:58:06 GMT
Vary: Accept-Encoding
Expires: Thu, 31 Dec 2037 23:55:55 GMT
Cache-Control: max-age=315360000
Pragma: public
Cache-Control: public
Accept-Ranges: bytes
any idea of what i have done wrong or what shall i do to get compressed content?
As others have written, it's not enough to enable gzip compression in your server -- the client also needs to ask for it in its requests via the Accept-Encoding: gzip header (or a superset thereof). Modern browsers include this header automatically, but for curl you'll need to include one of the following in your command:
-H "Accept-Encoding: gzip" : You should see the Content-Encoding: gzip header in the response (might need to output headers with curl's -v flag), as well as some seemingly garbled output for the content, the actual gzip stream.
--compressed : You should still see Content-Encoding: gzip in the response headers, but curl knows to decompress the content before outputting it.
I can't find anything obviously wrong with your config, usually gzip on & gzip_types application/x-javascript would be enough to get you going. If everything is working right you'll get a "Content-Encoding:gzip" returned back to you.
PLEASE KEEP IN MIND: I have much more consistency with GOOGLE DEVELOPER TOOLS (curl just doesn't behave the way a browser would).
In Chrome, right click and go to "inspect element" then go to "network" (then reload the page if you have to), then click on a resource and check the header tab, the output should look like this (notice the content-encoding is gzip, yay):
Request URL:https://ssl.gstatic.com/gb/js/sem_a3becc1f55aef317b63a03a400446790.js
Request Method:GET
Status Code:200 OK (from cache)
Response Headersview source
age:199067
cache-control:public, max-age=691200
content-encoding:gzip
content-length:19132
content-type:text/javascript
date:Fri, 12 Apr 2013 06:32:58 GMT
expires:Sat, 20 Apr 2013 06:32:58 GMT
last-modified:Sat, 23 Mar 2013 01:48:21 GMT
server:sffe
status:200 OK
vary:Accept-Encoding
version:HTTP/1.1
x-content-type-options:nosniff
x-xss-protection:1; mode=block
Anyway if you are SURE your content is not getting gzipped, I normally get up and running pretty fast with the following:
## Compression
gzip on;
gzip_buffers 16 8k;
gzip_comp_level 4;
gzip_http_version 1.0;
gzip_min_length 1280;
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript image/x-icon image/bmp;
gzip_vary on;
You could try this in replacement for your code, and/or tweak your values one at a time to help you localize your issue.
Remember to restart or reload nginx after changing the config.
It may also be useful to check your logs and see if there's anything interesting there should you still be stuck.
I just changed gzip_http_version 1.1; to be gzip_http_version 1.0; and then it worked
I had to enable gzip in my /etc/nginx/nginx.conf configuration:
gzip on;
gzip_disable "msie6";
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
Please note that I had to add application/javascript to the standard gzip_types configuration.
You need to run:
curl -I --compressed my_js_file
to make curl send an Accept-Encoding header for gzip - the server will only compress content if the client sends a header saying it will accept it.
NB you can write:
gzip_disable "msi6"
rather than using a regex to disable in IE 5.5 and 6, and you needn't specify text/html as a type because it is always compressed as long as gzip is activated.
I am just taking a guess here, but I think you may have to increase your gzip buffer size.
Here are the files that the browser pulls down from the domain. The number on the right is the file download size.
You may not be able to tell from the screen shot, but all of the text content files ARE gzipped, except for the js file you mention in your question. In the screenshot the js file is the file in green, with a size of about 200K. This file size is greater than what you have specified for your gzip buffers (128K).
The Gzip module docs do not really give a good indication as to what the gzip buffers are used for (whether the buffers are used for uncompressed or compressed data). However, the following post seems to indicate that the buffer size should be greater than the uncompressed file size: Large files with NGINX, GZip, and SSL
Here is my nginx configuration and it works.
gzip on;
gzip_min_length 1000;
gzip_buffers 4 8k;
gzip_http_version 1.0;
gzip_disable "msie6";
gzip_types text/plain text/css application/json application/javascript application/x-javasc ript text/xml application/xml application/xml+rss text/javascript;
gzip_vary on;
I think the keypoints are gzip, gzip_disable and gzip_types.
Just like Alaa I had to add gzip_http_version 1.0; (no version was previously specified) for it to work (I tried on Firefox 27.0.0).
I've experienced the same problem as Alaa, and the problem is caused by Antivirus software, that is currently installed on my computer.
Proxy servers and anti-virus software can disable compression when files are downloaded to a client machine. So if you are running web site in a browser on a client machine that is using such anti-virus software, or that sits behind an intermediate proxy server (many proxies are transparent, and you may not even be aware of a proxy intervening between your client and web server), they may be the cause of this issue.
Disabling antivirus solved my problem with browsers and you don't even need to set gzip_http_version to 1.0.
Hope that will help you.