Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Apache 2 gzip json output by default?

Tags:

php

apache

gzip

I'm using this PHP code for json output. Does apache gzip it by default? Or, how can I check to make sure?

header('Content-type: application/json');
header('Cache-Control: max-age=0,no-cache,no-store,post-check=0,pre-check=0');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
echo json_encode($response);
like image 968
celwell Avatar asked Oct 08 '13 17:10

celwell


People also ask

Is gzip good for JSON?

Compressing with gzip As text data, JSON data compresses nicely. That's why gzip is our first option to reduce the JSON data size. Moreover, it can be automatically applied in HTTP, the common protocol for sending and receiving JSON.

How do you check GZIP compression is enabled or not?

Double click on the file and select headers. Under 'Response headers' you are looking for the 'Connection-Encoding' field, it will say gzip if it is enabled. NOTE: By default, Deflate is enabled on all of our servers.

Is Brotli better than gzip?

Brotli has a better compression ratio (i.e. it produces smaller compressed files) across every level of compression. While GZIP does beat Brotli on speed most of the time, the level you compress at factors into the results you'll see.


2 Answers

This is what did the trick for me:

(assuming you have access to apache configuration)

AddOutputFilterByType DEFLATE application/json

I added this line directly in /etc/apache2/mods-available/deflate.conf (so it will work for every json file on the webserver) maybe someplace else is better suited in your use case (e.g. if you only want to enable json compression for a single web application and not by default).

edit: In google chromes developer tools you can easily check if your content is served compressed or uncompressed: https://webmasters.stackexchange.com/a/4613

like image 192
mwallisch Avatar answered Oct 12 '22 18:10

mwallisch


No gzip is normally not used you have to enforce that yourself.

In the simplest case you just need to add this php line:

ob_start("ob_gzhandler");

See also the official php documentation.

like image 43
rekire Avatar answered Oct 12 '22 18:10

rekire